namespace Books.Api.Domain.Invoices;

/// <summary>
/// Type of invoice document. Credit notes are accounting-wise just invoices with negative amounts.
/// </summary>
public enum InvoiceType
{
    /// <summary>
    /// Regular invoice with positive amounts.
    /// Debits the customer's sub-ledger account when sent.
    /// </summary>
    Invoice,

    /// <summary>
    /// Credit note (kreditnota) - an invoice with negative amounts.
    /// Credits the customer's sub-ledger account when issued.
    /// Can reference an original invoice.
    /// </summary>
    CreditNote
}

public static class InvoiceTypeExtensions
{
    public static string ToStringValue(this InvoiceType type) => type switch
    {
        InvoiceType.Invoice => "invoice",
        InvoiceType.CreditNote => "credit_note",
        _ => throw new ArgumentOutOfRangeException(nameof(type))
    };

    public static InvoiceType FromString(string value) => value.ToLowerInvariant() switch
    {
        "invoice" => InvoiceType.Invoice,
        "credit_note" or "creditnote" => InvoiceType.CreditNote,
        _ => throw new ArgumentException($"Unknown invoice type: {value}", nameof(value))
    };

    /// <summary>
    /// Returns the document title in Danish for PDF generation.
    /// </summary>
    public static string GetDocumentTitleDanish(this InvoiceType type) => type switch
    {
        InvoiceType.Invoice => "FAKTURA",
        InvoiceType.CreditNote => "KREDITNOTA",
        _ => throw new ArgumentOutOfRangeException(nameof(type))
    };

    /// <summary>
    /// Returns the document title in English.
    /// </summary>
    public static string GetDocumentTitleEnglish(this InvoiceType type) => type switch
    {
        InvoiceType.Invoice => "INVOICE",
        InvoiceType.CreditNote => "CREDIT NOTE",
        _ => throw new ArgumentOutOfRangeException(nameof(type))
    };
}
