<?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('payrolls', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->onDelete('cascade')->comment('테넌트 ID');
            $table->foreignId('user_id')->constrained()->onDelete('cascade')->comment('사용자 ID');
            $table->unsignedSmallInteger('pay_year')->comment('급여 연도');
            $table->unsignedTinyInteger('pay_month')->comment('급여 월 (1-12)');

            // 지급 항목
            $table->decimal('base_salary', 15, 2)->default(0)->comment('기본급');
            $table->decimal('overtime_pay', 15, 2)->default(0)->comment('연장근로수당');
            $table->decimal('bonus', 15, 2)->default(0)->comment('상여금');
            $table->json('allowances')->nullable()->comment('수당 상세 [{name, amount}]');
            $table->decimal('gross_salary', 15, 2)->default(0)->comment('총지급액');

            // 공제 항목
            $table->decimal('income_tax', 15, 2)->default(0)->comment('소득세');
            $table->decimal('resident_tax', 15, 2)->default(0)->comment('주민세');
            $table->decimal('health_insurance', 15, 2)->default(0)->comment('건강보험');
            $table->decimal('pension', 15, 2)->default(0)->comment('국민연금');
            $table->decimal('employment_insurance', 15, 2)->default(0)->comment('고용보험');
            $table->json('deductions')->nullable()->comment('공제 상세 [{name, amount}]');
            $table->decimal('total_deductions', 15, 2)->default(0)->comment('총공제액');

            // 실수령액
            $table->decimal('net_salary', 15, 2)->default(0)->comment('실수령액');

            // 상태 관리
            $table->string('status', 20)->default('draft')->comment('상태: draft/confirmed/paid');
            $table->timestamp('confirmed_at')->nullable()->comment('확정일시');
            $table->foreignId('confirmed_by')->nullable()->constrained('users')->nullOnDelete()->comment('확정자');
            $table->timestamp('paid_at')->nullable()->comment('지급일시');
            $table->foreignId('withdrawal_id')->nullable()->comment('출금 연결 ID');

            // 비고
            $table->text('note')->nullable()->comment('비고');

            // 감사 컬럼
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete()->comment('생성자');
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete()->comment('수정자');
            $table->foreignId('deleted_by')->nullable()->constrained('users')->nullOnDelete()->comment('삭제자');
            $table->softDeletes();
            $table->timestamps();

            // 인덱스
            $table->unique(['tenant_id', 'user_id', 'pay_year', 'pay_month'], 'uk_tenant_user_month');
            $table->index(['tenant_id', 'pay_year', 'pay_month'], 'idx_tenant_month');
            $table->index(['tenant_id', 'status'], 'idx_tenant_status');
        });
    }

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