<?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('attendance_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->date('date')->comment('考勤日期');
            $table->dateTime('check_in_time')->nullable()->comment('签到时间');
            $table->dateTime('check_out_time')->nullable()->comment('签退时间');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态:1正常,2迟到,3早退,4缺勤,5请假,6出差,7加班,8休息');
            $table->unsignedInteger('late_minutes')->default(0)->comment('迟到分钟数');
            $table->unsignedInteger('early_minutes')->default(0)->comment('早退分钟数');
            $table->decimal('work_hours', 4, 1)->default(0)->comment('工作时长');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['employee_id', 'date'], 'uk_employee_date');
            $table->index('date');
            $table->index('status');
        });

        // 请假申请
        Schema::create('leave_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('type')->comment('请假类型');
            $table->dateTime('start_time')->comment('开始时间');
            $table->dateTime('end_time')->comment('结束时间');
            $table->decimal('duration', 4, 1)->default(0)->comment('请假时长(天)');
            $table->string('reason', 500)->nullable()->comment('请假原因');
            $table->unsignedTinyInteger('approval_status')->default(1)->comment('审批状态');
            $table->foreignId('approver_id')->nullable()->constrained('employees')->nullOnDelete();
            $table->string('approval_opinion', 500)->nullable()->comment('审批意见');
            $table->dateTime('approved_at')->nullable()->comment('审批时间');
            $table->timestamps();
            $table->softDeletes();

            $table->index('employee_id');
            $table->index('approval_status');
            $table->index('start_time');
        });

        // 加班申请
        Schema::create('overtime_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->date('date')->comment('加班日期');
            $table->dateTime('start_time')->comment('开始时间');
            $table->dateTime('end_time')->comment('结束时间');
            $table->decimal('duration', 4, 1)->default(0)->comment('加班时长(小时)');
            $table->string('reason', 500)->nullable()->comment('加班原因');
            $table->unsignedTinyInteger('approval_status')->default(1)->comment('审批状态');
            $table->foreignId('approver_id')->nullable()->constrained('employees')->nullOnDelete();
            $table->string('compensatory_type', 20)->default('pay')->comment('补偿方式:pay薪资,compensatory调休');
            $table->timestamps();
            $table->softDeletes();

            $table->index('employee_id');
            $table->index('date');
            $table->index('approval_status');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('overtime_requests');
        Schema::dropIfExists('leave_requests');
        Schema::dropIfExists('attendance_records');
    }
};
