<?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('bad_debts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('client_id')->comment('거래처 ID');
            $table->decimal('debt_amount', 15, 2)->default(0)->comment('채권 금액');
            $table->string('status', 20)->default('collecting')->comment('상태: collecting(추심중), legal_action(법적조치), recovered(회수완료), bad_debt(대손처리)');
            $table->integer('overdue_days')->default(0)->comment('연체일수');
            $table->unsignedBigInteger('assigned_user_id')->nullable()->comment('담당자 ID');
            $table->date('occurred_at')->nullable()->comment('악성채권 발생일');
            $table->date('closed_at')->nullable()->comment('악성채권 종료일');
            $table->boolean('is_active')->default(true)->comment('설정 활성화 여부');
            $table->json('options')->nullable()->comment('추가 옵션 JSON');
            $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');
            $table->index('client_id');
            $table->index('status');
            $table->index('is_active');
            $table->index(['tenant_id', 'status']);
        });
    }

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