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

            if (! Schema::hasColumn('report_schedules', 'include_csv')) {
                $table->boolean('include_csv')->default(false)->after('include_excel');
            }
        });

        Schema::table('email_logs', function (Blueprint $table) {
            if (! Schema::hasColumn('email_logs', 'attachment_paths')) {
                $table->json('attachment_paths')->nullable()->after('provider_message_id');
            }

            if (! Schema::hasColumn('email_logs', 'meta')) {
                $table->json('meta')->nullable()->after('attachment_paths');
            }
        });
    }

    public function down(): void
    {
        Schema::table('email_logs', function (Blueprint $table) {
            if (Schema::hasColumn('email_logs', 'meta')) {
                $table->dropColumn('meta');
            }

            if (Schema::hasColumn('email_logs', 'attachment_paths')) {
                $table->dropColumn('attachment_paths');
            }
        });

        Schema::table('report_schedules', function (Blueprint $table) {
            if (Schema::hasColumn('report_schedules', 'include_csv')) {
                $table->dropColumn('include_csv');
            }

            if (Schema::hasColumn('report_schedules', 'channels')) {
                $table->dropColumn('channels');
            }
        });
    }
};
