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
        /// <summary>
        /// Represents a Redis server endpoint, typically used to encapsulate connection details such as host, port, and credentials required to connect to a Redis instance.
        /// </summary>
        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:
        
        /// <summary>
        /// Provides static cache key constants used throughout the application to ensure consistent key naming when storing and retrieving cached data.
        /// </summary>
        public static class CacheKeys
        {
            // ----------------- Patients -----------------
            /// <summary>
            /// Builds a formatted patient identifier by prefixing the provided patient ID with "patients:".
            /// </summary>
            /// <param name="patientId">The unique identifier of the patient to be included in the formatted string.</param>
            /// <returns>A string in the format "patients:{patientId}" representing the patient resource key.</returns>
            public static string Patient(string patientId)
                            => $"patients:{patientId}";

            // ----------------- ConfigDisplays -----------------
            /// <summary>
            /// Builds a configuration key for a config display by combining the <c>configDisplays</c> namespace with the specified identifier.
            /// </summary>
            /// <param name="id">The unique identifier of the config display to include in the key.</param>
            /// <returns>A formatted key string in the form <c>configDisplays:{id}</c> suitable for lookup or caching.</returns>
            public static string ConfigDisplay(string id)
                            => $"configDisplays:{id}";

            /// <summary>
            /// Returns the configuration key representing the "all displays" setting, typically used to request or identify the display configuration for all available displays.
            /// </summary>
            /// <returns>The string identifier "configDisplays:all" used to reference the all-displays configuration.</returns>
            public static string ConfigDisplaysAll()
                            => "configDisplays:all";

            // ----------------- PumpObservations -----------------
            /// <summary>
            /// Formats the specified observation identifier as a pump observation reference string by prefixing it with "pumpObs:".
            /// </summary>
            /// <param name="obsId">The observation identifier to include in the formatted result.</param>
            /// <returns>A formatted string in the form "pumpObs:{obsId}".</returns>
            public static string PumpObservation(string obsId)
                            => $"pumpObs:{obsId}";

            /// <summary>
            /// Builds a daily pump observation identifier by composing a prefixed key with the pump identifier and the date in <c>yyyyMMdd</c> format.
            /// </summary>
            /// <param name="pumpId">The unique identifier of the pump used in the composed key.</param>
            /// <param name="date">The observation date whose year, month, and day are formatted into the key.</param>
            /// <returns>A string in the form <c>pumpObs:pump:{pumpId}:{yyyyMMdd}</c> representing the daily pump observation key.</returns>
            public static string PumpDaily(string pumpId, DateTime date)
                            => $"pumpObs:pump:{pumpId}:{date:yyyyMMdd}";

            // ----------------- Appointments -----------------
            /// <summary>
            /// Builds an appointment resource identifier by combining the "appointments" prefix with the specified appointment ID.
            /// </summary>
            /// <param name="apptId">The unique identifier of the appointment.</param>
            /// <returns>A formatted string in the form "appointments:{apptId}".</returns>
            public static string Appointment(string apptId)
                            => $"appointments:{apptId}";

            /// <summary>
            /// Builds a cache key for retrieving a patient's appointments for a specific calendar day.
            /// The key is composed of the patient identifier and the date formatted as <c>yyyyMMdd</c>.
            /// </summary>
            /// <param name="patientId">The unique identifier of the patient whose appointments are being addressed.</param>
            /// <param name="date">The date for which the appointments are being requested.</param>
            /// <returns>A formatted cache key string combining the patient identifier and the day in <c>yyyyMMdd</c> format.</returns>
            public static string AppointmentsByPatientDay(string patientId, DateTime date)
                            => $"appointments:patient:{patientId}:{date:yyyyMMdd}";

            /// <summary>
            /// Builds a deterministic cache key used to look up a patient's appointments for the month of the supplied date.
            /// The key is composed of the patient identifier and the date formatted as <c>yyyyMM</c>.
            /// </summary>
            /// <param name="patientId">The identifier of the patient whose appointments are being queried.</param>
            /// <param name="date">The date whose year and month determine the appointment period encoded in the key.</param>
            /// <returns>A formatted string in the form <c>appointments:patient:{patientId}:{yyyyMM}</c> suitable for use as a cache key.</returns>
            public static string AppointmentsByPatientMonth(string patientId, DateTime date)
                            => $"appointments:patient:{patientId}:{date:yyyyMM}";

            // ----------------- GroupedObservations -----------------
            /// <summary>
            /// Builds a formatted identifier string for grouped observations associated with a specific patient, typically used as a cache key or lookup token.
            /// </summary>
            /// <param name="patientId">The unique identifier of the patient whose observations are being referenced.</param>
            /// <param name="groupedField">The optional field used to group the observations; may be null.</param>
            /// <returns>A formatted string in the pattern "groupedObs:{groupedField}:patient:{patientId}".</returns>
            public static string GroupedObservationsByPatient(string patientId, string? groupedField)
                            => $"groupedObs:{groupedField}:patient:{patientId}";

            /// <summary>
            /// Generates a formatted cache key for grouped observations scoped to a specific patient and day.
            /// </summary>
            /// <param name="patientId">The unique identifier of the patient.</param>
            /// <param name="groupedField">The field name used to group the observations.</param>
            /// <param name="date">The observation date, formatted as <c>yyyyMMdd</c> in the generated key.</param>
            /// <returns>A cache key string in the format <c>groupedObs:{groupedField}:patient:{patientId}:{yyyyMMdd}</c>.</returns>
            public static string GroupedObservationsByPatientDay(string patientId, string groupedField, DateTime date)
                            => $"groupedObs:{groupedField}:patient:{patientId}:{date:yyyyMMdd}";

            /// <summary>
            /// Builds a cache key for grouped observations scoped to a specific patient and month, combining the grouping field, patient identifier, and the year-month portion of the provided date.
            /// </summary>
            /// <param name="patientId">The identifier of the patient whose observations are being grouped.</param>
            /// <param name="groupedField">The observation field used as the grouping criterion.</param>
            /// <param name="date">The date used to derive the month bucket (formatted as yyyyMM) for the cache key.</param>
            /// <returns>A formatted cache key string in the form <c>groupedObs:{groupedField}:patient:{patientId}:{yyyyMM}</c>.</returns>
            public static string GroupedObservationsByPatientMonth(string patientId, string groupedField, DateTime date)
                            => $"groupedObs:{groupedField}:patient:{patientId}:{date:yyyyMM}";

            /// <summary>
            /// Generates a cache key for retrieving the most recent grouped observations for a specific patient.
            /// </summary>
            /// <param name="patientId">The unique identifier of the patient whose observations are being queried.</param>
            /// <param name="groupedField">The field used to group the observations.</param>
            /// <param name="take">The maximum number of latest observations to include in the cache key.</param>
            /// <returns>A formatted cache key string identifying the patient's latest grouped observations.</returns>
            public static string GroupedObservationsLatest(string patientId, string groupedField, int take)
                            => $"groupedObs:{groupedField}:patient:{patientId}:latest:{take}";
        }
    }
}