using adas_core.Domain.Enums;
using adas_core.Domain.Models.Masters;
using MongoDB.Bson;

namespace adas_core.Domain.Models.MongoModels;

/// <summary>
/// Represents a patient entity, typically used to encapsulate patient-related data and behavior within a healthcare or medical information system.
/// </summary>
public class Patient
{
    public ObjectId Id { get; set; }

    public string? PatientNumber { get; set; }

    public string? PatientId { get; set; }

    public DateTime? AdmTime { get; set; }

    //Estado del Alta: Altable, no Altable, Exitus, Transferible
    public OptionList? DischargeStatus { get; set; }

    public OptionList? Altable { get; set; }

    //Procedencia
    public OptionList? Origin { get; set; }

    public string? OriginAux { get; set; }

    //Diagnósticos
    public OptionList? Diagnosis { get; set; }

    public string? DiagnosisAux { get; set; }

    //Visitas
    public OptionList? Visits { get; set; }

    public OptionList? AccessControl { get; set; }

    //Aislamiento: por defecto No Aislado
    public OptionList? Insulation { get; set; }

    //NEMS
    public OptionList? PatientStatus { get; set; }

    //Movilidad: por defecto sin asignar
    public OptionList? Mobility { get; set; }

    //Techo terapeutico
    public OptionList? TherapeuticCeiling { get; set; }

    // Intervenciones
    public List<OptionList>? Procedures { get; set; }

    //Pruebas (tests)
    public List<OptionList>? Tests { get; set; }

    //Terapias
    public List<OptionList>? Treatment { get; set; }

    //Facultativos
    public List<OptionList>? Doctors { get; set; }

    //Alergias
    public List<OptionList>? Allergies { get; set; }

    public List<OptionList>? LanguageBarrier { get; set; }
    public OptionList? PassiveSitting { get; set; }

    public DateTime? DisTime { get; set; }

    public Person? Person { get; set; }

    public Person? AttendingDoctor { get; set; }

    public DateTime? LastObservationDate { get; set; }

    public DateTime? CreationDate { get; set; }

    public DateTime? UpdateDate { get; set; }

    public DateTime? ArchiveDate { get; set; }

    public ObjectId? PointOfCareId { get; set; }

    public ObjectId? UnitId { get; set; }
    
    public List<HistoricalLocation>? HistoricalLocations { get; set; }

    /// <summary>
    /// Returns a human-readable string representation of the person, including the identifier and any
    /// available contextual details such as point of care, bed, patient numbers, admission and discharge
    /// timestamps, assigned patient, attending doctor, and relevant date metadata. Optional fields are
    /// appended to the output only when their values are present (non-null, non-empty, or with a value
    /// for nullable types).
    /// </summary>
    /// <returns>A formatted string prefixed with "person[" and containing the included fields joined by commas.</returns>
    public override string ToString()
        {
            List<string> items = [$"id: {Id}"];
            if (!string.IsNullOrEmpty(UnitString)) items.Add($"pointOfCare: {UnitString}");
            if (!string.IsNullOrEmpty(Bed)) items.Add($"bed: {Bed}");
            if (!string.IsNullOrEmpty(PatientNumber)) items.Add($"patientNumber: {PatientNumber}");
            if (!string.IsNullOrEmpty(PatientId)) items.Add($"patientId: {PatientId}");
            if (AdmTime.HasValue) items.Add($"admTime: {AdmTime}");
            if (DisTime.HasValue) items.Add($"disTime: {DisTime}");
            if (Person != null) items.Add($"patient: {Person}");
            if (AttendingDoctor != null) items.Add($"attendingDoctor: {AttendingDoctor}");
            if (LastObservationDate.HasValue) items.Add($"lastObservationDate: {LastObservationDate}");
            if (CreationDate.HasValue) items.Add($"creationDate: {CreationDate}");
            if (UpdateDate.HasValue) items.Add($"updateDate: {UpdateDate}");
            if (ArchiveDate.HasValue) items.Add($"archiveDate: {UpdateDate}");
            return "person[" + string.Join(", ", items) + "]";
        }

    /// <summary>
    /// Determines whether the current <see cref="UnitString"/> represents an inactive Virtual Point of Care by checking that it is not null and is not defined as a value of the <see cref="VirtualPointOfCare"/> enumeration.
    /// </summary>
    /// <returns><c>true</c> if <c>UnitString</c> is not null and does not match any defined <see cref="VirtualPointOfCare"/> value; otherwise, <c>false</c>.</returns>
    public bool IsInActivePoC()
        {
            return UnitString != null && !Enum.IsDefined(typeof(VirtualPointOfCare), UnitString);
        }

    /// <summary>
    /// Creates a copy of the current <see cref="Patient"/> instance by performing a shallow copy.
    /// </summary>
    /// <returns>A new <see cref="Patient"/> object that is a shallow copy of the current instance.</returns>
    public Patient DeepCopy()
        {
            return (Patient)MemberwiseClone();
        }

    #region UnMap

    public string? Bed { get; set; }
    public string? Room { get; set; }
    public string? UnitString { get; set; }
    public PointOfCare? PointOfCare { get; set; }

    public PatientLocation Location
    {
        get => new(UnitString, Bed, Room ?? Bed);
        set
        {
            if (value == null || (UnitString == value.UnitName && Bed == value.Bed)) 
                return;

            UnitString = value.UnitName;
            Bed = value.Bed;
            HistoricalLocations ??= [];

            var currentUtc = DateTime.UtcNow;

            bool shouldAdd = true;
            if (HistoricalLocations.Count > 0)
            {
                var last = HistoricalLocations[^1];
                if (last.PatientLocation?.Bed == value.Bed && last.PatientLocation?.UnitName == value.UnitName)
                {
                    shouldAdd = false;
                }
                else if (currentUtc.ToString("o").Equals(last.AdmTime.ToString()))
                {
                    currentUtc = currentUtc.AddSeconds(1);
                }
            }

            if (shouldAdd)
            {
                HistoricalLocations.Add(new HistoricalLocation(
                    currentUtc, 
                    new PatientLocation(value.UnitName, value.Bed, value.Room)
                ));
            }
        }
    }

    #endregion
}

/// <summary>
/// Represents data related to a patient's income information.
/// </summary>
public class PatientIncomeData
{
    public DateTime? AdmTime { get; set; }
    public OptionList? Origin { get; set; }
    public string? OriginAux { get; set; }
    public OptionList? Diagnosis { get; set; }
    public string? DiagnosisAux { get; set; }
}