<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('client_groups', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('group_code', 30)->comment('그룹 코드');
            $table->string('group_name', 100)->comment('그룹명');
            $table->decimal('price_rate', 5, 4)->default(1.0000)->comment('가격 배율 (1.0 = 기준가, 0.9 = 90%, 1.1 = 110%)');
            $table->tinyInteger('is_active')->default(1)->comment('활성 여부 (1=활성, 0=비활성)');

            // 감사 컬럼
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자 ID');

            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index(['tenant_id', 'is_active']);
            $table->unique(['tenant_id', 'group_code'], 'uq_client_groups_tenant_code');

            // 외래키
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('client_groups');
    }
};
