<?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('attendance_events', function (Blueprint $table) {
            $table->id();
            $table->uuid('event_uuid')->unique();
            $table->foreignId('employee_id')->constrained('employees')->restrictOnDelete();
            $table->foreignId('device_id')->nullable()->constrained('devices')->nullOnDelete();
            $table->foreignId('site_id')->constrained('sites')->restrictOnDelete();
            $table->enum('direction', ['IN', 'OUT']);
            $table->dateTime('ts');
            $table->float('confidence')->default(0);
            $table->boolean('liveness_pass')->default(true);
            $table->enum('source', ['face', 'manual'])->default('face');
            $table->string('operator')->nullable();
            $table->float('direction_confidence')->default(0);
            $table->foreignId('corrected_by')->nullable()->constrained('attendance_events')->nullOnDelete();
            $table->foreignId('corrects_event_id')->nullable()->constrained('attendance_events')->nullOnDelete();
            $table->string('snapshot_path')->nullable();
            $table->timestamps();

            $table->index(['employee_id', 'ts']);
            $table->index(['site_id', 'ts']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('attendance_events');
    }
};
