<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('stores', function (Blueprint $table) {
            $table->bigInteger('id')->primary()->comment('主键 - 来自远程 和 tools 保持一致 ');
            $table->integer('company_id')->comment('店铺所属的公司 ID');
            $table->integer('platform_id')->comment('平台 ID');
            $table->text('platform_store_id')->nullable()->comment('平台店铺 ID');
            $table->integer('warehouse_id')->nullable()->comment('店铺使用的仓库 ID');
            $table->integer('currency_id')->comment('店铺的本币 ID');
            $table->boolean('enabled')->default('true')->comment('激活状态');
            $table->string('name')->unique()->comment('店铺名 英文');
            $table->string('label')->nullable()->default('null')->comment('店铺名 中文');
            $table->jsonb('ext')->nullable()->comment('扩展字段');

            $table->timestampsTz();

            // 索引
            $table->index('company_id', 'idx_stores_company_id');
            $table->index('platform_id', 'idx_stores_platform_id');
            $table->index('platform_store_id', 'idx_stores_platform_store_id');
            $table->index('warehouse_id', 'idx_stores_warehouse_id');
            $table->index('enabled', 'idx_stores_enabled');
            $table->index(['company_id', 'enabled'], 'idx_stores_company_enabled');
            $table->index(['platform_id', 'enabled'], 'idx_stores_platform_enabled');
            $table->index(['company_id', 'platform_id'], 'idx_stores_company_platform');
            $table->index('created_at', 'idx_stores_created_at');
            $table->index('updated_at', 'idx_stores_updated_at');

        });

        // 为 JSONB 字段创建 GIN 索引，支持高效的 JSON 查询
        Db::statement('CREATE INDEX idx_stores_ext ON stores USING GIN (ext)');
    }

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