<?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('users', function (Blueprint $table) {
            // 영업담당자 계층 구조 (상위 관리자)
            $table->unsignedBigInteger('parent_id')->nullable()->after('is_super_admin');
            $table->foreign('parent_id')->references('id')->on('users')->onDelete('set null');

            // 승인 상태: pending(대기), approved(승인), rejected(반려)
            $table->string('approval_status', 20)->default('approved')->after('parent_id');

            // 승인 관련 정보
            $table->unsignedBigInteger('approved_by')->nullable()->after('approval_status');
            $table->timestamp('approved_at')->nullable()->after('approved_by');
            $table->text('rejection_reason')->nullable()->after('approved_at');

            // 인덱스
            $table->index('parent_id');
            $table->index('approval_status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['parent_id']);
            $table->dropIndex(['parent_id']);
            $table->dropIndex(['approval_status']);

            $table->dropColumn([
                'parent_id',
                'approval_status',
                'approved_by',
                'approved_at',
                'rejection_reason',
            ]);
        });
    }
};
