<?php

namespace Tests\Unit\Services\AI;

use App\Campaigns\PerformanceAdvice;
use App\Campaigns\PerformanceVerdict;
use App\Services\AI\CampaignPerformanceAdvisor;
use Laravel\Ai\StructuredAnonymousAgent;
use Tests\TestCase;

class CampaignPerformanceAdvisorTest extends TestCase
{
    public function test_it_keeps_a_pause_reason_that_uses_snapshot_spend(): void
    {
        config(['campaign-performance.llm_enabled' => true, 'campaign-performance.skip_llm_while_learning' => false]);

        StructuredAnonymousAgent::fake([
            [
                'reason' => '$12.10 spend and 0 clicks. Pause this campaign.',
                'confidence' => 'high',
            ],
        ]);

        $php = new PerformanceAdvice(PerformanceVerdict::Pause, 'PHP reason', 'php');
        $advice = (new CampaignPerformanceAdvisor)->explain($this->snapshot(), $php);

        $this->assertSame('llm', $advice->source);
        $this->assertSame(PerformanceVerdict::Pause, $advice->status);
        $this->assertStringContainsString('$12.10', $advice->reason);
    }

    public function test_it_keeps_the_php_verdict_even_when_the_model_tries_to_change_it(): void
    {
        config(['campaign-performance.llm_enabled' => true, 'campaign-performance.skip_llm_while_learning' => false]);

        StructuredAnonymousAgent::fake([
            [
                'reason' => '$12.10 spend looks healthy. Keep running.',
                'confidence' => 'high',
            ],
        ]);

        $php = new PerformanceAdvice(PerformanceVerdict::Pause, 'PHP pause reason', 'php');
        $advice = (new CampaignPerformanceAdvisor)->explain($this->snapshot(), $php);

        $this->assertSame('llm', $advice->source);
        $this->assertSame(PerformanceVerdict::Pause, $advice->status);
    }

    public function test_it_does_not_call_the_agent_when_llm_is_disabled(): void
    {
        config(['campaign-performance.llm_enabled' => false]);

        StructuredAnonymousAgent::fake([
            ['reason' => 'should not run', 'confidence' => 'high'],
        ]);

        $php = new PerformanceAdvice(PerformanceVerdict::Pause, 'PHP pause reason', 'php');
        $advice = (new CampaignPerformanceAdvisor)->explain($this->snapshot(), $php);

        $this->assertSame('php', $advice->source);
        StructuredAnonymousAgent::assertPromptedTimes(0);
    }

    public function test_an_invented_amount_is_discarded(): void
    {
        config(['campaign-performance.llm_enabled' => true, 'campaign-performance.skip_llm_while_learning' => false]);

        StructuredAnonymousAgent::fake([
            [
                'reason' => '$9999 spend. Pause this campaign.',
                'confidence' => 'high',
            ],
        ]);

        $php = new PerformanceAdvice(PerformanceVerdict::Pause, 'PHP pause reason', 'php');
        $advice = (new CampaignPerformanceAdvisor)->explain($this->snapshot(), $php);

        $this->assertSame('php', $advice->source);
        $this->assertSame('PHP pause reason', $advice->reason);
    }

    public function test_it_skips_the_agent_for_no_action_verdicts_while_learning(): void
    {
        config(['campaign-performance.llm_enabled' => true, 'campaign-performance.skip_llm_while_learning' => true]);

        StructuredAnonymousAgent::fake([
            ['reason' => 'should not run', 'confidence' => 'high'],
        ]);

        $php = new PerformanceAdvice(PerformanceVerdict::NoAction, 'Still learning.', 'php');
        $advice = (new CampaignPerformanceAdvisor)->explain($this->snapshot(), $php);

        $this->assertSame('php', $advice->source);
        StructuredAnonymousAgent::assertPromptedTimes(0);
    }

    /**
     * @return array<string, mixed>
     */
    private function snapshot(): array
    {
        return [
            'campaign' => 'Italy packages',
            'platform' => 'meta',
            'platform_status' => 'ACTIVE',
            'daily_budget' => 20,
            'days_live' => 10,
            'learning_window_days' => 3,
            'max_test_spend' => 40,
            'last_7_days' => [
                'spend' => 12.1,
                'impressions' => 800,
                'clicks' => 0,
                'cpc' => null,
                'ctr' => null,
            ],
            'last_7_hours' => [
                'spend' => 3.5,
                'impressions' => 200,
                'clicks' => 0,
                'cpc' => null,
                'ctr' => null,
                'hours' => 5,
            ],
            'yesterday' => ['spend' => 12.1, 'impressions' => 400, 'clicks' => 0],
            'rules' => ['zero_click_spend_floor' => 10, 'max_cpc' => 2.0],
            'final_verdict' => 'pause',
            'final_reason' => '$12.10 spend and 0 clicks over 7 days. Suggestion: pause this campaign.',
            'allowed_verdicts' => ['no_action', 'continue', 'pause', 'stop', 'start', 'action_required'],
        ];
    }
}
