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

            $table->foreignId('employee_id')
                ->constrained('employees')
                ->cascadeOnDelete();

            $table->foreignId('device_id')
                ->nullable()
                ->constrained('biometric_devices')
                ->nullOnDelete();

            $table->dateTime('log_time');

            $table->enum('type', ['IN', 'OUT'])->nullable(); //data not coming from biometric device, so nullable not use

            $table->enum('source', [
                'biometric',
                'manual'
            ])->default('biometric');

            $table->string('device_user_id')
                ->nullable();

            $table->string('raw_punch')
                ->nullable();


            $table->boolean('is_processed')
                ->default(false);

            $table->text('raw_payload')
                ->nullable();

            $table->foreignId('processed_by')->nullable();
            $table->timestamp('processed_at')->nullable();

            $table->timestamps();

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

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