CREATE TABLE notification_source (
    id            INT AUTO_INCREMENT PRIMARY KEY,
    name          VARCHAR(64) NOT NULL,
    api_key       VARCHAR(128) NOT NULL,
    parse_mode    VARCHAR(16) NOT NULL DEFAULT 'json',
    parse_pattern VARCHAR(512) DEFAULT NULL,
    status        TINYINT NOT NULL DEFAULT 1,
    created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_name (name),
    UNIQUE KEY uk_api_key (api_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE notification_template (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    name       VARCHAR(64) NOT NULL,
    content    TEXT NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE notification_channel (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    name       VARCHAR(32) NOT NULL,
    type       VARCHAR(32) NOT NULL,
    config     JSON NOT NULL,
    status     TINYINT NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE notification_rule (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    source_id   INT NOT NULL,
    event       VARCHAR(64) NOT NULL,
    template_id INT NOT NULL,
    conditions  JSON DEFAULT NULL,
    enabled     TINYINT NOT NULL DEFAULT 1,
    created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_source_event (source_id, event),
    FOREIGN KEY (source_id)   REFERENCES notification_source(id),
    FOREIGN KEY (template_id) REFERENCES notification_template(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE notification_rule_channel (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    rule_id    INT NOT NULL,
    channel_id INT NOT NULL,
    enabled    TINYINT NOT NULL DEFAULT 1,
    UNIQUE KEY uk_rule_channel (rule_id, channel_id),
    FOREIGN KEY (rule_id)    REFERENCES notification_rule(id) ON DELETE CASCADE,
    FOREIGN KEY (channel_id) REFERENCES notification_channel(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE notification_message_log (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    rule_id     INT NOT NULL,
    channel_id  INT NOT NULL,
    source      VARCHAR(64) NOT NULL,
    event       VARCHAR(64) NOT NULL,
    payload     JSON NOT NULL,
    content     TEXT NOT NULL,
    status      ENUM('pending','success','failed','retrying') NOT NULL DEFAULT 'pending',
    retry_count INT NOT NULL DEFAULT 0,
    response    TEXT DEFAULT NULL,
    error_msg   TEXT DEFAULT NULL,
    created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_source_event_time (source, event, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
