<?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('salaries', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('employee_id')->comment('직원 ID');
            $table->unsignedSmallInteger('year')->comment('년도');
            $table->unsignedTinyInteger('month')->comment('월');

            // 급여 금액
            $table->decimal('base_salary', 15, 2)->default(0)->comment('기본급');
            $table->decimal('total_allowance', 15, 2)->default(0)->comment('총 수당');
            $table->decimal('total_overtime', 15, 2)->default(0)->comment('초과근무수당');
            $table->decimal('total_bonus', 15, 2)->default(0)->comment('상여');
            $table->decimal('total_deduction', 15, 2)->default(0)->comment('총 공제');
            $table->decimal('net_payment', 15, 2)->default(0)->comment('실지급액');

            // 상세 내역 (JSON)
            $table->json('allowance_details')->nullable()->comment('수당 상세 내역');
            $table->json('deduction_details')->nullable()->comment('공제 상세 내역');

            // 지급 정보
            $table->date('payment_date')->nullable()->comment('지급일');
            $table->enum('status', ['scheduled', 'completed'])->default('scheduled')->comment('상태: 지급예정/지급완료');

            // 감사 필드
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('tenant_id');
            $table->index('employee_id');
            $table->index(['year', 'month']);
            $table->index('status');
            $table->unique(['tenant_id', 'employee_id', 'year', 'month'], 'unique_salary_per_employee_month');
        });
    }

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