<?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::table('generated_reports', function (Blueprint $table) {
            if (! Schema::hasColumn('generated_reports', 'channels')) {
                $table->json('channels')->nullable()->after('report_type');
            }

            if (! Schema::hasColumn('generated_reports', 'html_path')) {
                $table->string('html_path')->nullable()->after('summary');
            }

            if (! Schema::hasColumn('generated_reports', 'csv_path')) {
                $table->string('csv_path')->nullable()->after('excel_path');
            }

            if (! Schema::hasColumn('generated_reports', 'generated_at')) {
                $table->timestamp('generated_at')->nullable()->after('sent_at');
            }
        });
    }

    public function down(): void
    {
        Schema::table('generated_reports', function (Blueprint $table) {
            foreach (['channels', 'html_path', 'csv_path', 'generated_at'] as $column) {
                if (Schema::hasColumn('generated_reports', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
