<?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('attendances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('program_id')->constrained('programs')->cascadeOnDelete();
            $table->foreignId('participant_id')->constrained('participants')->cascadeOnDelete();
            $table->foreignId('program_participant_id')->nullable()->constrained('program_participants')->nullOnDelete();
            $table->enum('attendance_source', ['pre_registered_staff', 'walk_in_external', 'admin_manual']);
            $table->enum('attendance_session', ['pagi', 'petang', 'full_day']);
            $table->timestamp('checked_in_at');
            $table->string('checked_in_ip', 45)->nullable();
            $table->string('user_agent', 500)->nullable();
            $table->string('notes', 500)->nullable();
            $table->timestamps();

            $table->unique(['program_id', 'participant_id']);
            $table->index('program_id');
            $table->index('participant_id');
            $table->index('attendance_source');
            $table->index('attendance_session');
        });
    }

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