<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Carbon\Carbon;
use Hyperf\DbConnection\Db;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('platforms', function (Blueprint $table) {
            $table->integer('id')->primary()->comment('主键 - 平台 ID');
            $table->string('name', 100)->unique()->comment('平台名称');
            $table->string('label', 100)->nullable()->comment('平台名称 EN');
            $table->boolean('enabled')->default(true)->comment('平台状态');

            $table->timestampsTz();

            // 索引
            $table->index('name', 'idx_platforms_name');
            $table->index('created_at', 'idx_platforms_created_at');
            $table->index('updated_at', 'idx_platforms_updated_at');
        });

        // 填充初始数据
        $platforms = [
            ['id' => 1, 'name' => 'JD'],
            ['id' => 2, 'name' => 'Tmall'],
            ['id' => 3, 'name' => 'RedBook'],
            ['id' => 4, 'name' => 'Rakuten'],
            ['id' => 5, 'name' => 'Kaola'],
            ['id' => 6, 'name' => 'PDD'],
            ['id' => 7, 'name' => 'Wechat'],
            ['id' => 9, 'name' => '1688'],
            ['id' => 10, 'name' => 'ERP'],
            ['id' => 11, 'name' => 'DeWu'],
            ['id' => 17, 'name' => 'Amazon Japan'],
            ['id' => 18, 'name' => 'LAZADA'],
            ['id' => 19, 'name' => 'Shopify'],
            ['id' => 20, 'name' => 'DouYin'],
            ['id' => 21, 'name' => 'YouZan'],
            ['id' => 22, 'name' => 'Yahoo Japan'],
            ['id' => 23, 'name' => 'Coupang'],
            ['id' => 24, 'name' => 'Offline'],
            ['id' => 25, 'name' => 'Shopee'],
            ['id' => 26, 'name' => 'Naver'],
            ['id' => 27, 'name' => 'Wechat Video'],
            ['id' => 28, 'name' => 'VIP'],
            ['id' => 29, 'name' => 'Goofish'],
        ];

        foreach ($platforms as $platform) {
            Db::table('platforms')->insert([
                'id' => $platform['id'],
                'name' => $platform['name'],
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ]);
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('platforms');
    }
};
