<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * 거래처 2차 확장 필드 추가 (sam-design 기준)
     */
    public function up(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            // 기본 정보
            $table->enum('client_type', ['매입', '매출', '매입매출'])->default('매입')->after('is_active')->comment('거래처 유형');

            // 연락처 정보
            $table->string('mobile', 20)->nullable()->after('phone')->comment('모바일 번호');
            $table->string('fax', 20)->nullable()->after('mobile')->comment('팩스 번호');

            // 담당자 정보
            $table->string('manager_name', 50)->nullable()->after('contact_person')->comment('담당자명');
            $table->string('manager_tel', 20)->nullable()->after('manager_name')->comment('담당자 전화');
            $table->string('system_manager', 50)->nullable()->after('manager_tel')->comment('시스템 관리자');

            // 발주처 설정
            $table->string('account_id', 50)->nullable()->after('address')->comment('계정 ID');
            $table->string('account_password', 255)->nullable()->after('account_id')->comment('비밀번호 (암호화)');
            $table->string('purchase_payment_day', 20)->nullable()->after('account_password')->comment('매입 결제일');
            $table->string('sales_payment_day', 20)->nullable()->after('purchase_payment_day')->comment('매출 결제일');

            // 약정 세금
            $table->boolean('tax_agreement')->default(false)->after('business_item')->comment('세금 약정 여부');
            $table->decimal('tax_amount', 15, 2)->nullable()->after('tax_agreement')->comment('약정 금액');
            $table->date('tax_start_date')->nullable()->after('tax_amount')->comment('약정 시작일');
            $table->date('tax_end_date')->nullable()->after('tax_start_date')->comment('약정 종료일');

            // 악성채권 정보
            $table->boolean('bad_debt')->default(false)->after('tax_end_date')->comment('악성채권 여부');
            $table->decimal('bad_debt_amount', 15, 2)->nullable()->after('bad_debt')->comment('악성채권 금액');
            $table->date('bad_debt_receive_date')->nullable()->after('bad_debt_amount')->comment('채권 발생일');
            $table->date('bad_debt_end_date')->nullable()->after('bad_debt_receive_date')->comment('채권 만료일');
            $table->enum('bad_debt_progress', ['협의중', '소송중', '회수완료', '대손처리'])->nullable()->after('bad_debt_end_date')->comment('진행 상태');

            // 기타 정보
            $table->text('memo')->nullable()->after('bad_debt_progress')->comment('메모');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('clients', function (Blueprint $table) {
            $table->dropColumn([
                'client_type',
                'mobile',
                'fax',
                'manager_name',
                'manager_tel',
                'system_manager',
                'account_id',
                'account_password',
                'purchase_payment_day',
                'sales_payment_day',
                'tax_agreement',
                'tax_amount',
                'tax_start_date',
                'tax_end_date',
                'bad_debt',
                'bad_debt_amount',
                'bad_debt_receive_date',
                'bad_debt_end_date',
                'bad_debt_progress',
                'memo',
            ]);
        });
    }
};
