<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    protected $connection = 'sam_stat';

    public function up(): void
    {
        Schema::connection($this->connection)->create('dim_product', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('item_id')->comment('원본 items.id');
            $table->string('item_code', 100);
            $table->string('item_name', 300);
            $table->string('item_type', 50)->nullable()->comment('item_type from items');
            $table->unsignedBigInteger('category_id')->nullable();
            $table->string('category_name', 200)->nullable();
            $table->date('valid_from');
            $table->date('valid_to')->nullable()->comment('NULL = 현재 유효');
            $table->boolean('is_current')->default(true);

            $table->index(['tenant_id', 'item_id'], 'idx_tenant_item');
            $table->index('is_current', 'idx_current');
        });
    }

    public function down(): void
    {
        Schema::connection($this->connection)->dropIfExists('dim_product');
    }
};