<?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->decimal('actual_revenue', 15, 2)->default(0)->after('expected_revenue');
            $table->string('lead_source')->nullable()->after('actual_revenue');
        });
        Schema::create('campaign_targets', function (Blueprint $table) {
            $table->id(); $table->foreignId('business_id')->index(); $table->foreignId('campaign_id')->index();
            $table->string('target_type'); $table->unsignedBigInteger('target_id');
            $table->string('delivery_status')->default('pending')->index();
            $table->string('response_status')->nullable()->index();
            $table->boolean('converted')->default(false)->index();
            $table->string('converted_type')->nullable(); $table->unsignedBigInteger('converted_id')->nullable();
            $table->decimal('conversion_value', 15, 2)->default(0);
            $table->string('provider')->nullable(); $table->string('external_id')->nullable();
            $table->timestamp('sent_at')->nullable(); $table->timestamp('responded_at')->nullable();
            $table->timestamp('converted_at')->nullable(); $table->timestamps(); $table->softDeletes();
            $table->unique(['campaign_id','target_type','target_id'], 'campaign_target_unique');
            $table->index(['target_type','target_id']);
        });
    }
    public function down(): void {
        Schema::dropIfExists('campaign_targets');
        Schema::table('campaigns', fn (Blueprint $table) => $table->dropColumn(['actual_revenue','lead_source']));
    }
};
