<?php

namespace Tests\Unit;

use App\Library\CampaignDecision\CampaignMarketingDiagnosis;
use Tests\TestCase;

class CampaignMarketingDiagnosisTest extends TestCase
{
    public function test_clicks_without_results_point_at_the_landing_page(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 17.9, 'impressions' => 800, 'clicks' => 10],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'spend' => 70.17,
                    'platform_spend' => 70.17,
                    'orders' => 0,
                    'revenue' => 0,
                    'conversions' => 0,
                    'leads' => 0,
                ],
            ],
            'creative_context' => [
                'found' => true,
                'landing_urls' => ['https://lucos.com/landing?utm_source=meta'],
            ],
        ]);

        $this->assertSame('fix_landing_page', $call['primary_lever']);
        $this->assertSame('no', $call['spend_more']);
        $this->assertStringContainsString('lucos.com/landing', $call['summary']);
        $this->assertStringContainsString('70.17', $call['summary']);
    }

    public function test_impressions_with_no_clicks_point_at_the_creative(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 12.1, 'impressions' => 800, 'clicks' => 0],
            ],
            'channels' => ['outcome' => ['available' => false]],
        ]);

        $this->assertSame('improve_creative', $call['primary_lever']);
        $this->assertSame('not_tested', $call['landing_page']);
        $this->assertSame('no', $call['spend_more']);
    }

    public function test_a_profitable_campaign_can_spend_more(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'scale_up',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 80, 'impressions' => 4000, 'clicks' => 120],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'platform_spend' => 80,
                    'revenue' => 200,
                    'orders' => 4,
                ],
            ],
        ]);

        $this->assertSame('spend_more', $call['primary_lever']);
        $this->assertSame('yes', $call['spend_more']);
    }

    public function test_missing_platform_stats_do_not_pick_a_lever(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'data_issue',
            'capabilities' => ['platform_metrics_available' => false],
        ]);

        $this->assertSame('hold', $call['primary_lever']);
        $this->assertStringContainsString('Platform stats are missing', $call['summary']);
    }

    public function test_a_thin_seven_day_window_falls_back_to_fifteen_days_for_creative(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 2, 'impressions' => 40, 'clicks' => 0],
                '15d' => ['spend' => 30, 'impressions' => 900, 'clicks' => 0],
            ],
            'channels' => ['outcome' => ['available' => false]],
        ]);

        $this->assertSame('15d', $call['window_used']);
        $this->assertSame('improve_creative', $call['primary_lever']);
        $this->assertStringContainsString('15 days', $call['plan'][0]);
    }

    public function test_the_plan_names_how_many_of_the_last_30_days_were_on_file(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 12.1, 'impressions' => 800, 'clicks' => 0],
            ],
            'channels' => ['outcome' => ['available' => false]],
            'daily_history' => [
                'from' => '2026-08-24',
                'to' => '2026-09-22',
                'expected_days' => 30,
                'days_on_file' => 2,
            ],
        ]);

        $this->assertSame('7d', $call['lookup_basis']);
        $this->assertSame(
            'Looked at 2 of the last 30 days, from 2026-08-24 to 2026-09-22, and decided from the last 7 days.',
            $call['plan'][0],
        );
    }

    public function test_a_thin_week_names_the_15_day_lookup_when_30_days_are_on_file(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 2, 'impressions' => 40, 'clicks' => 0],
                '15d' => ['spend' => 30, 'impressions' => 900, 'clicks' => 0],
            ],
            'channels' => ['outcome' => ['available' => false]],
            'daily_history' => [
                'from' => '2026-08-24',
                'to' => '2026-09-22',
                'expected_days' => 30,
                'days_on_file' => 30,
            ],
        ]);

        $this->assertSame('15d', $call['lookup_basis']);
        $this->assertSame(
            'Looked at the last 30 days, from 2026-08-24 to 2026-09-22, and decided from the last 15 days because the last 7 days were too thin.',
            $call['plan'][0],
        );
    }

    public function test_a_substantial_seven_day_window_ignores_fifteen_days(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'pause',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 12.1, 'impressions' => 800, 'clicks' => 0],
                '15d' => ['spend' => 80, 'impressions' => 4000, 'clicks' => 120],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'platform_spend' => 80,
                    'revenue' => 200,
                    'orders' => 4,
                ],
            ],
        ]);

        $this->assertSame('7d', $call['window_used']);
        $this->assertSame('improve_creative', $call['primary_lever']);
    }

    public function test_decrease_budget_with_landing_page_keeps_the_budget_action_in_line_two(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'decrease_budget',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 17.9, 'impressions' => 800, 'clicks' => 10],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'spend' => 70.17,
                    'platform_spend' => 70.17,
                    'orders' => 0,
                    'revenue' => 0,
                ],
            ],
        ]);

        $this->assertSame('fix_landing_page', $call['primary_lever']);
        $this->assertStringContainsString('Decrease the daily budget by 10%', $call['plan'][1]);
        $this->assertStringContainsString('fix the landing page', $call['plan'][1]);
    }

    public function test_a_budget_cut_names_the_new_daily_budget(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'decrease_budget',
            'recommended_budget_change_percent' => -10,
            'daily_budget' => 80,
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 17.9, 'impressions' => 800, 'clicks' => 10],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'spend' => 70.17,
                    'platform_spend' => 70.17,
                    'orders' => 0,
                    'revenue' => 0,
                ],
            ],
        ]);

        $this->assertStringContainsString(
            'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day)',
            $call['plan'][1],
        );
    }

    public function test_both_windows_thin_returns_hold(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'watch',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 1, 'impressions' => 20, 'clicks' => 0],
                '15d' => ['spend' => 4, 'impressions' => 80, 'clicks' => 1],
            ],
            'channels' => ['outcome' => ['available' => false]],
        ]);

        $this->assertSame('hold', $call['primary_lever']);
        $this->assertStringContainsString('Leave the budget unchanged until the next check.', $call['plan'][1]);
    }

    public function test_learning_with_thin_clicks_and_no_results_preps_the_landing_page(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'learning',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 8, 'impressions' => 200, 'clicks' => 5],
                '15d' => ['spend' => 10, 'impressions' => 250, 'clicks' => 6],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'platform_spend' => 8,
                    'orders' => 0,
                    'revenue' => 0,
                ],
            ],
            'creative_context' => [
                'found' => true,
                'landing_urls' => ['https://example.com/offer'],
            ],
        ]);

        $this->assertSame('fix_landing_page', $call['primary_lever']);
        $this->assertSame('learning_prep', $call['lookup_basis']);
        $this->assertSame('no', $call['spend_more']);
        $this->assertStringContainsString('While learning', $call['summary']);
        $this->assertStringContainsString('page owner', strtolower($call['summary']));
        $this->assertStringContainsString('example.com/offer', $call['summary']);
    }

    public function test_learning_with_impressions_and_no_clicks_preps_creative(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'learning',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 5, 'impressions' => 300, 'clicks' => 0],
                '15d' => ['spend' => 8, 'impressions' => 400, 'clicks' => 0],
            ],
            'channels' => ['outcome' => ['available' => false]],
        ]);

        $this->assertSame('improve_creative', $call['primary_lever']);
        $this->assertSame('learning_prep', $call['lookup_basis']);
        $this->assertStringContainsString('While learning', $call['summary']);
        $this->assertStringContainsString('creative', strtolower($call['summary']));
    }

    public function test_learning_does_not_recommend_spend_more_even_when_early_roas_looks_strong(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'learning',
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => ['spend' => 80, 'impressions' => 4000, 'clicks' => 120],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'platform_spend' => 80,
                    'revenue' => 200,
                    'orders' => 4,
                ],
            ],
        ]);

        $this->assertSame('hold', $call['primary_lever']);
        $this->assertSame('no', $call['spend_more']);
        $this->assertStringContainsString('While learning', $call['summary']);
        $this->assertStringContainsString('budget steady', strtolower($call['summary']));
    }

    public function test_a_traffic_campaign_with_landing_views_is_not_a_page_fix(): void
    {
        $call = (new CampaignMarketingDiagnosis)->fromSnapshot([
            'verdict' => 'learning',
            'campaign_profile' => ['objective' => 'OUTCOME_TRAFFIC'],
            'capabilities' => ['platform_metrics_available' => true],
            'windows' => [
                '7d' => [
                    'spend' => 198.92,
                    'impressions' => 38000,
                    'clicks' => 753,
                    'outbound_clicks' => 753,
                ],
            ],
            'channels' => [
                'outcome' => [
                    'available' => true,
                    'platform_spend' => 198.92,
                    'orders' => 0,
                    'revenue' => 0,
                    'conversions' => 0,
                ],
            ],
        ]);

        $this->assertSame('hold', $call['primary_lever']);
        $this->assertStringContainsString('do not rewrite', strtolower($call['summary']));
        $this->assertStringNotContainsString('fix the landing page', strtolower($call['summary']));
    }
}
