<?php

/**
 * 文件功能：创建每日签到奖励规则表。
 *
 * 规则按连续签到天数配置奖励，签到服务会匹配小于等于当前连续天数的最高启用规则。
 */

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

/**
 * 类功能：维护 sign_in_reward_rules 表结构与回滚逻辑。
 */
return new class extends Migration
{
    /**
     * 创建每日签到奖励规则表。
     */
    public function up(): void
    {
        Schema::create('sign_in_reward_rules', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('streak_days')->unique()->comment('连续签到天数门槛');
            $table->unsignedInteger('gold_reward')->default(0)->comment('金币奖励');
            $table->unsignedInteger('exp_reward')->default(0)->comment('经验奖励');
            $table->unsignedInteger('charm_reward')->default(0)->comment('魅力奖励');
            $table->string('identity_badge_code', 50)->nullable()->comment('身份徽章编码');
            $table->string('identity_badge_name', 50)->nullable()->comment('身份徽章名称');
            $table->string('identity_badge_icon', 120)->nullable()->comment('身份徽章图标');
            $table->string('identity_badge_color', 20)->nullable()->comment('身份徽章文字颜色');
            $table->unsignedSmallInteger('identity_duration_days')->default(0)->comment('身份有效天数，0表示永久');
            $table->boolean('is_enabled')->default(true)->index()->comment('是否启用');
            $table->unsignedInteger('sort_order')->default(0)->index()->comment('后台排序值');
            $table->timestamps();

            $table->index(['is_enabled', 'streak_days'], 'idx_sign_in_rules_enabled_streak');
        });

        DB::table('sign_in_reward_rules')->insert($this->defaultRules());
    }

    /**
     * 回滚：删除每日签到奖励规则表。
     */
    public function down(): void
    {
        Schema::dropIfExists('sign_in_reward_rules');
    }

    /**
     * 默认连续签到奖励规则。
     *
     * @return array<int, array<string, mixed>>
     */
    private function defaultRules(): array
    {
        $now = now();

        return [
            [
                'streak_days' => 1,
                'gold_reward' => 100,
                'exp_reward' => 10,
                'charm_reward' => 0,
                'identity_badge_code' => 'sign_in_new_star',
                'identity_badge_name' => '签到新星',
                'identity_badge_icon' => '✅',
                'identity_badge_color' => '#0f766e',
                'identity_duration_days' => 7,
                'is_enabled' => true,
                'sort_order' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 3,
                'gold_reward' => 180,
                'exp_reward' => 18,
                'charm_reward' => 1,
                'identity_badge_code' => 'sign_in_spark',
                'identity_badge_name' => '三日星火',
                'identity_badge_icon' => '✨',
                'identity_badge_color' => '#2563eb',
                'identity_duration_days' => 10,
                'is_enabled' => true,
                'sort_order' => 3,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 7,
                'gold_reward' => 350,
                'exp_reward' => 35,
                'charm_reward' => 3,
                'identity_badge_code' => 'sign_in_flame',
                'identity_badge_name' => '七日恒星',
                'identity_badge_icon' => '🔥',
                'identity_badge_color' => '#dc2626',
                'identity_duration_days' => 30,
                'is_enabled' => true,
                'sort_order' => 7,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 14,
                'gold_reward' => 600,
                'exp_reward' => 60,
                'charm_reward' => 5,
                'identity_badge_code' => 'sign_in_moon_guard',
                'identity_badge_name' => '半月守望',
                'identity_badge_icon' => '🌙',
                'identity_badge_color' => '#7c3aed',
                'identity_duration_days' => 45,
                'is_enabled' => true,
                'sort_order' => 14,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 30,
                'gold_reward' => 1200,
                'exp_reward' => 120,
                'charm_reward' => 8,
                'identity_badge_code' => 'sign_in_month_master',
                'identity_badge_name' => '月签达人',
                'identity_badge_icon' => '🏆',
                'identity_badge_color' => '#ca8a04',
                'identity_duration_days' => 60,
                'is_enabled' => true,
                'sort_order' => 30,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 60,
                'gold_reward' => 2200,
                'exp_reward' => 220,
                'charm_reward' => 14,
                'identity_badge_code' => 'sign_in_diamond',
                'identity_badge_name' => '双月钻冕',
                'identity_badge_icon' => '💎',
                'identity_badge_color' => '#0891b2',
                'identity_duration_days' => 90,
                'is_enabled' => true,
                'sort_order' => 60,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 100,
                'gold_reward' => 4000,
                'exp_reward' => 400,
                'charm_reward' => 25,
                'identity_badge_code' => 'sign_in_legend',
                'identity_badge_name' => '百日传奇',
                'identity_badge_icon' => '👑',
                'identity_badge_color' => '#9333ea',
                'identity_duration_days' => 180,
                'is_enabled' => true,
                'sort_order' => 100,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'streak_days' => 365,
                'gold_reward' => 12000,
                'exp_reward' => 1200,
                'charm_reward' => 100,
                'identity_badge_code' => 'sign_in_year_guardian',
                'identity_badge_name' => '周年守护者',
                'identity_badge_icon' => '🌟',
                'identity_badge_color' => '#e11d48',
                'identity_duration_days' => 0,
                'is_enabled' => true,
                'sort_order' => 365,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ];
    }
};
