<?php

namespace Tests\Unit;

use App\Campaigns\CampaignPerformanceChatAnswer;
use Tests\TestCase;

class CampaignPerformanceChatAnswerTest extends TestCase
{
    public function test_metrics_come_before_one_engine_decision_without_status_codes(): void
    {
        $answer = CampaignPerformanceChatAnswer::fromHealthPayload([
            'campaign' => 'Attestpath -Retargeting- LP Visitors - 30d-20260921',
            'campaign_id' => '120251938088550626',
            'account_id' => '1950320145707342',
            'platform' => 'meta',
            'platform_status' => 'ACTIVE',
            'verdict' => 'decrease_budget',
            'confidence' => 0.65,
            'saved_status' => 'decrease_budget',
            'ai_status' => 'decrease_budget',
            'decision' => 'Decrease budget: the last 7 hours show $0 spend and 0 clicks.',
            'stored_evaluation' => [
                'verdict' => 'decrease_budget',
                'verdict_reason' => 'No sales. Cost per conversions is $69.83, which is above $4.00.',
            ],
            'live_evaluation' => [
                'verdict' => 'pause',
                'reason' => 'ROI spend in the lagged window has no sales.',
            ],
            'last_7_days' => [
                'from' => '2026-10-01 00:00:00',
                'to' => '2026-10-07 00:00:00',
                'spend' => 17.26,
                'clicks' => 8,
                'impressions' => 183,
            ],
            'last_7_hours' => [
                'from' => '2026-10-07 00:00:00',
                'to' => '2026-10-07 06:00:00',
                'spend' => 0,
                'clicks' => 0,
                'impressions' => 0,
            ],
            'campaign_details' => [
                'outcome_window' => [
                    'from' => '2026-09-29',
                    'to' => '2026-10-05',
                    'spend' => 57.29,
                    'orders' => 0,
                    'sales' => 0,
                    'conversions' => 0,
                    'subscriptions' => 0,
                    'installs' => 0,
                    'bookings' => 0,
                    'leads' => 0,
                ],
            ],
        ]);

        $this->assertSame(trim(<<<'ANSWER'
**Attestpath -Retargeting- LP Visitors - 30d-20260921**

- Campaign ID: 120251938088550626
- Account ID: 1950320145707342
- Platform: Meta
- Platform status: Active

**Meta delivery, last 7 days (2026-10-01 to 2026-10-07)**
- Spend: $17.26
- Clicks: 8
- Impressions: 183

**Meta delivery, last 7 hours (2026-10-07 00:00 to 2026-10-07 06:00)**
- Spend: $0.00
- Clicks: 0
- Impressions: 0

**Lagged outcomes (2026-09-29 to 2026-10-05)**
- Spend: $57.29
- Orders: 0
- Sales: $0.00
- Conversions: 0
- Subscriptions: 0
- Installs: 0
- Bookings: 0
- Leads: 0

**Verdict:** Decrease budget — Consider reducing budget within guardrail limits.

**Confidence:** 0.65

**Why this verdict:**
No sales. Cost per conversions is $69.83, which is above $4.00.

**What to do:**
Decrease budget: the last 7 hours show $0 spend and 0 clicks.

**Saved status:** Decrease budget
**AI status:** Decrease budget

**Live re-check:** Pause — ROI spend in the lagged window has no sales.
ANSWER), $answer);
        $this->assertStringNotContainsString('decrease_budget', $answer);
    }

    public function test_a_vague_budget_decision_is_rewritten_with_the_dollar_change(): void
    {
        $answer = CampaignPerformanceChatAnswer::fromHealthPayload([
            'campaign' => 'Italy packages',
            'verdict' => 'decrease_budget',
            'decision' => 'Decrease budget: the last 7 hours show $0 spend and 0 clicks, while outcome data shows $69.83 spent with 0 orders and $0 revenue.',
            'budget_recommendation' => [
                'summary' => 'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day).',
            ],
        ]);

        $this->assertStringContainsString(
            '**Verdict:** Decrease budget — Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day).',
            $answer,
        );
        $this->assertStringContainsString(
            'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day): the last 7 hours show $0 spend',
            $answer,
        );
    }

    public function test_a_saved_engine_verdict_is_used_when_there_is_no_live_evaluation(): void
    {
        $answer = CampaignPerformanceChatAnswer::fromHealthPayload([
            'campaign' => 'Italy packages',
            'stored_evaluation' => [
                'verdict' => 'decrease_budget',
                'verdict_reason' => 'No sales. Cost per conversions is $69.83, which is above $4.00.',
            ],
        ]);

        $this->assertSame(trim(<<<'ANSWER'
**Italy packages**

**Verdict:** Decrease budget — Consider reducing budget within guardrail limits.

**Why this verdict:**
No sales. Cost per conversions is $69.83, which is above $4.00.
ANSWER), $answer);
    }

    public function test_status_codes_are_shown_as_labels(): void
    {
        $text = CampaignPerformanceChatAnswer::humanizeStatusCodes(
            'Verdict: Decrease budget (decrease_budget). Saved status: decrease_budget. Verdict: pause. Campaign is learning.',
        );

        $this->assertSame(
            'Verdict: Decrease budget. Saved status: Decrease budget. Verdict: Pause. Campaign is learning.',
            $text,
        );
    }
}
