<?php

namespace Tests\Unit;

use App\Campaigns\CampaignReviewSnapshot;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class CampaignReviewSnapshotTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_attaches_ad_sets_regions_and_creative_copy(): void
    {
        Schema::connection('whale')->create('arb_adops_adsets', function (Blueprint $table): void {
            $table->string('platform');
            $table->string('campaign_id');
            $table->string('adset_id');
            $table->string('name')->nullable();
            $table->string('effective_status')->nullable();
            $table->decimal('daily_budget', 12, 2)->nullable();
            $table->string('optimization_goal')->nullable();
            $table->string('billing_event')->nullable();
        });
        Schema::connection('whale')->create('arb_adops_ads', function (Blueprint $table): void {
            $table->string('platform');
            $table->string('campaign_id');
            $table->string('ad_id');
            $table->string('creative_id')->nullable();
            $table->string('final_url')->nullable();
        });
        Schema::connection('whale')->create('arb_adops_creatives', function (Blueprint $table): void {
            $table->string('platform');
            $table->string('creative_id');
            $table->text('primary_text')->nullable();
            $table->text('headlines')->nullable();
        });
        $geo = 'arb_adops_stats_geo_daily_'.now()->format('Ym');
        Schema::connection('whale')->create($geo, function (Blueprint $table): void {
            $table->string('platform');
            $table->string('campaign_id');
            $table->date('stat_date');
            $table->string('region')->nullable();
            $table->decimal('spend', 12, 2)->default(0);
            $table->integer('impressions')->default(0);
            $table->integer('clicks')->default(0);
        });

        DB::connection('whale')->table('arb_adops_adsets')->insert([
            'platform' => 'meta',
            'campaign_id' => 'camp-9',
            'adset_id' => 'set-1',
            'name' => 'US broad',
            'effective_status' => 'ACTIVE',
            'daily_budget' => 25,
            'optimization_goal' => 'LINK_CLICKS',
            'billing_event' => 'IMPRESSIONS',
        ]);
        DB::connection('whale')->table('arb_adops_ads')->insert([
            'platform' => 'meta',
            'campaign_id' => 'camp-9',
            'ad_id' => 'ad-1',
            'creative_id' => 'cr-1',
            'final_url' => 'https://example.com/offer',
        ]);
        DB::connection('whale')->table('arb_adops_creatives')->insert([
            'platform' => 'meta',
            'creative_id' => 'cr-1',
            'primary_text' => 'See the offer.',
            'headlines' => json_encode([['text' => 'Offer headline']]),
        ]);
        DB::connection('whale')->table($geo)->insert([
            'platform' => 'meta',
            'campaign_id' => 'camp-9',
            'stat_date' => now()->subDay()->toDateString(),
            'region' => 'California',
            'spend' => 18.25,
            'impressions' => 900,
            'clicks' => 14,
        ]);

        $snapshot = app(CampaignReviewSnapshot::class)->attach([
            'campaign_profile' => ['name' => 'Offer'],
        ], 'meta', 'camp-9');

        $this->assertSame('Offer headline', $snapshot['creative_context']['ads'][0]['headline']);
        $this->assertSame('US broad', $snapshot['ad_sets'][0]['name']);
        $this->assertSame('LINK_CLICKS', $snapshot['ad_sets'][0]['optimization_goal']);
        $this->assertSame('California', $snapshot['breakdowns']['regions'][0]['label']);
        $this->assertSame(18.25, $snapshot['breakdowns']['regions'][0]['spend']);
        $this->assertSame('Offer', $snapshot['campaign_profile']['name']);
        $this->assertSame(['LINK_CLICKS'], $snapshot['campaign_goals']['optimization_goals']);
    }

    public function test_it_attaches_the_campaign_goal_and_bid(): void
    {
        Schema::connection('whale')->create('arb_adops_campaigns', function (Blueprint $table): void {
            $table->string('platform');
            $table->string('campaign_id');
            $table->string('objective')->nullable();
            $table->string('bid_strategy')->nullable();
            $table->decimal('target_cpa', 12, 2)->nullable();
            $table->decimal('daily_budget', 12, 2)->nullable();
        });
        Schema::connection('whale')->create('arb_adops_adsets', function (Blueprint $table): void {
            $table->string('platform');
            $table->string('campaign_id');
            $table->string('adset_id');
            $table->string('optimization_goal')->nullable();
        });
        DB::connection('whale')->table('arb_adops_campaigns')->insert([
            'platform' => 'meta',
            'campaign_id' => 'camp-goal',
            'objective' => 'OUTCOME_SALES',
            'bid_strategy' => 'COST_CAP',
            'target_cpa' => 18.5,
            'daily_budget' => 40,
        ]);
        DB::connection('whale')->table('arb_adops_adsets')->insert([
            'platform' => 'meta',
            'campaign_id' => 'camp-goal',
            'adset_id' => 'set-1',
            'optimization_goal' => 'OFFSITE_CONVERSIONS',
        ]);

        $snapshot = app(CampaignReviewSnapshot::class)->attach([], 'meta', 'camp-goal');

        $this->assertSame('OUTCOME_SALES', $snapshot['campaign_goals']['objective']);
        $this->assertSame('COST_CAP', $snapshot['campaign_goals']['bid_strategy']);
        $this->assertSame(18.5, $snapshot['campaign_goals']['target_cpa']);
        $this->assertSame(40.0, $snapshot['campaign_goals']['daily_budget']);
        $this->assertSame(['OFFSITE_CONVERSIONS'], $snapshot['campaign_goals']['optimization_goals']);
    }
}
