<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Tracks background-generated report PDFs. Large per-member reports are rendered
 * by a queued job to storage; this row records the request, its status and the
 * resulting file so the UI can poll and offer a download.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('report_files', function (Blueprint $table) {
            $table->id();
            $table->string('key');                       // report type, e.g. deduction-schedules
            $table->foreignId('payroll_run_id')->nullable()->constrained()->cascadeOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained()->cascadeOnDelete();
            $table->unsignedSmallInteger('year')->nullable();
            $table->string('status')->default('PENDING'); // PENDING | PROCESSING | READY | FAILED
            $table->string('path')->nullable();          // storage path when READY
            $table->string('filename')->nullable();      // download filename
            $table->unsignedBigInteger('size')->nullable();
            $table->text('error')->nullable();
            $table->foreignId('requested_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['payroll_run_id', 'key']);
            $table->index(['employee_id', 'key', 'year']);
        });
    }

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