<?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
    {
        if (Schema::hasTable('agency_management_reports')) {
            return;
        }

        Schema::create('agency_management_reports', function (Blueprint $table) {
            $table->id();
            $table->string('period_type')->default('weekly')->index();
            $table->date('date_from')->index();
            $table->date('date_to')->index();
            $table->string('status')->default('completed')->index();
            $table->unsignedTinyInteger('overall_score')->default(0);
            $table->json('summary')->nullable();
            $table->json('kpis')->nullable();
            $table->json('risk_clients')->nullable();
            $table->json('top_clients')->nullable();
            $table->json('team_workload')->nullable();
            $table->json('channel_health')->nullable();
            $table->json('recommendations')->nullable();
            $table->string('html_path')->nullable();
            $table->string('csv_path')->nullable();
            $table->timestamp('generated_at')->nullable();
            $table->foreignId('generated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('error_message')->nullable();
            $table->timestamps();

            $table->index(['period_type', 'date_from', 'date_to']);
        });
    }

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