<?php

/**
 * 文件功能：为商店商品类型加入签到补签卡。
 *
 * 允许后台商店配置补签卡，并提供一条默认上架商品。
 */

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

return new class extends Migration
{
    /**
     * 方法功能：扩展商品类型枚举并写入默认补签卡。
     */
    public function up(): void
    {
        if (DB::getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE `shop_items` MODIFY `type` ENUM('instant','duration','one_time','ring','auto_fishing','sign_repair') NOT NULL COMMENT '道具类型'");
        }

        DB::table('shop_items')->updateOrInsert(
            ['slug' => 'sign_repair_card'],
            [
                'name' => '补签卡',
                'description' => '用于补签本月漏掉的未签到日期，每张可补签 1 天；不能补签上月或更早日期。',
                'icon' => '🗓️',
                'price' => 10000,
                'type' => 'sign_repair',
                'duration_days' => 0,
                'duration_minutes' => 0,
                'intimacy_bonus' => 0,
                'charm_bonus' => 0,
                'sort_order' => 35,
                'is_active' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ]
        );
    }

    /**
     * 方法功能：回滚补签卡商品类型。
     */
    public function down(): void
    {
        DB::table('shop_items')->where('slug', 'sign_repair_card')->delete();

        if (DB::getDriverName() === 'mysql') {
            DB::statement("UPDATE `shop_items` SET `type` = 'one_time' WHERE `type` = 'sign_repair'");
            DB::statement("ALTER TABLE `shop_items` MODIFY `type` ENUM('instant','duration','one_time','ring','auto_fishing') NOT NULL COMMENT '道具类型'");
        }
    }
};
