<?php

namespace Tests\Feature;

use App\Models\CampaignPerformanceSuggestion;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\CreatesAdopsWhaleTables;
use Tests\TestCase;

class CampaignPerformanceAdopsPageTest extends TestCase
{
    use CreatesAdopsWhaleTables;
    use RefreshDatabase;

    public function test_the_list_includes_whale_meta_campaigns_that_were_never_created_here(): void
    {
        $this->travelTo('2026-09-22 12:00:00');
        config(['campaign-performance.adops_list_all' => true]);
        $this->createAdopsWhaleTables();
        $user = User::factory()->create();

        $this->seedAdopsCampaignMeta('999888', 'Whale only campaign');
        DB::connection('whale')->table('arb_adops_stats_campaign_daily')->insert([
            'platform' => 'meta',
            'account_id' => 'act_111',
            'campaign_id' => '999888',
            'stat_date' => now()->toDateString(),
            'spend' => 8.50,
            'impressions' => 200,
            'clicks' => 4,
        ]);

        $this->actingAs($user)
            ->get(route('campaigns.performance'))
            ->assertOk()
            ->assertSee('Whale only campaign', false)
            ->assertSee('$8.50', false)
            ->assertSee('Active', false)
            ->assertSee('$20.00/day', false)
            ->assertSee('Impr 7d', false)
            ->assertSee('2.00%', false);
    }

    public function test_the_list_does_not_call_an_ok_whale_ingest_a_failure(): void
    {
        $this->travelTo('2026-09-22 12:00:00');
        config(['campaign-performance.adops_list_all' => true]);
        $this->createAdopsWhaleTables();
        $user = User::factory()->create();

        $this->seedAdopsCampaignMeta('999888', 'Whale only campaign');
        DB::connection('whale')->table('arb_adops_stats_campaign_daily')->insert([
            'platform' => 'meta',
            'account_id' => 'act_111',
            'campaign_id' => '999888',
            'stat_date' => now()->toDateString(),
            'spend' => 8.50,
            'impressions' => 200,
            'clicks' => 4,
        ]);
        DB::connection('whale')->table('arb_adops_sync_status')->insert([
            'platform' => 'meta',
            'account_id' => 'act_111',
            'dataset' => 'hourly',
            'status' => 'ok',
            'last_success_at' => now()->subHours(5),
            'data_through' => now()->subHours(5)->toDateTimeString(),
            'error' => null,
        ]);
        CampaignPerformanceSuggestion::create([
            'user_id' => $user->id,
            'platform' => 'meta',
            'external_campaign_id' => '999888',
            'status' => 'no_action',
            'source' => 'php',
            'reason' => 'Keep watching.',
            'action_state' => 'pending',
            'snapshot' => [
                'campaign' => 'Whale only campaign',
                'sync_failed' => true,
                'sync_error' => 'Whale sync is stale or failed.',
                'campaign_profile' => ['account_id' => 'act_111'],
            ],
            'suggested_at' => now(),
        ]);

        $this->actingAs($user)
            ->get(route('campaigns.performance'))
            ->assertOk()
            ->assertDontSee('Last sync failed.', false);
    }

    public function test_the_list_shows_a_failed_whale_ingest(): void
    {
        $this->travelTo('2026-09-22 12:00:00');
        config(['campaign-performance.adops_list_all' => true]);
        $this->createAdopsWhaleTables();
        $user = User::factory()->create();

        $this->seedAdopsCampaignMeta('999888', 'Whale only campaign');
        DB::connection('whale')->table('arb_adops_stats_campaign_daily')->insert([
            'platform' => 'meta',
            'account_id' => 'act_111',
            'campaign_id' => '999888',
            'stat_date' => now()->toDateString(),
            'spend' => 8.50,
            'impressions' => 200,
            'clicks' => 4,
        ]);
        DB::connection('whale')->table('arb_adops_sync_status')->insert([
            'platform' => 'meta',
            'account_id' => 'act_111',
            'dataset' => 'hourly',
            'status' => 'failed',
            'last_success_at' => now()->subHours(5),
            'data_through' => now()->subHours(5)->toDateTimeString(),
            'error' => 'Meta API error 80004',
        ]);

        $this->actingAs($user)
            ->get(route('campaigns.performance'))
            ->assertOk()
            ->assertSee('Last sync failed.', false)
            ->assertSee('Meta API error 80004', false);
    }
}
