<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `notification_triggers` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `trigger_code` VARCHAR(50) NOT NULL,
            `trigger_name_ar` VARCHAR(200) NOT NULL,
            `trigger_name_en` VARCHAR(200) NULL,
            `event_name` VARCHAR(100) NOT NULL COMMENT 'EventBus event to listen to',
            `template_id` BIGINT UNSIGNED NULL,
            `is_active` TINYINT(1) NOT NULL DEFAULT 1,
            `channel` VARCHAR(20) NOT NULL DEFAULT 'sms' COMMENT 'sms, push, email, all',
            `recipient_type` VARCHAR(30) NOT NULL DEFAULT 'player' COMMENT 'player, member, coach, parent, all',
            `config_json` JSON NULL COMMENT 'timing: days_before, repeat_interval, max_retries',
            `priority` TINYINT UNSIGNED NOT NULL DEFAULT 5 COMMENT '1=urgent, 5=normal, 10=low',
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY `uq_nt_code` (`trigger_code`),
            INDEX `idx_nt_event` (`event_name`),
            INDEX `idx_nt_active` (`is_active`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `notification_subscriptions` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `subscriber_type` VARCHAR(20) NOT NULL COMMENT 'player, member, coach, parent',
            `subscriber_id` BIGINT UNSIGNED NOT NULL,
            `trigger_code` VARCHAR(50) NOT NULL,
            `is_opted_in` TINYINT(1) NOT NULL DEFAULT 1,
            `channel_preference` VARCHAR(20) NULL DEFAULT 'sms',
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY `uq_ns_sub_trigger` (`subscriber_type`, `subscriber_id`, `trigger_code`),
            INDEX `idx_ns_subscriber` (`subscriber_type`, `subscriber_id`),
            INDEX `idx_ns_trigger` (`trigger_code`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `scheduled_notifications` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `trigger_code` VARCHAR(50) NOT NULL,
            `recipient_type` VARCHAR(20) NOT NULL,
            `recipient_id` BIGINT UNSIGNED NOT NULL,
            `recipient_phone` VARCHAR(30) NULL,
            `message` TEXT NOT NULL,
            `scheduled_at` DATETIME NOT NULL,
            `sent_at` DATETIME NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, sent, failed, cancelled',
            `attempts` INT UNSIGNED NOT NULL DEFAULT 0,
            `max_attempts` INT UNSIGNED NOT NULL DEFAULT 3,
            `error_message` TEXT NULL,
            `context_json` JSON NULL COMMENT 'related entity IDs for deduplication',
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_sn_status_scheduled` (`status`, `scheduled_at`),
            INDEX `idx_sn_recipient` (`recipient_type`, `recipient_id`),
            INDEX `idx_sn_trigger` (`trigger_code`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `scheduled_notifications`;
        DROP TABLE IF EXISTS `notification_subscriptions`;
        DROP TABLE IF EXISTS `notification_triggers`
    ",
];
