using SQLite;
using System.Text.Json.Serialization;

namespace salesbook.Shared.Core.Entity;

[Table("vtb_dest")]
public class VtbDest
{
    [PrimaryKey, Column("composite_key")] 
    public string CompositeKey { get; set; }

    private string? _codAnag;

    [Column("cod_anag"), JsonPropertyName("codAnag"), Indexed(Name = "VtbDestPK", Order = 1, Unique = true)]
    public string? CodAnag
    {
        get => _codAnag;
        set
        {
            _codAnag = value;
            if (_codAnag != null && _codVdes != null)
                UpdateCompositeKey();
        }
    }

    private string? _codVdes;

    [Column("cod_vdes"), JsonPropertyName("codVdes"), Indexed(Name = "VtbDestPK", Order = 2, Unique = true)]
    public string? CodVdes
    {
        get => _codVdes;
        set
        {
            _codVdes = value;
            if (_codAnag != null && _codVdes != null)
                UpdateCompositeKey();
        }
    }

    [Column("destinatario"), JsonPropertyName("destinatario")]
    public string Destinatario { get; set; }

    [Column("indirizzo"), JsonPropertyName("indirizzo")]
    public string Indirizzo { get; set; }

    [Column("cap"), JsonPropertyName("cap")]
    public string Cap { get; set; }

    [Column("citta"), JsonPropertyName("citta")]
    public string Citta { get; set; }

    [Column("prov"), JsonPropertyName("prov")]
    public string Prov { get; set; }

    [Column("nazione"), JsonPropertyName("nazione")]
    public string Nazione { get; set; }

    [Column("tel"), JsonPropertyName("tel")]
    public string Tel { get; set; }

    [Column("fax"), JsonPropertyName("fax")]
    public string Fax { get; set; }

    [Column("note"), JsonPropertyName("note")]
    public string Note { get; set; }

    [Column("fonte"), JsonPropertyName("fonte")]
    public string Fonte { get; set; }

    [Column("cod_centro_azi"), JsonPropertyName("codCentroAzi")]
    public string CodCentroAzi { get; set; }

    [Column("gg_cons"), JsonPropertyName("ggCons")]
    public int GgCons { get; set; } = 0;

    [Column("cod_aliq_out"), JsonPropertyName("codAliqOut")]
    public string CodAliqOut { get; set; }

    [Column("cod_aliq_in"), JsonPropertyName("codAliqIn")]
    public string CodAliqIn { get; set; }

    [Column("descriz_aliq_out"), JsonPropertyName("descrizAliqOut")]
    public string DescrizAliqOut { get; set; }

    [Column("cod_vzon"), JsonPropertyName("codVzon")]
    public string CodVzon { get; set; }

    [Column("cod_vlis"), JsonPropertyName("codVlis")]
    public string CodVlis { get; set; }

    [Column("cod_vage"), JsonPropertyName("codVage")]
    public string CodVage { get; set; }

    [Column("persona_rif"), JsonPropertyName("personaRif")]
    public string PersonaRif { get; set; }

    [Column("part_iva"), JsonPropertyName("partIva")]
    public string PartIva { get; set; }

    [Column("cod_affiliazione"), JsonPropertyName("codAffiliazione")]
    public string CodAffiliazione { get; set; }

    [Column("indirizzo_legale"), JsonPropertyName("indirizzoLegale")]
    public string IndirizzoLegale { get; set; }

    [Column("cap_legale"), JsonPropertyName("capLegale")]
    public string CapLegale { get; set; }

    [Column("citta_legale"), JsonPropertyName("cittaLegale")]
    public string CittaLegale { get; set; }

    [Column("prov_legale"), JsonPropertyName("provLegale")]
    public string ProvLegale { get; set; }

    [Column("nazione_legale"), JsonPropertyName("nazioneLegale")]
    public string NazioneLegale { get; set; }

    [Column("cod_mdep"), JsonPropertyName("codMdep")]
    public string CodMdep { get; set; }

    [Column("flag_domic_riba"), JsonPropertyName("flagDomicRiba")]
    public string FlagDomicRiba { get; set; }

