using Books.Api.EventFlow.ReadModels;
using GraphQL.Types;

namespace Books.Api.GraphQL.Types;

public class JournalEntryDraftType : ObjectGraphType<JournalEntryDraftReadModelDto>
{
    public JournalEntryDraftType()
    {
        Name = "JournalEntryDraft";
        Description = "A journal entry draft (kassekladde) - work in progress before posting";

        Field(x => x.Id).Description("Unique identifier");
        Field(x => x.CompanyId).Description("Company this draft belongs to");
        Field(x => x.Name).Description("Draft name");
        Field(x => x.VoucherNumber).Description("Bilagsnummer - unique document number (required by Bogføringsloven § 7)");
        Field(x => x.DocumentDate, nullable: true).Description("Bilagsdato - the document/transaction date (e.g., invoice date)");
        Field(x => x.Description, nullable: true).Description("Entry description");
        Field(x => x.FiscalYearId, nullable: true).Description("Fiscal year ID");
        Field(x => x.Status).Description("Draft status: active, posted, or discarded");
        Field(x => x.TransactionId, nullable: true).Description("Transaction ID after posting");
        Field(x => x.CreatedBy).Description("User who created the draft");
        Field(x => x.CreatedAt).Description("When the draft was created");
        Field(x => x.UpdatedAt).Description("When the draft was last updated");

        Field<NonNullGraphType<ListGraphType<NonNullGraphType<DraftLineType>>>>("lines")
            .Description("The posting lines with VAT codes")
            .Resolve(ctx => ctx.Source.GetLines());

        Field<NonNullGraphType<ListGraphType<NonNullGraphType<StringGraphType>>>>("attachmentIds")
            .Description("References to attached documents (bilag - required by Bogføringsloven § 6)")
            .Resolve(ctx => ctx.Source.GetAttachmentIds());

        Field<StringGraphType>("extractionData")
            .Description("Full AI extraction data as JSON (vendor CVR, amounts, VAT, due date, payment reference, line items)")
            .Resolve(ctx => ctx.Source.ExtractionData);
    }
}

public class DraftLineType : ObjectGraphType<Domain.JournalEntryDrafts.DraftLine>
{
    public DraftLineType()
    {
        Name = "DraftLine";
        Description = "A single posting line in a journal entry draft";

        Field(x => x.LineNumber).Description("Line number for ordering");
        Field(x => x.AccountId, nullable: true).Description("Account ID");
        Field(x => x.DebitAmount).Description("Debit amount");
        Field(x => x.CreditAmount).Description("Credit amount");
        Field(x => x.Description, nullable: true).Description("Line description");
        Field(x => x.VatCode, nullable: true).Description("VAT code (momskode) - e.g. U25, I25, IEUV for SKAT compliance");
    }
}
