<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `notification_triggers` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `trigger_code` VARCHAR(100) NOT NULL UNIQUE,
            `trigger_name_ar` VARCHAR(255) NOT NULL,
            `event_name` VARCHAR(150) NOT NULL,
            `template_id` BIGINT UNSIGNED NULL,
            `is_active` TINYINT(1) NOT NULL DEFAULT 1,
            `channel` VARCHAR(30) NOT NULL DEFAULT 'sms' COMMENT 'sms, whatsapp, push',
            `recipient_type` VARCHAR(50) NOT NULL DEFAULT 'player' COMMENT 'player, member, coach, parent, player_and_parent, booker, group_players, session_players, direct',
            `config_json` JSON NULL COMMENT 'delay_minutes, max_attempts, custom settings',
            `priority` INT UNSIGNED NOT NULL DEFAULT 0,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_nt_event` (`event_name`),
            INDEX `idx_nt_active` (`is_active`),
            INDEX `idx_nt_code` (`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(100) NOT NULL,
            `recipient_type` VARCHAR(50) NOT NULL,
            `recipient_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
            `recipient_phone` VARCHAR(20) NOT 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 'original event data for reference',
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_sn_status_scheduled` (`status`, `scheduled_at`),
            INDEX `idx_sn_trigger` (`trigger_code`),
            INDEX `idx_sn_recipient` (`recipient_type`, `recipient_id`),
            INDEX `idx_sn_sent` (`sent_at`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    ",
    'down' => "
        DROP TABLE IF EXISTS `scheduled_notifications`;
        DROP TABLE IF EXISTS `notification_triggers`;
    ",
];
