<?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('stat_kpi_targets', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->smallInteger('stat_year');
            $table->tinyInteger('stat_month')->nullable()->comment('NULL = 연간 목표');

            $table->string('domain', 50)->comment('sales, production, finance 등');
            $table->string('metric_code', 100)->comment('monthly_sales_amount 등');
            $table->decimal('target_value', 18, 2);
            $table->string('unit', 20)->default('KRW')->comment('KRW, %, count, hours');
            $table->string('description', 300)->nullable();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();

            $table->unique(['tenant_id', 'stat_year', 'stat_month', 'metric_code'], 'uk_tenant_metric');
            $table->index('domain', 'idx_domain');
        });
    }

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