<?php
/**
 * NotificationsSchema — DB schema for the Notifications module.
 *
 * Tables:
 *   1. ultraguard_notification_log      — record of every dispatched notification
 *   2. ultraguard_notification_rules    — user-defined rules: events → channels
 *   3. ultraguard_notification_channels — saved reusable delivery destinations
 *
 * @package UltraguardPro\modules\notifications\schema
 * @since   6.5.0
 */

namespace UltraguardPro\modules\notifications\schema;

use UltraguardPro\modules\core\AbstractModuleSchema;

if ( ! defined( 'ABSPATH' ) ) { exit; }

class NotificationsSchema extends AbstractModuleSchema {

    public function get_module_id(): string { return 'notifications'; }

    // ── Table map ──────────────────────────────────────────────────────────────

    public function get_table_map(): array {
        return [
            'notification_log'      => \UltraguardPro\modules\notifications\db\NotificationLogTable::class,
            'notification_rules'    => \UltraguardPro\modules\notifications\db\NotificationRulesTable::class,
            'notification_channels' => \UltraguardPro\modules\notifications\db\NotificationChannelsTable::class,
        ];
    }

    // ── CREATE TABLE definitions ───────────────────────────────────────────────

    public function get_schemas(): array {
        $cs = $this->charset_collate();

        return [

            // ── 1. Notification log ────────────────────────────────────────────
            $this->table( 'ultraguard_notification_log' ) =>
            "CREATE TABLE {$this->table('ultraguard_notification_log')} (
              id            bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              event_type    varchar(100)        NOT NULL DEFAULT '',
              channel       varchar(20)         NOT NULL DEFAULT 'email',
              recipient     varchar(255)        NOT NULL DEFAULT '',
              subject       varchar(255)        NOT NULL DEFAULT '',
              message       text                NOT NULL,
              status        varchar(20)         NOT NULL DEFAULT 'sent',
              rule_id       bigint(20) UNSIGNED          DEFAULT NULL,
              error_message text                         DEFAULT NULL,
              context_json  longtext                     DEFAULT NULL,
              sent_at       datetime                     DEFAULT NULL,
              created_at    datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_event_type (event_type),
              KEY idx_channel    (channel),
              KEY idx_status     (status),
              KEY idx_rule_id    (rule_id),
              KEY idx_sent_at    (sent_at),
              KEY idx_created_at (created_at)
            ) {$cs};",

            // ── 2. Notification rules ──────────────────────────────────────────
            $this->table( 'ultraguard_notification_rules' ) =>
            "CREATE TABLE {$this->table('ultraguard_notification_rules')} (
              id                  bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              name                varchar(255)        NOT NULL DEFAULT '',
              event_type          varchar(100)        NOT NULL DEFAULT '',
              severity_threshold  varchar(20)         NOT NULL DEFAULT 'medium',
              channel             varchar(20)         NOT NULL DEFAULT 'email',
              recipient           varchar(500)        NOT NULL DEFAULT '',
              subject_template    varchar(255)        NOT NULL DEFAULT '',
              message_template    text                NOT NULL,
              webhook_secret      varchar(128)                 DEFAULT NULL,
              is_active           tinyint(1)          NOT NULL DEFAULT 1,
              cooldown_minutes    int(11)             NOT NULL DEFAULT 60,
              last_triggered_at   datetime                     DEFAULT NULL,
              trigger_count       bigint(20) UNSIGNED NOT NULL DEFAULT 0,
              created_by          bigint(20) UNSIGNED          DEFAULT NULL,
              created_at          datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              updated_at          datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_event_type  (event_type),
              KEY idx_is_active   (is_active),
              KEY idx_severity    (severity_threshold),
              KEY idx_channel     (channel)
            ) {$cs};",

            // ── 3. Notification channels (saved delivery destinations) ─────────
            $this->table( 'ultraguard_notification_channels' ) =>
            "CREATE TABLE {$this->table('ultraguard_notification_channels')} (
              id             bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              name           varchar(255)        NOT NULL DEFAULT '',
              channel_type   varchar(20)         NOT NULL DEFAULT 'email',
              address        varchar(500)        NOT NULL DEFAULT '',
              webhook_secret varchar(128)                 DEFAULT NULL,
              config_json    longtext                     DEFAULT NULL,
              is_active      tinyint(1)          NOT NULL DEFAULT 1,
              last_tested_at datetime                     DEFAULT NULL,
              test_status    varchar(20)                  DEFAULT NULL,
              created_by     bigint(20) UNSIGNED          DEFAULT NULL,
              created_at     datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
              updated_at     datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (id),
              KEY idx_channel_type (channel_type),
              KEY idx_is_active    (is_active)
            ) {$cs};",
        ];
    }
}
