<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('主键');
            $table->integer('company_id')->comment('公司 ID');
            $table->integer('platform_id')->comment('平台 ID');
            $table->integer('store_id')->comment('店铺 ID');
            $table->integer('status_id')->comment('产品状态 ID');
            $table->integer('type_id')->comment('产品类型 ID');
            $table->integer('warehouse_id')->nullable()->default(null)->comment('仓库 ID');
            $table->integer('sub_warehouse_id')->nullable()->default(null)->comment('子仓库 ID');
            $table->text('platform_item_id')->comment('平台商品 ID，如平台没有规格细分，则对应 sku');
            $table->text('platform_model_id')->nullable()->default(null)->comment('此商品的规格 ID，对应 sku');
            $table->text('origin_sku_id')->nullable()->default(null)->comment('品牌方提供的原始 SKU ID，表示一个特定规格');
            $table->text('mapped_sku_id')->nullable()->default(null)->comment('映射后的 SKU ID，origin_sku_id 可能在电商平台无法填写，需要进行映射转换');
            $table->text('barcode')->nullable()->default(null)->comment('条形码');
            $table->text('hscode')->nullable()->default(null)->comment('统一海关货物编码');
            $table->jsonb('bundled')->nullable()->default(null)->comment('组合商品信息，包含详细的 sku 列表和数量');
            $table->decimal('price', 10, 2)->default(0)->comment('售价');
            $table->text('currency')->default('CNY')->comment('货币代码');
            $table->integer('num')->default(0)->comment('平台上设置的可售卖数量');
            $table->text('url')->nullable()->default(null)->comment('平台 URL');
            $table->text('picture')->nullable()->default(null)->comment('图片链接');
            $table->char('hash', 32)->comment('raw 字段的 MD5 哈希值，用于检测数据变化');
            $table->text('name')->nullable()->default(null)->comment('商品名称');
            $table->jsonb('raw')->comment('平台返回的原始产品信息');
            $table->jsonb('ext')->nullable()->default(null)->comment('扩展字段，用来记录其他额外信息');
            $table->timestampTz('created_date')->nullable()->default(null)->comment('商品在平台的创建时间');
            $table->timestampTz('updated_date')->nullable()->default(null)->comment('商品在平台的更新时间');
            $table->timestampsTz();

            // 索引
            $table->index('company_id');
            $table->index('platform_id');
            $table->index('store_id');
            $table->index('status_id');
            $table->index('type_id');
            $table->index('warehouse_id');
            $table->index('created_date');
            $table->index('updated_date');
            $table->index('origin_sku_id');
            $table->index('mapped_sku_id');
            $table->index('barcode');

            // 联合索引：优化按店铺查询某时间段内的商品
            $table->index(['store_id', 'created_date'], 'products_store_created_idx');
            $table->index(['store_id', 'updated_date'], 'products_store_updated_idx');

            // 联合唯一索引：店铺 + 平台商品ID + 规格ID
            $table->unique(['store_id', 'platform_item_id', 'platform_model_id'], 'products_store_item_model_unique');
        });

        // 为 jsonb 字段创建 GIN 索引（PostgreSQL）
        Schema::getConnection()->statement('CREATE INDEX products_raw_gin_idx ON products USING gin (raw)');
        Schema::getConnection()->statement('CREATE INDEX products_ext_gin_idx ON products USING gin (ext)');
        Schema::getConnection()->statement('CREATE INDEX products_bundled_gin_idx ON products USING gin (bundled)');
    }

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