<?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::table('campaigns', function (Blueprint $table) {
            $table->string('frequency', 20)->nullable()->after('type'); // daily | weekly | monthly
            $table->json('recurring_days')->nullable()->after('frequency'); // weekly: [0-6], monthly: [1-31]
            $table->string('recurring_time', 5)->nullable()->after('recurring_days'); // "HH:MM"
            $table->string('recurring_timezone', 64)->nullable()->after('recurring_time');
            $table->boolean('auto_send')->default(true)->after('recurring_timezone');
            $table->date('start_date')->nullable()->after('auto_send');
            $table->string('end_type', 20)->nullable()->default('never')->after('start_date'); // never | date | count
            $table->date('end_date')->nullable()->after('end_type');
            $table->unsignedInteger('end_count')->nullable()->after('end_date');
            $table->unsignedInteger('occurrences_sent')->default(0)->after('end_count');
            $table->timestamp('last_sent_at')->nullable()->after('occurrences_sent');
            $table->timestamp('next_run_at')->nullable()->after('last_sent_at');

            $table->index('next_run_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('campaigns', function (Blueprint $table) {
            $table->dropIndex(['next_run_at']);
            $table->dropColumn([
                'frequency', 'recurring_days', 'recurring_time', 'recurring_timezone',
                'auto_send', 'start_date', 'end_type', 'end_date', 'end_count',
                'occurrences_sent', 'last_sent_at', 'next_run_at',
            ]);
        });
    }
};
