using System;

namespace LFP_Manager.DataStructure
{
    internal static class DbConstData
    {
        // ============================== //
        //  DB 정의 (SQLite)
        // ============================== //
        public static class Database
        {
            // 오타 수정: AlamrHistory → AlarmHistory
            public const string FileName = @"\db\AlarmHistory.db";
            public const string TableName = "TAlarmHistory";

            // NOTE:
            // - SQLite 권장 타입(TEXT/INTEGER/REAL) 사용
            // - 마지막 콤마 제거
            // - IF NOT EXISTS 추가
            // - 자동 증가 PK(Id) 추가
            public static readonly string CreateTable = $@"
CREATE TABLE IF NOT EXISTS {TableName} (
  Id            INTEGER PRIMARY KEY AUTOINCREMENT,
  create_date       TEXT    NOT NULL,
  alarm_code        INTEGER,
  alarm_status      INTEGER,
  alarm_cname       TEXT,
  alarm_sname       TEXT,
  module_no         INTEGER,
  ct_no             INTEGER,
  alarm_param_1     REAL,
  alarm_param_2     REAL,
  alarm_param_3     REAL
);";

            public static readonly string CreateIndexes = $@"
CREATE INDEX IF NOT EXISTS IX_{TableName}_create_date              ON {TableName}(create_date);
CREATE INDEX IF NOT EXISTS IX_{TableName}_create_date_alarm_code   ON {TableName}(create_date, alarm_code);
CREATE INDEX IF NOT EXISTS IX_{TableName}_create_date_module_no    ON {TableName}(create_date, module_no);
-- 선택: 코드/상태/모듈 단독 인덱스(필요할 때만)
-- CREATE INDEX IF NOT EXISTS IX_{TableName}_alarm_code           ON {TableName}(alarm_code);
-- CREATE INDEX IF NOT EXISTS IX_{TableName}_alarm_status         ON {TableName}(alarm_status);
-- CREATE INDEX IF NOT EXISTS IX_{TableName}_module_no            ON {TableName}(module_no);
";
        }

        // ============================== //
        //  Alarm 코드 정의 (enum)
        //  - 추가하신 15~18 포함
        //  - 기존 숫자값 유지 (호환성)
        // ============================== //
        public enum Alarm
        {
            CELL_UNDER_VOLTAGE = 0,
            CELL_OVER_VOLTAGE = 1,
            UNDER_VOLTAGE = 2,
            OVER_VOLTAGE = 3,  // 오타 수정: VOLTAGAE → VOLTAGE
            CHARGE_OVER_TEMPERATURE = 4,
            DISCHARGE_OVER_TEMPERATURE = 5,
            CHARGE_LOW_TEMPERATURE = 6,
            DISCHARGE_LOW_TEMPERATURE = 7,
            CHARGE_OVER_CURRENT = 8,
            DISCHARGE_OVER_CURRENT = 9,
            LOW_SOC = 10,
            LOW_SOH = 11,
            CELL_VOLTAGE_DIFF = 12,
            SHORT_CIRCUIT = 13,
            COMM_FAIL = 14,
            MOSFET_OVER_TERMPERATURE = 15,

            CB_OFF = 16,

            VOLTAGE_MEASUREMENT_ERROR = 17, // 신규
            TEMPERATURE_MEASUREMENT_ERROR = 18, // 신규
            BATTERY_CELL_UNBALANCE = 19, // 신규
            EMERGENCY_ALARM = 20, // 신규

            STS_STAND_BY = 21,
            STS_CHARGING = 22, // 오타 수정: CHARING → CHARGING
            STS_DISCHARGING = 23,
            STS_PROTECTED = 24,
            STS_PRE_CHARGING = 25,
            STS_UNKNOWN = 30,
            START = 31,
            STOP = 32,
        }

        // index = 코드
        private static readonly string[] AlarmNames =
        {
            /*  0 */ "CELL UNDER VOLTAGE",
            /*  1 */ "CELL OVER VOLTAGE",
            /*  2 */ "UNDER VOLTAGE",
            /*  3 */ "OVER VOLTAGE",
            /*  4 */ "CHARGE OVER TEMPERATURE",
            /*  5 */ "DISCHARGE OVER TEMPERATURE",
            /*  6 */ "CHARGE LOW TEMPERATURE",
            /*  7 */ "DISCHARGE LOW TEMPERATURE",
            /*  8 */ "CHARGE OVER CURRENT",
            /*  9 */ "DISCHARGE OVER CURRENT",
            /* 10 */ "LOW SOC",
            /* 11 */ "LOW SOH",
            /* 12 */ "CELL VOLTAGE DIFFERENCE",
            /* 13 */ "SHORT CIRCUIT",
            /* 14 */ "COMM FAIL",
            /* 15 */ "MOSFET OVER TEMPERATURE",
            /* 16 */ "CIRCUIT BREAKER OFF",
            /* 17 */ "VOLTAGE MEASUREMENT ERROR",
            /* 18 */ "TEMPERATURE MEASUREMENT ERROR",
            /* 19 */ "BATTERY CELL UNBALANCE",
            /* 20 */ "EMERGENCY ALARM",
            /* 21 */ "STAND BY",
            /* 22 */ "CHARGING",
            /* 23 */ "DISCHARGING",
            /* 24 */ "PROTECTED",
            /* 25 */ "PRE-CHARGING",
            /* 26 */ "RESERVED",
            /* 27 */ "RESERVED",
            /* 28 */ "RESERVED",
            /* 29 */ "RESERVED",
            /* 30 */ "UNKNOWN",
            /* 31 */ "START",
            /* 32 */ "STOP",
        };

        // 기존 상수 ALARM_NAME_SIZE는 실제 길이와 어긋나므로 제거 권장.
        // 필요하시면 아래 프로퍼티로 대체하세요.
        public static int AlarmNameCount => AlarmNames.Length;

        public static string GetAlarmName(int code)
            => (code >= 0 && code < AlarmNames.Length) ? AlarmNames[code] : $"UNKNOWN({code})";

        public static string GetAlarmName(Alarm code) => GetAlarmName((int)code);

        // ============================== //
        //  Flag 정의 (enum)
        // ============================== //
        public enum Flag
        {
            RELEASE = 0,
            WARNING = 1,
            PROTECT = 2,
            ERROR = 3,
            STS_CHANGED = 4,
            INFORMATION = 5,
        }

        private static readonly string[] FlagNames =
        {
            "RELEASE",
            "WARNING",
            "PROTECT",
            "ERROR",
            "STATUS CHANGED",
            "INFORMATION",
        };

        public static int FlagNameCount => FlagNames.Length;

        public static string GetFlagName(int code)
            => (code >= 0 && code < FlagNames.Length) ? FlagNames[code] : $"UNKNOWN({code})";

        public static string GetFlagName(Flag code) => GetFlagName((int)code);
    }
}
