<?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
    {
        // Tabel untuk menyimpan data presensi yang sudah diverifikasi (display log)
        Schema::create('attendance_summaries', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('attendance_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('action'); // check_in, check_out, create, update, delete
            $table->date('log_date'); // tanggal absen (sorting key)
            $table->time('log_time')->nullable(); // waktu aksi
            $table->string('type')->nullable(); // office, wfh, business_trip
            $table->string('status')->nullable(); // present, late, absent
            $table->json('details')->nullable(); // data tambahan
            $table->unsignedBigInteger('source_id'); // referensi ke audit_logs.id
            $table->date('period_month'); // periode bulan (untuk tracking transfer)
            $table->timestamp('transferred_at'); // kapan di-transfer
            $table->timestamps();

            $table->index(['user_id', 'log_date']);
            $table->index('log_date');
            $table->index('period_month');
            $table->unique('source_id'); // satu audit log hanya sekali transfer
        });

        // Tabel untuk tracking cutoff transfer
        Schema::create('attendance_periods', function (Blueprint $table) {
            $table->id();
            $table->date('period_month'); // format: 2026-12-01 (bulan yang di-transfer)
            $table->date('cutoff_date'); // tanggal cutoff (misal 2027-01-10)
            $table->timestamp('transferred_at');
            $table->integer('total_records');
            $table->timestamps();

            $table->unique('period_month');
        });
    }

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