using SQLite;
using System.ComponentModel;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;

namespace LSFE.MobApp.Entities.Location
{
    [Table("Institutions")]
    public class Institution : SyncEntity
    {
        private int _institutionId;
        private string? _institutionName;
        private int _territoryId;
        private bool _isActive = true;
        private DateTime _createdDate = DateTime.UtcNow;
        private ObservableCollection<Doctor.Doctor> _doctors = new();
        private bool _isExpanded = false;

        [PrimaryKey]
        public Guid Id { get; set; } = Guid.NewGuid();

        [Column("InstitutionId"), NotNull]
        public int InstitutionId
        {
            get => _institutionId;
            set
            {
                _institutionId = value;
                OnPropertyChanged();
            }
        }

        [Column("InstitutionName")]
        public string? InstitutionName
        {
            get => _institutionName;
            set
            {
                _institutionName = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(Name));
            }
        }

        [Column("TerritoryId"), NotNull]
        public int TerritoryId
        {
            get => _territoryId;
            set
            {
                _territoryId = value;
                OnPropertyChanged();
            }
        }

        [Column("IsActive"), NotNull]
        public bool IsActive
        {
            get => _isActive;
            set
            {
                _isActive = value;
                OnPropertyChanged();
            }
        }

        [Column("CreatedDate"), NotNull]
        public DateTime CreatedDate
        {
            get => _createdDate;
            set
            {
                _createdDate = value;
                OnPropertyChanged();
            }
        }

        // Expand/collapse property - NOT stored in database
        [Ignore]
        public bool IsExpanded
        {
            get => _isExpanded;
            set
            {
                if (_isExpanded != value)
                {
                    _isExpanded = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(ArrowIcon));
                }
            }
        }

        // Computed property for arrow direction
        [Ignore]
        public string ArrowIcon => IsExpanded ? "▲" : "▼";

        // For backward compatibility
        [Ignore]
        public string? Name
        {
            get => InstitutionName;
            set => InstitutionName = value;
        }

        [Ignore]
        public ObservableCollection<Entities.Doctor.Doctor> Doctors
        {
            get => _doctors;
            set
            {
                _doctors = value;
                OnPropertyChanged();
            }
        }
    }
}