<?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('employee_profiles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('employee_no', 30)->nullable();
            $table->date('hire_date')->nullable();
            $table->date('birth_date')->nullable();
            $table->tinyInteger('gender')->default(0)->comment('0=未知 1=男 2=女');
            $table->string('id_card', 20)->nullable();
            $table->string('education', 30)->nullable();
            $table->string('emergency_contact', 50)->nullable();
            $table->string('emergency_phone', 20)->nullable();
            $table->decimal('base_salary', 10, 2)->default(0);
            $table->decimal('position_salary', 10, 2)->default(0);
            $table->tinyInteger('status')->default(1)->comment('1=在职 2=试用 3=离职');
            $table->date('left_date')->nullable();
            $table->timestamps();
        });

        // 排班
        Schema::create('schedules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->date('schedule_date');
            $table->tinyInteger('shift_type')->default(1)->comment('1=早班 2=中班 3=晚班 4=夜班 5=休息');
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index(['store_id', 'user_id', 'schedule_date']);
        });

        // 考勤记录
        Schema::create('attendance_records', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->date('attendance_date');
            $table->time('clock_in')->nullable();
            $table->time('clock_out')->nullable();
            $table->tinyInteger('status')->default(1)->comment('1=正常 2=迟到 3=早退 4=旷工 5=请假');
            $table->decimal('overtime_hours', 5, 1)->default(0);
            $table->string('remark')->nullable();
            $table->timestamps();
            $table->index(['store_id', 'user_id', 'attendance_date']);
        });

        // 请假申请
        Schema::create('leave_requests', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->tinyInteger('type')->comment('1=年假 2=事假 3=病假 4=调休 5=产假 6=婚假');
            $table->date('start_date');
            $table->date('end_date');
            $table->decimal('days', 4, 1);
            $table->text('reason')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0=待审批 1=已批准 2=已拒绝');
            $table->unsignedBigInteger('audit_user_id')->nullable();
            $table->timestamp('audit_at')->nullable();
            $table->timestamps();
        });

        // 工资记录
        Schema::create('salary_records', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('year_month', 7)->comment('YYYY-MM');
            $table->decimal('base_salary', 10, 2)->default(0);
            $table->decimal('position_salary', 10, 2)->default(0);
            $table->decimal('performance', 10, 2)->default(0);
            $table->decimal('overtime_pay', 10, 2)->default(0);
            $table->decimal('subsidy', 10, 2)->default(0);
            $table->decimal('social_insurance', 10, 2)->default(0);
            $table->decimal('housing_fund', 10, 2)->default(0);
            $table->decimal('tax', 10, 2)->default(0);
            $table->decimal('absence_deduction', 10, 2)->default(0);
            $table->decimal('other_deduction', 10, 2)->default(0);
            $table->decimal('actual_amount', 10, 2)->default(0);
            $table->tinyInteger('status')->default(0)->comment('0=待确认 1=已确认 2=已发放');
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
            $table->unique(['store_id', 'user_id', 'year_month']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('salary_records');
        Schema::dropIfExists('leave_requests');
        Schema::dropIfExists('attendance_records');
        Schema::dropIfExists('schedules');
        Schema::dropIfExists('employee_profiles');
    }
};
