<?php

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

return new class extends Migration
{
    public function up(): void
    {
        DB::table('site_settings')->updateOrInsert(
            ['key' => 'email_verification_enabled'],
            ['value' => 'on', 'created_at' => now(), 'updated_at' => now()]
        );

        Schema::create('email_broadcasts', function (Blueprint $table): void {
            $table->id();
            $table->string('subject');
            $table->longText('message');
            $table->string('recipient_group')->default('all_users');
            $table->string('status')->default('draft');
            $table->unsignedInteger('total_recipients')->default(0);
            $table->unsignedInteger('sent_count')->default(0);
            $table->unsignedInteger('failed_count')->default(0);
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();
        });
    }

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

        DB::table('site_settings')
            ->where('key', 'email_verification_enabled')
            ->delete();
    }
};
