<?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('attendance_details', function (Blueprint $table) {
            $table->id();

            // Parent
            $table->foreignId('attendance_id')->constrained('attendance')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();

            // Shift actually used (segment / calendar / HR override)
            $table->foreignId('shift_id')->nullable()->constrained()->nullOnDelete();

            // FINAL attendance status
            $table->string('attendance_status', 10)->default('A');
            /*
                A, P, DF, H,
                HP+HA, HA+HP,
                HP+HDF, HDF+HP,
                HA+HDF, HDF+HA
            */

            // Leave annotation (NOT attendance)
            $table->string('leave_code')->nullable();   // e.g. L(p), L(np)
            $table->boolean('leave_affects_pay')->default(false);

            // Main in/out (derived or corrected)
            $table->dateTime('in_time')->nullable();
            $table->dateTime('out_time')->nullable();

            // Calculated minutes
            $table->unsignedSmallInteger('late_minutes')->default(0);
            $table->unsignedSmallInteger('early_minutes')->default(0);
            $table->unsignedSmallInteger('short_leave_minutes')->default(0);

            // OT / DOT
            $table->unsignedSmallInteger('ot_minutes')->default(0);
            $table->unsignedSmallInteger('dot_minutes')->default(0);
            $table->boolean('remove_ot')->default(false);

            // UI flags
            $table->boolean('lunch_taken')->default(false);
            $table->boolean('dinner_taken')->default(false);

            // Audit
            $table->boolean('is_manual')->default(false);
            $table->text('remarks')->nullable();

            $table->timestamps();

            $table->unique(['attendance_id', 'employee_id']);
        });




    }

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