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

namespace salesbook.Shared.Core.Entity;

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

    private string? _activityTypeId;

    [Column("activity_type_id"), JsonPropertyName("activityTypeId"), Indexed(Name = "ActivityTypePK", Order = 1, Unique = true)]
    public string? ActivityTypeId
    {
        get => _activityTypeId;
        set
        {
            _activityTypeId = value;
            if (_activityTypeId != null && _flagTipologia != null && _userName != null)
                UpdateCompositeKey();
        }
    }

    private string? _flagTipologia;

    [Column("flag_tipologia"), JsonPropertyName("flagTipologia"), Indexed(Name = "ActivityTypePK", Order = 2, Unique = true)]
    public string? FlagTipologia
    {
        get => _flagTipologia;
        set
        {
            _flagTipologia = value;
            if (_activityTypeId != null && _flagTipologia != null && _userName != null)
                UpdateCompositeKey();
        }
    }

    private string? _userName;

    [Column("user_name"), JsonPropertyName("userName"), Indexed(Name = "ActivityTypePK", Order = 3, Unique = true)]
    public string? UserName
    {
        get => _userName;
        set
        {
            _userName = value;
            if (_activityTypeId != null && _flagTipologia != null && _userName != null)
                UpdateCompositeKey();
        }
    }

    private void UpdateCompositeKey() =>
        CompositeKey = $"{ActivityTypeId}::{FlagTipologia}::{UserName}";
}