<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('business_income_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->comment('테넌트');
            $table->foreignId('user_id')->constrained('users')->comment('사업소득자 사용자');
            $table->unsignedSmallInteger('pay_year')->comment('급여 연도');
            $table->unsignedTinyInteger('pay_month')->comment('급여 월 (1-12)');
            $table->string('service_content', 200)->nullable()->comment('용역내용');
            $table->decimal('gross_amount', 15, 0)->default(0)->comment('지급총액');
            $table->decimal('income_tax', 15, 0)->default(0)->comment('소득세 (3%)');
            $table->decimal('local_income_tax', 15, 0)->default(0)->comment('지방소득세 (0.3%)');
            $table->decimal('total_deductions', 15, 0)->default(0)->comment('공제합계');
            $table->decimal('net_amount', 15, 0)->default(0)->comment('실지급액');
            $table->date('payment_date')->nullable()->comment('지급일자');
            $table->text('note')->nullable()->comment('비고');
            $table->string('status', 20)->default('draft')->comment('상태: draft/confirmed/paid');
            $table->timestamp('confirmed_at')->nullable()->comment('확정일시');
            $table->foreignId('confirmed_by')->nullable()->constrained('users')->comment('확정자');
            $table->timestamp('paid_at')->nullable()->comment('지급일시');
            $table->foreignId('created_by')->nullable()->constrained('users')->comment('생성자');
            $table->foreignId('updated_by')->nullable()->constrained('users')->comment('수정자');
            $table->foreignId('deleted_by')->nullable()->constrained('users')->comment('삭제자');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'user_id', 'pay_year', 'pay_month'], 'bip_tenant_user_period_unique');
            $table->index(['tenant_id', 'pay_year', 'pay_month'], 'bip_tenant_period_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('business_income_payments');
    }
};
