<?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::table('clients', function (Blueprint $table) {
            $table->unsignedBigInteger('client_group_id')
                ->nullable()
                ->after('tenant_id')
                ->comment('고객 그룹 ID (NULL = 기본 그룹)');

            $table->index(['tenant_id', 'client_group_id']);
            $table->foreign('client_group_id')->references('id')->on('client_groups')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            $table->dropForeign(['client_group_id']);
            $table->dropIndex(['tenant_id', 'client_group_id']);
            $table->dropColumn('client_group_id');
        });
    }
};
