<?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('sales_partners', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('연결된 사용자 ID');
            $table->string('partner_code', 20)->unique()->comment('파트너 고유 코드');
            $table->enum('partner_type', ['individual', 'corporate'])->default('individual')->comment('파트너 유형');

            // 수수료 정보
            $table->decimal('commission_rate', 5, 2)->default(20.00)->comment('기본 수수료율 (%)');
            $table->decimal('manager_commission_rate', 5, 2)->default(5.00)->comment('관리자 수수료율 (%)');

            // 계좌 정보
            $table->string('bank_name', 50)->nullable()->comment('은행명');
            $table->string('account_number', 50)->nullable()->comment('계좌번호');
            $table->string('account_holder', 50)->nullable()->comment('예금주');

            // 상태 관리
            $table->enum('status', ['pending', 'active', 'inactive', 'suspended'])->default('pending')->comment('상태');
            $table->timestamp('approved_at')->nullable()->comment('승인 일시');
            $table->unsignedBigInteger('approved_by')->nullable()->comment('승인자 ID');

            // 실적 통계 (캐시용)
            $table->unsignedInteger('total_contracts')->default(0)->comment('총 계약 건수');
            $table->decimal('total_commission', 15, 2)->default(0)->comment('총 누적 수당');

            $table->text('notes')->nullable()->comment('메모');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('user_id');
            $table->index('status');
            $table->index('partner_type');
        });
    }

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