<?php

namespace Tests\Unit;

use App\Campaigns\CampaignPerformanceWhaleEnrichment;
use App\Campaigns\PerformanceVerdict;
use App\Campaigns\PerformanceWhaleVerdictOverlay;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class CampaignPerformanceWhaleEnrichmentTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        Config::set('campaign-performance.whale_enabled', true);

        if (! Schema::connection('whale')->hasTable('arb_adops_campaign_performance')) {
            Schema::connection('whale')->create('arb_adops_campaign_performance', function (Blueprint $table): void {
                $table->unsignedBigInteger('id')->primary();
                $table->date('date');
                $table->string('campaign_id', 32);
                $table->unsignedInteger('clicks');
                $table->decimal('sum_cpc', 12, 4);
                $table->decimal('orders', 12, 4);
                $table->decimal('net_revenue', 12, 4);
                $table->json('debug_json')->nullable();
            });
        }
    }

    public function test_it_enriches_snapshot_when_campaign_id_matches(): void
    {
        $this->travelTo('2026-09-22 12:00:00');

        DB::connection('whale')->table('arb_adops_campaign_performance')->insert([
            'id' => 105,
            'date' => '2026-09-19',
            'campaign_id' => '120251915605170683',
            'clicks' => 3,
            'sum_cpc' => 0.15,
            'orders' => 3,
            'net_revenue' => 89.44,
            'debug_json' => null,
        ]);

        $enrichment = app(CampaignPerformanceWhaleEnrichment::class);
        $snapshot = $enrichment->enrichSnapshot([], '120251915605170683');

        $this->assertTrue($snapshot['affiliate']['matched']);
        $this->assertSame(89.44, $snapshot['affiliate']['7d']['net_revenue']);
    }

    public function test_it_skips_when_no_matching_rows(): void
    {
        $enrichment = app(CampaignPerformanceWhaleEnrichment::class);
        $snapshot = $enrichment->enrichSnapshot([], '999999999999999999');

        $this->assertFalse($snapshot['affiliate']['matched']);
    }

    public function test_overlay_softens_pause_when_revenue_exists(): void
    {
        $overlay = app(PerformanceWhaleVerdictOverlay::class);

        [$verdict, $reason] = $overlay->apply([
            'affiliate' => [
                'matched' => true,
                '7d' => ['net_revenue' => 89.44, 'orders' => 3],
            ],
            'last_7_days' => ['spend' => 42],
            'rules' => ['zero_click_spend_floor' => 10],
        ], PerformanceVerdict::Pause, 'Pause suggestion');

        $this->assertSame(PerformanceVerdict::Continue, $verdict);
        $this->assertStringContainsString('89.44', $reason);
    }

    public function test_overlay_flags_spend_without_revenue(): void
    {
        $overlay = app(PerformanceWhaleVerdictOverlay::class);

        [$verdict, $reason] = $overlay->apply([
            'affiliate' => [
                'matched' => true,
                '7d' => ['net_revenue' => 0, 'orders' => 0],
            ],
            'last_7_days' => ['spend' => 42],
            'rules' => ['zero_click_spend_floor' => 10],
        ], PerformanceVerdict::Continue, 'Keep running');

        $this->assertSame(PerformanceVerdict::ActionRequired, $verdict);
        $this->assertStringContainsString('investigate', strtolower($reason));
    }
}
