<?php

declare(strict_types=1);

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::table('skus_mapping', function (Blueprint $table) {
            $table->bigInteger('origin_sku_id')->nullable()->comment('关联 skus_origin.id（FK）');
            $table->text('platform_outer_sku')->nullable()->comment('平台侧使用的 SKU 编码');
            $table->text('generation_strategy')->nullable()->comment('生成策略记录，如 prefix:C03、prefix_random:C03:4、manual');

            $table->index(['company_id', 'origin_sku_id', 'platform_id'], 'idx_company_origin_platform');
            $table->index(['platform_outer_sku', 'platform_id'], 'idx_outer_sku_platform');

            $table->foreign('origin_sku_id')
                ->references('id')
                ->on('skus_origin')
                ->onDelete('restrict')
                ->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('skus_mapping', function (Blueprint $table) {
            $table->dropForeign(['origin_sku_id']);
            $table->dropIndex('idx_company_origin_platform');
            $table->dropIndex('idx_outer_sku_platform');
            $table->dropColumn(['origin_sku_id', 'platform_outer_sku', 'generation_strategy']);
        });
    }
};
