<?php

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

// Sender profiles are removed - the default SMTP profile's From details
// now act as the sender identity for campaigns.
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('campaigns', function (Blueprint $table) {
            $table->dropForeign(['sender_profile_id']);
            $table->dropColumn('sender_profile_id');
        });

        Schema::dropIfExists('sender_profiles');
    }

    public function down(): void
    {
        Schema::create('sender_profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('from_name');
            $table->string('from_email');
            $table->string('reply_to')->nullable();
            $table->string('status', 20)->default('pending');
            $table->timestamp('verified_at')->nullable();
            $table->string('verification_token', 80)->nullable()->unique();
            $table->timestamp('verification_sent_at')->nullable();
            $table->timestamp('last_sent_at')->nullable();
            $table->unsignedInteger('campaigns_count')->default(0);
            $table->timestamps();
        });

        Schema::table('campaigns', function (Blueprint $table) {
            $table->unsignedBigInteger('sender_profile_id')->nullable()->after('template_id');

            $table->foreign('sender_profile_id')
                ->references('id')->on('sender_profiles')
                ->nullOnDelete();
        });
    }
};
