<?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_client', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('client_id')->comment('원본 clients.id');
            $table->string('client_name', 200);
            $table->unsignedBigInteger('client_group_id')->nullable();
            $table->string('client_group_name', 200)->nullable();
            $table->string('client_type', 50)->nullable()->comment('고객/공급업체/양쪽');
            $table->string('region', 100)->nullable();
            $table->date('valid_from');
            $table->date('valid_to')->nullable()->comment('NULL = 현재 유효');
            $table->boolean('is_current')->default(true);

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

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