<?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('mailings', function (Blueprint $table) {
            // Add updated_by_user_id column
            $table->unsignedBigInteger('updated_by_user_id')->nullable()->after('status');
            $table->foreign('updated_by_user_id')->references('id')->on('users')->onDelete('set null');

            // Drop old created_by columns
            $table->dropColumn(['created_by_id', 'created_by_type']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('mailings', function (Blueprint $table) {
            // Restore created_by columns
            $table->unsignedBigInteger('created_by_id')->nullable();
            $table->string('created_by_type')->nullable();

            // Drop updated_by_user_id
            $table->dropForeign(['updated_by_user_id']);
            $table->dropColumn('updated_by_user_id');
        });
    }
};
