<?php

/**
 * 文件功能：节日福利模板年度调度字段迁移
 *
 * 为节日福利模板补充按年复用、连续多天与单日多轮发送所需的调度字段。
 */

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * 升级 holiday_events 表结构。
     */
    public function up(): void
    {
        Schema::table('holiday_events', function (Blueprint $table) {
            $table->tinyInteger('schedule_month')->unsigned()->nullable()->after('repeat_type')->comment('年度节日触发月份');
            $table->tinyInteger('schedule_day')->unsigned()->nullable()->after('schedule_month')->comment('年度节日触发日');
            $table->string('schedule_time', 5)->nullable()->after('schedule_day')->comment('年度节日首轮开始时间（HH:ii）');
            $table->unsignedSmallInteger('duration_days')->default(1)->after('schedule_time')->comment('连续发放天数');
            $table->unsignedSmallInteger('daily_occurrences')->default(1)->after('duration_days')->comment('每天触发次数');
            $table->unsignedSmallInteger('occurrence_interval_minutes')->nullable()->after('daily_occurrences')->comment('同一天多轮发送间隔（分钟）');
        });

        Schema::table('holiday_events', function (Blueprint $table) {
            // Laravel 12 / MySQL 变更列时需要显式保留原有默认值。
            $table->enum('repeat_type', ['once', 'daily', 'weekly', 'monthly', 'cron', 'yearly'])
                ->default('once')
                ->change();
        });
    }

    /**
     * 回滚 holiday_events 的年度调度字段。
     */
    public function down(): void
    {
        Schema::table('holiday_events', function (Blueprint $table) {
            $table->enum('repeat_type', ['once', 'daily', 'weekly', 'monthly', 'cron'])
                ->default('once')
                ->change();

            $table->dropColumn([
                'schedule_month',
                'schedule_day',
                'schedule_time',
                'duration_days',
                'daily_occurrences',
                'occurrence_interval_minutes',
            ]);
        });
    }
};
