<?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::rename('campaigns', 'ad_campaigns');

        Schema::table('ad_campaigns', function (Blueprint $table) {
            $table->foreignId('customer_id')->nullable()->after('id')->constrained()->nullOnDelete();
            $table->foreignId('created_by_user_id')->nullable()->after('customer_id')->constrained('users')->nullOnDelete();
            $table->string('campaign_id')->nullable()->after('created_by_user_id'); // Meta Campaign ID
            $table->renameColumn('name', 'campaign_name');
            $table->renameColumn('age_min', 'age_from');
            $table->renameColumn('age_max', 'age_to');
            $table->renameColumn('call_to_action', 'cta');
            $table->string('media_type')->nullable()->after('destination_url');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('ad_campaigns', function (Blueprint $table) {
            $table->dropForeign(['customer_id']);
            $table->dropForeign(['created_by_user_id']);
            $table->dropColumn(['customer_id', 'created_by_user_id', 'campaign_id', 'media_type']);
            $table->renameColumn('campaign_name', 'name');
            $table->renameColumn('age_from', 'age_min');
            $table->renameColumn('age_to', 'age_max');
            $table->renameColumn('cta', 'call_to_action');
        });

        Schema::rename('ad_campaigns', 'campaigns');
    }
};
