#include "config.h"

#include <bitcoin/chainparams.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/json_stream.h>
#include <plugins/bkpr/bookkeeper.h>
#include <plugins/bkpr/channel_event.h>
#include <plugins/bkpr/descriptions.h>
#include <plugins/bkpr/rebalances.h>

struct channel_event *new_channel_event(const tal_t *ctx,
					const char *tag,
					struct amount_msat credit,
					struct amount_msat debit,
					struct amount_msat fees,
					const struct sha256 *payment_id TAKES,
					u32 part_id,
					u64 timestamp)
{
	struct channel_event *ev = tal(ctx, struct channel_event);

	ev->tag = tal_strdup(ev, tag);
	ev->credit = credit;
	ev->debit = debit;
	ev->fees = fees;
	ev->payment_id = tal_dup_or_null(ev, struct sha256, payment_id);
	ev->part_id = part_id;
	ev->timestamp = timestamp;

	return ev;
}

void json_add_channel_event(struct json_stream *out,
			    const struct bkpr *bkpr,
			    struct channel_event *ev)
{
	json_object_start(out, NULL);
	json_add_string(out, "account", ev->acct_name);
	json_add_string(out, "type", "channel");
	json_add_string(out, "tag", ev->tag);
	json_add_amount_msat(out, "credit_msat", ev->credit);
	json_add_amount_msat(out, "debit_msat", ev->debit);
	if (!amount_msat_is_zero(ev->fees))
		json_add_amount_msat(out, "fees_msat", ev->fees);
	json_add_string(out, "currency", chainparams->lightning_hrp);
	json_add_currencyrate(out, "currencyrate", bkpr, ev->timestamp);
	if (ev->payment_id) {
		const char *desc = channel_event_description(bkpr, ev);
		json_add_sha256(out, "payment_id", ev->payment_id);
		json_add_u32(out, "part_id", ev->part_id);
		if (desc)
			json_add_string(out, "description", desc);
	}
	json_add_u64(out, "timestamp", ev->timestamp);
	json_add_bool(out, "is_rebalance",
		      find_rebalance(bkpr, ev->db_id) != NULL);
	json_object_end(out);
}
