<?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('cached_monthly_attendances', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('employee_id');
            $table->string('year_month', 7); // Format: YYYY-MM
            $table->date('start_date');
            $table->date('end_date');
            $table->longText('attendance_data'); // JSON encoded data
            $table->integer('present_days')->default(0);
            $table->timestamp('computed_at')->nullable();
            $table->timestamps();
            
            // Indexes for faster queries
            $table->unique(['employee_id', 'year_month']);
            $table->index('employee_id');
            $table->index('year_month');
            $table->index('computed_at');
            
            // Foreign key (adjust table name as needed)
            // $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
        });
    }

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