using GraphQL.Types;

namespace Books.Api.GraphQL.InputTypes;

public class CreateJournalEntryDraftInputType : InputObjectGraphType<CreateJournalEntryDraftInput>
{
    public CreateJournalEntryDraftInputType()
    {
        Name = "CreateJournalEntryDraftInput";
        Description = "Input for creating a new journal entry draft (kassekladde)";

        Field(x => x.CompanyId).Description("Company ID (required)");
        Field(x => x.Name).Description("Draft name, e.g. 'Januar udgifter' (required)");
    }
}

public class CreateJournalEntryDraftInput
{
    public string CompanyId { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
}

public class UpdateJournalEntryDraftInputType : InputObjectGraphType<UpdateJournalEntryDraftInput>
{
    public UpdateJournalEntryDraftInputType()
    {
        Name = "UpdateJournalEntryDraftInput";
        Description = "Input for updating a journal entry draft (auto-save)";

        Field(x => x.Id).Description("Draft ID (required)");
        Field(x => x.Name, nullable: true).Description("Draft name");
        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<NonNullGraphType<ListGraphType<NonNullGraphType<DraftLineInputType>>>>("lines")
            .Description("The posting lines (posteringslinjer)");
        Field<ListGraphType<NonNullGraphType<StringGraphType>>>("attachmentIds")
            .Description("References to attached documents (bilag) - required by Bogføringsloven § 6");
    }
}

public class UpdateJournalEntryDraftInput
{
    public string Id { get; set; } = string.Empty;
    public string? Name { get; set; }
    /// <summary>
    /// Bilagsdato - the date of the transaction/document (e.g., invoice date)
    /// </summary>
    public DateOnly? DocumentDate { get; set; }
    public string? Description { get; set; }
    public string? FiscalYearId { get; set; }
    public List<DraftLineInput> Lines { get; set; } = [];
    /// <summary>
    /// References to attached documents (bilag) - required by Bogføringsloven § 6.
    /// </summary>
    public List<string>? AttachmentIds { get; set; }
}

public class DraftLineInputType : InputObjectGraphType<DraftLineInput>
{
    public DraftLineInputType()
    {
        Name = "DraftLineInput";
        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 (may be null while editing)");
        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");
    }
}

public class DraftLineInput
{
    public int LineNumber { get; set; }
    public string? AccountId { get; set; }
    public decimal DebitAmount { get; set; }
    public decimal CreditAmount { get; set; }
    public string? Description { get; set; }
    public string? VatCode { get; set; }
}
