<?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_definitions', function (Blueprint $table) {
            $table->id();
            $table->string('code', 100)->unique()->comment('통계 코드 (sales_daily_revenue)');
            $table->string('domain', 50)->index()->comment('도메인 (sales, finance, production)');
            $table->string('name', 200)->comment('통계명 (일일 매출액)');
            $table->text('description')->nullable();
            $table->json('source_tables')->comment('원본 테이블 목록');
            $table->string('aggregation', 20)->default('daily')->index()->comment('집계 주기');
            $table->text('query_template')->nullable()->comment('집계 SQL 템플릿');
            $table->boolean('is_active')->default(true)->index();
            $table->json('config')->nullable()->comment('추가 설정 (임계값, 단위 등)');
            $table->timestamps();
        });
    }

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