using GraphQL.Types;

namespace Books.Api.GraphQL.InputTypes;

public class CreateJournalEntryInputType : InputObjectGraphType<CreateJournalEntryInput>
{
    public CreateJournalEntryInputType()
    {
        Name = "CreateJournalEntryInput";
        Description = "Input for creating a journal entry (bilag) with double-entry bookkeeping";

        Field(x => x.FiscalYearId).Description("Fiscal year ID for period locking (required)");
        Field(x => x.Description, nullable: true).Description("Description of the journal entry");
        Field(x => x.ReferenceId, nullable: true).Description("External reference (e.g. invoice number)");
        Field(x => x.End2EndId, nullable: true).Description("End-to-end tracking ID (generated if not provided)");
        Field(x => x.IdempotencyKey, nullable: true).Description("Idempotency key for duplicate prevention (generated if not provided)");
        Field<NonNullGraphType<ListGraphType<NonNullGraphType<JournalEntryPostingInputType>>>>("postings")
            .Description("The postings (posteringer) - must balance (debits = credits)");
    }
}

public class JournalEntryPostingInputType : InputObjectGraphType<JournalEntryPostingInput>
{
    public JournalEntryPostingInputType()
    {
        Name = "JournalEntryPostingInput";
        Description = "A single posting (postering) in a journal entry";

        Field(x => x.AccountId).Description("Account ID (required)");
        Field(x => x.DebitAmount, nullable: true).Description("Debit amount (positive, mutually exclusive with creditAmount)");
        Field(x => x.CreditAmount, nullable: true).Description("Credit amount (positive, mutually exclusive with debitAmount)");
        Field(x => x.Currency, nullable: true).Description("Currency code (default: DKK)");
    }
}

public class CreateJournalEntryInput
{
    public string FiscalYearId { get; set; } = string.Empty;
    public string? Description { get; set; }
    public string? ReferenceId { get; set; }
    public string? End2EndId { get; set; }
    public string? IdempotencyKey { get; set; }
    public List<JournalEntryPostingInput> Postings { get; set; } = [];
}

public class JournalEntryPostingInput
{
    public string AccountId { get; set; } = string.Empty;
    public decimal? DebitAmount { get; set; }
    public decimal? CreditAmount { get; set; }
    public string? Currency { get; set; }
}
