use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Flat invoice row for Excel export — one row per line item.
///
/// All fields are `String` or `f64` for easy tabular output.
/// Complex nested structs (podmiot3, adnotacje, platnosc, rozliczenie,
/// warunki_transakcji, zamowienie, zalacznik, stopka, correction_data) are skipped.
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct InvoiceXlsRow {
    // ====== Core fields ======
    #[serde(default)]
    pub lp: u32,
    #[serde(default, rename = "invoiceNumber")]
    pub invoice_number: String,
    #[serde(default, rename = "issueDate")]
    pub issue_date: String,

    // ====== Issuer (Podmiot1) ======
    #[serde(default, rename = "issuerName")]
    pub issuer_name: String,
    #[serde(default, rename = "issuerNip")]
    pub issuer_nip: String,
    #[serde(default, rename = "issuerAddress")]
    pub issuer_address: String,
    #[serde(default, rename = "issuerPostalCode")]
    pub issuer_postal_code: String,
    #[serde(default, rename = "issuerCity")]
    pub issuer_city: String,
    #[serde(default, rename = "issuerCountry")]
    pub issuer_country: String,

    // ====== Recipient (Podmiot2) ======
    #[serde(default, rename = "recipientName")]
    pub recipient_name: String,
    #[serde(default, rename = "recipientNip")]
    pub recipient_nip: String,
    #[serde(default, rename = "recipientAddress")]
    pub recipient_address: String,
    #[serde(default, rename = "recipientPostalCode")]
    pub recipient_postal_code: String,
    #[serde(default, rename = "recipientCity")]
    pub recipient_city: String,
    #[serde(default, rename = "recipientCountry")]
    pub recipient_country: String,

    // ====== Amounts ======
    #[serde(default, rename = "netAmount")]
    pub net_amount: f64,
    #[serde(default, rename = "taxAmount")]
    pub tax_amount: f64,
    #[serde(default, rename = "grossAmount")]
    pub gross_amount: f64,
    #[serde(default)]
    pub currency: String,

    // ====== KSeF & metadata ======
    #[serde(default)]
    pub status: String,
    #[serde(default, rename = "ksefNumber")]
    pub ksef_number: String,

    // ====== Bank ======
    #[serde(default, rename = "bankAccount")]
    pub bank_account: String,
    #[serde(default, rename = "bankName")]
    pub bank_name: String,
    #[serde(default, rename = "bankSwift")]
    pub bank_swift: String,

    // ====== Misc ======
    #[serde(default)]
    pub bdo: String,
    #[serde(default)]
    pub notes: String,

    // ====== Payment ======
    #[serde(default, rename = "paymentMethod")]
    pub payment_method: String,
    #[serde(default, rename = "paymentDate")]
    pub payment_date: String,
    #[serde(default, rename = "paidDate")]
    pub paid_date: String,

    // ====== Invoice kind / correction ======
    #[serde(default, rename = "invoiceKind")]
    pub invoice_kind: String,
    #[serde(default, rename = "correctionReason")]
    pub correction_reason: String,
    #[serde(default, rename = "correctedInvoiceNumber")]
    pub corrected_invoice_number: String,

    // ====== Billing period ======
    #[serde(default, rename = "billingPeriodFrom")]
    pub billing_period_from: String,
    #[serde(default, rename = "billingPeriodTo")]
    pub billing_period_to: String,

    // ====== Contact ======
    #[serde(default, rename = "buyerEmail")]
    pub buyer_email: String,
    #[serde(default, rename = "sellerEmail")]
    pub seller_email: String,
    #[serde(default, rename = "orderNumber")]
    pub order_number: String,
    #[serde(default, rename = "sellerPhone")]
    pub seller_phone: String,

    // ====== Bank extra ======
    #[serde(default, rename = "bankDescription")]
    pub bank_description: String,
    #[serde(default, rename = "bankOwnAccount")]
    pub bank_own_account: String,

    // ====== Dates ======
    #[serde(default, rename = "creationDate")]
    pub creation_date: String,

    // ====== Line item fields ======
    #[serde(default, rename = "itemDescription")]
    pub item_description: String,
    #[serde(default, rename = "itemUnit")]
    pub item_unit: String,
    #[serde(default, rename = "itemQuantity")]
    pub item_quantity: f64,
    #[serde(default, rename = "itemUnitPrice")]
    pub item_unit_price: f64,
    #[serde(default, rename = "itemNetAmount")]
    pub item_net_amount: f64,
    #[serde(default, rename = "itemVatRate")]
    pub item_vat_rate: String,
    #[serde(default, rename = "itemTaxAmount")]
    pub item_tax_amount: f64,
    #[serde(default, rename = "itemGrossAmount")]
    pub item_gross_amount: f64,

    // ====== New flat fields from Invoice ======
    #[serde(default, rename = "saleDate")]
    pub sale_date: String,
    #[serde(default, rename = "serviceDate")]
    pub service_date: String,
    #[serde(default, rename = "issuePlace")]
    pub issue_place: String,
    #[serde(default, rename = "formCode")]
    pub form_code: String,
    #[serde(default, rename = "systemInfo")]
    pub system_info: String,

    // Issuer extra
    #[serde(default, rename = "issuerKrs")]
    pub issuer_krs: String,
    #[serde(default, rename = "issuerRegon")]
    pub issuer_regon: String,
    #[serde(default, rename = "issuerTaxPrefix")]
    pub issuer_tax_prefix: String,
    #[serde(default, rename = "issuerNrEori")]
    pub issuer_nr_eori: String,

    // Recipient extra
    #[serde(default, rename = "recipientKrs")]
    pub recipient_krs: String,
    #[serde(default, rename = "recipientRegon")]
    pub recipient_regon: String,
    #[serde(default, rename = "recipientNrEori")]
    pub recipient_nr_eori: String,
    #[serde(default, rename = "recipientKodUe")]
    pub recipient_kod_ue: String,
    #[serde(default, rename = "recipientNrVatUe")]
    pub recipient_nr_vat_ue: String,

    // Buyer extra
    #[serde(default, rename = "buyerPhone")]
    pub buyer_phone: String,
    #[serde(default, rename = "buyerClientNumber")]
    pub buyer_client_number: String,

    // Exchange & discount
    #[serde(default, rename = "exchangeRate")]
    pub exchange_rate: String,
    #[serde(default, rename = "discountAmount")]
    pub discount_amount: f64,

    // Correction extra
    #[serde(default, rename = "correctionType")]
    pub correction_type: String,
    #[serde(default, rename = "correctedInvoiceDate")]
    pub corrected_invoice_date: String,
    #[serde(default, rename = "correctedKsefNumber")]
    pub corrected_ksef_number: String,

    // Footer / settlement / delivery
    #[serde(default, rename = "footerText")]
    pub footer_text: String,
    #[serde(default)]
    pub settlement: String,
    #[serde(default, rename = "deliveryTerms")]
    pub delivery_terms: String,

    // P_13_x fields
    #[serde(default, rename = "p131")]
    pub p_13_1: f64,
    #[serde(default, rename = "p132")]
    pub p_13_2: f64,
    #[serde(default, rename = "p133")]
    pub p_13_3: f64,
    #[serde(default, rename = "p134")]
    pub p_13_4: f64,
    #[serde(default, rename = "p135")]
    pub p_13_5: f64,
    #[serde(default, rename = "p1361")]
    pub p_13_6_1: f64,
    #[serde(default, rename = "p1362")]
    pub p_13_6_2: f64,
    #[serde(default, rename = "p1363")]
    pub p_13_6_3: f64,
    #[serde(default, rename = "p137")]
    pub p_13_7: f64,
    #[serde(default, rename = "p138")]
    pub p_13_8: f64,
    #[serde(default, rename = "p139")]
    pub p_13_9: f64,
    #[serde(default, rename = "p1310")]
    pub p_13_10: f64,
    #[serde(default, rename = "p1311")]
    pub p_13_11: f64,

    // P_14_x fields
    #[serde(default, rename = "p141")]
    pub p_14_1: f64,
    #[serde(default, rename = "p141w")]
    pub p_14_1w: f64,
    #[serde(default, rename = "p142")]
    pub p_14_2: f64,
    #[serde(default, rename = "p142w")]
    pub p_14_2w: f64,
    #[serde(default, rename = "p143")]
    pub p_14_3: f64,
    #[serde(default, rename = "p143w")]
    pub p_14_3w: f64,
    #[serde(default, rename = "p144")]
    pub p_14_4: f64,
    #[serde(default, rename = "p144w")]
    pub p_14_4w: f64,
    #[serde(default, rename = "p145")]
    pub p_14_5: f64,

    // Flags
    #[serde(default, rename = "rodzajFaktury")]
    pub rodzaj_faktury: String,
    #[serde(default)]
    pub fp: String,
    #[serde(default)]
    pub tp: String,
    #[serde(default, rename = "zwrotAkcyzy")]
    pub zwrot_akcyzy: String,

    // Bool flags as String
    #[serde(default, rename = "isSelfBilling")]
    pub is_self_billing: String,
    #[serde(default, rename = "isSimplified")]
    pub is_simplified: String,
    #[serde(default, rename = "isPaid")]
    pub is_paid: String,

    // Payment
    #[serde(default, rename = "paymentDueDate")]
    pub payment_due_date: String,

    // Processing
    #[serde(default, rename = "processingDate")]
    pub processing_date: String,
    #[serde(default, rename = "upoReference")]
    pub upo_reference: String,
    #[serde(default, rename = "qrCodeUrl")]
    pub qr_code_url: String,

    // Item-level extra
    #[serde(default, rename = "itemPkwiu")]
    pub item_pkwiu: String,
    #[serde(default, rename = "itemGtin")]
    pub item_gtin: String,
    #[serde(default, rename = "itemCn")]
    pub item_cn: String,
    #[serde(default, rename = "itemGtu")]
    pub item_gtu: String,
    #[serde(default, rename = "itemIndeks")]
    pub item_indeks: String,
    #[serde(default, rename = "itemPkob")]
    pub item_pkob: String,
    #[serde(default, rename = "itemProcedura")]
    pub item_procedura: String,

    // ====== data_source_values — MUST be last ======
    #[serde(default, rename = "dataSourceValues")]
    pub data_source_values: HashMap<String, String>,
}
