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

namespace salesbook.Shared.Core.Entity;

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

    private int? _idPersRif;

    [Column("id_pers_rif"), JsonPropertyName("idPersRif"), Indexed(Name = "VtbCliePersRifPK", Order = 1, Unique = true)]
    public int? IdPersRif
    {
        get => _idPersRif;
        set
        {
            _idPersRif = value;
            if (_idPersRif != null && _codAnag != null)
                UpdateCompositeKey();
        }
    }

    private string? _codAnag;

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

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

    [Column("mansione"), JsonPropertyName("mansione")]
    public string? Mansione { get; set; }

    [Column("telefono"), JsonPropertyName("telefono")]
    public string? Telefono { get; set; }

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

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

    [Column("num_cellulare"), JsonPropertyName("numCellulare")]
    public string? NumCellulare { get; set; }

    [Column("tipo_indirizzo"), JsonPropertyName("tipoIndirizzo")]
    public string TipoIndirizzo { get; set; }

    [Column("cod_vdes"), JsonPropertyName("codVdes")]
    public string CodVdes { get; set; }

    [Column("data_ult_agg"), JsonPropertyName("dataUltAgg")]
    public DateTime? DataUltAgg { get; set; } = DateTime.Now;

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