<?php

/**
 * 文件功能：为商店补充新增特效商品数据
 * 将 6 种新特效的单次卡与周卡写入 shop_items，供现有站点直接使用。
 */

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

/**
 * 商店新增特效商品数据迁移
 * 负责把新特效商品同步到已有数据库。
 */
return new class extends Migration
{
    /**
     * 写入新增特效商品，已存在时按 slug 更新展示字段。
     */
    public function up(): void
    {
        $now = now();
        $items = [
            ['name' => '樱花飘落单次卡', 'slug' => 'once_sakura', 'description' => '购买即刻在聊天室飘落浪漫樱花，适合欢迎、节日与轻氛围场景。', 'icon' => '🌸', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 5, 'is_active' => true],
            ['name' => '流星单次卡', 'slug' => 'once_meteors', 'description' => '流星划过夜空，亮度更强、数量更多，适合 VIP 登场和晚间氛围。', 'icon' => '🌠', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 6, 'is_active' => true],
            ['name' => '金币雨单次卡', 'slug' => 'once_gold-rain', 'description' => '金币从天而降，适合开奖、奖励和活动发放时刻。', 'icon' => '🪙', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 7, 'is_active' => true],
            ['name' => '爱心飘落单次卡', 'slug' => 'once_hearts', 'description' => '柔和爱心缓缓飘落，适合婚礼、表白与祝福场景。', 'icon' => '💖', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 8, 'is_active' => true],
            ['name' => '彩带庆典单次卡', 'slug' => 'once_confetti', 'description' => '连续彩带和纸屑庆典从天而降，适合公告、中奖和庆祝。', 'icon' => '🎊', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 9, 'is_active' => true],
            ['name' => '萤火虫单次卡', 'slug' => 'once_fireflies', 'description' => '发光萤火虫在屏幕间穿梭，适合安静、夜色和浪漫房间。', 'icon' => '✨', 'price' => 888, 'type' => 'instant', 'duration_days' => null, 'sort_order' => 10, 'is_active' => true],
            ['name' => '樱花飘落周卡', 'slug' => 'week_sakura', 'description' => '连续7天，每次进入聊天室自动飘落樱花。同时只能激活一种周卡。', 'icon' => '🌸', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 15, 'is_active' => true],
            ['name' => '流星周卡', 'slug' => 'week_meteors', 'description' => '连续7天，每次进入聊天室自动划过流星。同时只能激活一种周卡。', 'icon' => '🌠', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 16, 'is_active' => true],
            ['name' => '金币雨周卡', 'slug' => 'week_gold-rain', 'description' => '连续7天，每次进入聊天室自动触发金币雨。同时只能激活一种周卡。', 'icon' => '🪙', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 17, 'is_active' => true],
            ['name' => '爱心飘落周卡', 'slug' => 'week_hearts', 'description' => '连续7天，每次进入聊天室自动飘落爱心。同时只能激活一种周卡。', 'icon' => '💖', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 18, 'is_active' => true],
            ['name' => '彩带庆典周卡', 'slug' => 'week_confetti', 'description' => '连续7天，每次进入聊天室自动触发彩带庆典。同时只能激活一种周卡。', 'icon' => '🎊', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 19, 'is_active' => true],
            ['name' => '萤火虫周卡', 'slug' => 'week_fireflies', 'description' => '连续7天，每次进入聊天室自动飞出萤火虫。同时只能激活一种周卡。', 'icon' => '✨', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7, 'sort_order' => 20, 'is_active' => true],
        ];

        // 统一补齐创建/更新时间，便于 upsert 覆盖已有同 slug 商品。
        $payload = array_map(static function (array $item) use ($now): array {
            return $item + [
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }, $items);

        DB::table('shop_items')->upsert(
            $payload,
            ['slug'],
            ['name', 'description', 'icon', 'price', 'type', 'duration_days', 'sort_order', 'is_active', 'updated_at']
        );
    }

    /**
     * 回滚新增特效商品，删除本次补进的 12 个商店条目。
     */
    public function down(): void
    {
        DB::table('shop_items')->whereIn('slug', [
            'once_sakura',
            'once_meteors',
            'once_gold-rain',
            'once_hearts',
            'once_confetti',
            'once_fireflies',
            'week_sakura',
            'week_meteors',
            'week_gold-rain',
            'week_hearts',
            'week_confetti',
            'week_fireflies',
        ])->delete();
    }
};
