<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('campaigns', function (Blueprint $table) {
            $table->string('category')->default('other')->after('featured');
            $table->string('icon_path')->nullable()->after('category');
            $table->text('direct_url')->nullable()->after('icon_path');
            $table->string('direct_launch_mode')->default('iframe')->after('direct_url');
        });

        Schema::table('offerwall_clicks', function (Blueprint $table) {
            $table->foreignId('campaign_id')->nullable()->after('offerwall_id')->constrained()->nullOnDelete();
            $table->string('offer_name')->nullable()->after('provider_name');
            $table->text('offer_url')->nullable()->after('offer_name');
            $table->index(['campaign_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::table('offerwall_clicks', function (Blueprint $table) {
            $table->dropIndex(['campaign_id', 'created_at']);
            $table->dropConstrainedForeignId('campaign_id');
            $table->dropColumn(['offer_name', 'offer_url']);
        });

        Schema::table('campaigns', function (Blueprint $table) {
            $table->dropColumn(['category', 'icon_path', 'direct_url', 'direct_launch_mode']);
        });
    }
};
