<?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('biometric_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('employee_fingerprint_id', 128);
            $table->foreignId('device_id')->nullable()->constrained('biometric_devices')->nullOnDelete();
            $table->timestamp('punch_time');
            $table->json('raw_payload')->nullable();
            $table->timestamp('processed_at')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'processed_at']);
            $table->index(
                ['tenant_id', 'employee_fingerprint_id', 'punch_time'],
                'biometric_logs_tenant_fp_time_idx'
            );
        });
    }

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