namespace adas_core.Domain.Models.AppSettings
{
    /// <summary>
    /// Configuración Redis.
    /// No contiene lógica de qué entidades usan Redis (eso está en CacheSettings).
    /// Esta clase solo define:
    ///  - Conexión
    ///  - Cluster/Scalability
    ///  - SSL/password
    ///  - TTL global y por entidad
    ///  - Generación de claves CacheKeys
    /// </summary>
    public class RedisSettings
    {
        // Conexión y networking
        public string? ConnectionString { get; set; }
        public bool EnableScalable { get; set; }
        public bool Ssl { get; set; }
        public string? Password { get; set; }
        public IEnumerable<RedisEndpoint> Endpoints { get; set; } = [];
        public string ChannelPrefix { get; set; } = "MedDisCore";

        // TTL global y TTL específico por entidad
        // (no determina si la entidad se cachea o no)
        public int? ExpireKeyInSeconds { get; set; }

        public TtlSettings Ttl { get; set; } = new();

        // Submodelos
        public class RedisEndpoint
        {
            public string Host { get; set; } = null!;
            public int Port { get; set; }
        }

        // CacheKeys: claves estandarizadas y unificadas para todas las entidades
        // Estas claves son fundamentales para:
        
        public static class CacheKeys
        {
            // ----------------- Patients -----------------
            public static string Patient(string patientId)
                => $"patients:{patientId}";

            // ----------------- ConfigDisplays -----------------
            public static string ConfigDisplay(string id)
                => $"configDisplays:{id}";

            public static string ConfigDisplaysAll()
                => "configDisplays:all";

            // ----------------- PumpObservations -----------------
            public static string PumpObservation(string obsId)
                => $"pumpObs:{obsId}";

            public static string PumpDaily(string pumpId, DateTime date)
                => $"pumpObs:pump:{pumpId}:{date:yyyyMMdd}";

            // ----------------- Appointments -----------------
            public static string Appointment(string apptId)
                => $"appointments:{apptId}";

            public static string AppointmentsByPatientDay(string patientId, DateTime date)
                => $"appointments:patient:{patientId}:{date:yyyyMMdd}";

            public static string AppointmentsByPatientMonth(string patientId, DateTime date)
                => $"appointments:patient:{patientId}:{date:yyyyMM}";

            // ----------------- GroupedObservations -----------------
            public static string GroupedObservationsByPatient(string patientId, string? groupedField)
                => $"groupedObs:{groupedField}:patient:{patientId}";

            public static string GroupedObservationsByPatientDay(string patientId, string groupedField, DateTime date)
                => $"groupedObs:{groupedField}:patient:{patientId}:{date:yyyyMMdd}";

            public static string GroupedObservationsByPatientMonth(string patientId, string groupedField, DateTime date)
                => $"groupedObs:{groupedField}:patient:{patientId}:{date:yyyyMM}";

            public static string GroupedObservationsLatest(string patientId, string groupedField, int take)
                => $"groupedObs:{groupedField}:patient:{patientId}:latest:{take}";
        }
    }
}