<?php

declare(strict_types=1);

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('salary_slips', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->unsignedSmallInteger('year')->comment('年份');
            $table->unsignedTinyInteger('month')->comment('月份');
            $table->decimal('gross_salary', 12, 2)->default(0)->comment('应发合计');
            $table->decimal('total_deduction', 12, 2)->default(0)->comment('扣款合计');
            $table->decimal('net_salary', 12, 2)->default(0)->comment('实发工资');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态:1草稿,2已确认,3已发放');
            $table->dateTime('generated_at')->nullable()->comment('生成时间');
            $table->dateTime('confirmed_at')->nullable()->comment('确认时间');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['employee_id', 'year', 'month'], 'uk_employee_ym');
            $table->index('status');
        });

        // 工资条明细
        Schema::create('salary_slip_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('salary_slip_id')->constrained()->cascadeOnDelete();
            $table->unsignedBigInteger('item_id')->nullable()->comment('薪资项目ID');
            $table->string('item_name', 50)->comment('项目名称');
            $table->decimal('amount', 12, 2)->default(0)->comment('金额');
            $table->timestamps();

            $table->index('salary_slip_id');
        });

        // 社保公积金
        Schema::create('social_insurances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->decimal('base_amount', 12, 2)->default(0)->comment('缴费基数');
            $table->decimal('pension_personal', 5, 4)->default(0.08)->comment('养老保险个人比例');
            $table->decimal('pension_company', 5, 4)->default(0.16)->comment('养老保险企业比例');
            $table->decimal('medical_personal', 5, 4)->default(0.02)->comment('医疗保险个人比例');
            $table->decimal('medical_company', 5, 4)->default(0.095)->comment('医疗保险企业比例');
            $table->decimal('unemployment_personal', 5, 4)->default(0.005)->comment('失业保险个人比例');
            $table->decimal('unemployment_company', 5, 4)->default(0.005)->comment('失业保险企业比例');
            $table->decimal('injury_company', 5, 4)->default(0.0016)->comment('工伤保险企业比例');
            $table->decimal('maternity_company', 5, 4)->default(0.01)->comment('生育保险企业比例');
            $table->decimal('housing_personal', 5, 4)->default(0.07)->comment('住房公积金个人比例');
            $table->decimal('housing_company', 5, 4)->default(0.07)->comment('住房公积金企业比例');
            $table->date('effective_date')->comment('生效日期');
            $table->timestamps();

            $table->index('employee_id');
            $table->index('effective_date');
        });

        // 个税配置
        Schema::create('tax_configs', function (Blueprint $table) {
            $table->id();
            $table->unsignedSmallInteger('year')->comment('年度');
            $table->decimal('bracket_min', 12, 2)->comment('级距下限');
            $table->decimal('bracket_max', 12, 2)->nullable()->comment('级距上限');
            $table->decimal('rate', 5, 4)->comment('税率');
            $table->decimal('quick_deduction', 12, 2)->default(0)->comment('速算扣除数');
            $table->timestamps();

            $table->index('year');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('tax_configs');
        Schema::dropIfExists('social_insurances');
        Schema::dropIfExists('salary_slip_items');
        Schema::dropIfExists('salary_slips');
    }
};