    [Column("flag_attivo"), JsonPropertyName("flagAttivo")]
    public string FlagAttivo { get; set; } = "S";

    [Column("flag_esponi"), JsonPropertyName("flagEsponi")]
    public string FlagEsponi { get; set; } = "S";

    [Column("rag_soc_legale"), JsonPropertyName("ragSocLegale")]
    public string RagSocLegale { get; set; }

    [Column("cod_alis"), JsonPropertyName("codAlis")]
    public string CodAlis { get; set; }

    [Column("cod_vpre"), JsonPropertyName("codVpre")]
    public string CodVpre { get; set; }

    [Column("cod_vcom"), JsonPropertyName("codVcom")]
    public string CodVcom { get; set; }

    [Column("cod_sco_cli"), JsonPropertyName("codScoCli")]
    public string CodScoCli { get; set; }

    [Column("e_mail"), JsonPropertyName("eMail")]
    public string EMail { get; set; }

    [Column("data_cessazione"), JsonPropertyName("dataCessazione")]
    public DateTime? DataCessazione { get; set; }

    [Column("data_attivazione"), JsonPropertyName("dataAttivazione")]
    public DateTime? DataAttivazione { get; set; }

    [Column("cod_vvet"), JsonPropertyName("codVvet")]
    public string CodVvet { get; set; }

    [Column("gg_chiusura"), JsonPropertyName("ggChiusura")]
    public string GgChiusura { get; set; }

    [Column("tipo_negozio"), JsonPropertyName("tipoNegozio")]
    public string TipoNegozio { get; set; }

    [Column("cod_ean"), JsonPropertyName("codEan")]
    public string CodEan { get; set; }

    [Column("flag_stampa_prezzi"), JsonPropertyName("flagStampaPrezzi")]
    public string FlagStampaPrezzi { get; set; } = "S";

    [Column("cod_aliq"), JsonPropertyName("codAliq")]
    public string CodAliq { get; set; }

    [Column("cod_griglia"), JsonPropertyName("codGriglia")]
    public string CodGriglia { get; set; }

    [Column("cod_acc"), JsonPropertyName("codAcc")]
    public string CodAcc { get; set; }

    [Column("cod_vtip"), JsonPropertyName("codVtip")]
    public string CodVtip { get; set; }

    [Column("cod_vset"), JsonPropertyName("codVset")]
    public string CodVset { get; set; }

    [Column("cod_vseg"), JsonPropertyName("codVseg")]
    public string CodVseg { get; set; }

    [Column("cod_vatt"), JsonPropertyName("codVatt")]
    public string CodVatt { get; set; }

    [Column("cod_fisc"), JsonPropertyName("codFisc")]
    public string CodFisc { get; set; }

    [Column("cuu_pa"), JsonPropertyName("cuuPa")]
    public string CuuPa { get; set; }

    [Column("e_mail_pec"), JsonPropertyName("eMailPec")]
    public string EMailPec { get; set; }

    [Column("flag_stabile_org"), JsonPropertyName("flagStabileOrg")]
    public string FlagStabileOrg { get; set; }

    [Column("lat"), JsonPropertyName("lat")]
    public decimal? Lat { get; set; }

    [Column("lng"), JsonPropertyName("lng")]
    public decimal? Lng { get; set; }

    [Column("term_cons"), JsonPropertyName("termCons")]
    public string TermCons { get; set; }

    [Column("itinerario"), JsonPropertyName("itinerario")]
    public string Itinerario { get; set; }

    [Column("imp_min_ord"), JsonPropertyName("impMinOrd")]
    public decimal ImpMinOrd { get; set; } = 0;

    [Column("part_iva_legale"), JsonPropertyName("partIvaLegale")]
    public string PartIvaLegale { get; set; }

    [Column("cod_fisc_legale"), JsonPropertyName("codFiscLegale")]
    public string CodFiscLegale { get; set; }

    private void UpdateCompositeKey() =>
        CompositeKey = $"{CodAnag}::{CodVdes}";
}