<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    protected $connection = 'sam_stat';

    public function up(): void
    {
        Schema::connection($this->connection)->create('stat_hr_attendance_daily', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->date('stat_date');

            // 근태
            $table->unsignedInteger('total_employees')->default(0)->comment('전체 직원 수');
            $table->unsignedInteger('attendance_count')->default(0)->comment('출근 인원');
            $table->unsignedInteger('late_count')->default(0)->comment('지각');
            $table->unsignedInteger('absent_count')->default(0)->comment('결근');
            $table->decimal('attendance_rate', 5, 2)->default(0)->comment('출근율 (%)');

            // 휴가
            $table->unsignedInteger('leave_count')->default(0)->comment('휴가 사용');
            $table->unsignedInteger('leave_annual_count')->default(0)->comment('연차');
            $table->unsignedInteger('leave_sick_count')->default(0)->comment('병가');
            $table->unsignedInteger('leave_other_count')->default(0)->comment('기타');

            // 초과근무
            $table->decimal('overtime_hours', 10, 2)->default(0);
            $table->unsignedInteger('overtime_employee_count')->default(0);

            // 인건비
            $table->decimal('total_labor_cost', 18, 2)->default(0);

            $table->timestamps();

            $table->unique(['tenant_id', 'stat_date'], 'uk_tenant_date');
            $table->index('stat_date');
        });
    }

    public function down(): void
    {
        Schema::connection($this->connection)->dropIfExists('stat_hr_attendance_daily');
    }
};