<?php

namespace Tests\Feature;

use Tests\TestCase;

class CampaignPerformanceCardVerdictTest extends TestCase
{
    public function test_the_card_badge_shows_the_ai_verdict(): void
    {
        $html = view('campaigns.partials.performance-campaign-card', [
            'row' => $this->row('pause', 'Pause'),
        ])->render();

        $this->assertStringContainsString('verdict: Pause', $html);
        $this->assertStringContainsString('is-failed', $html);
        $this->assertStringNotContainsString('>Maintain<', $html);
        $this->assertStringNotContainsString('verdict: Maintain', $html);
    }

    public function test_the_card_badge_uses_the_stored_verdict_when_the_ai_has_none(): void
    {
        $html = view('campaigns.partials.performance-campaign-card', [
            'row' => $this->row(null, null),
        ])->render();

        $this->assertStringContainsString('verdict: Maintain', $html);
        $this->assertStringNotContainsString('verdict: No verdict yet', $html);
    }

    public function test_the_card_badge_says_no_verdict_when_none_is_stored(): void
    {
        $row = $this->row(null, null);
        $row['status'] = null;

        $html = view('campaigns.partials.performance-campaign-card', [
            'row' => $row,
        ])->render();

        $this->assertStringContainsString('verdict: No verdict yet', $html);
    }

    public function test_a_written_recommendation_is_not_repeated_as_an_action(): void
    {
        $row = $this->row('pause', 'Pause');
        $row['marketing_lever'] = 'fix_landing_page';
        $row['review'] = [
            'analysis' => "Current performance\nSpend: 12.10\n\nWhat is working. The ads earn clicks.\nThe bottleneck. Nothing happens after the click.\nRecommendations\n1. Review each ad against the lead goal and change the visual and copy to tell the same story across the headline, the picture, and the button before the next spend period starts, including every comparison the current ads already make and every price claim they repeat.",
            'creatives' => [[
                'current_headline' => 'One Wrong File Change Can Cost You 20 Minutes',
                'action' => 'change',
                'why' => 'The picture shows the old way of working, not the approval step.',
                'replacement_headline' => 'Review the Change Before It Touches Your Code',
                'replacement_primary_text' => 'Lucos reviews every step before it touches your code.',
                'replacement_cta' => 'SEE_DETAILS',
            ]],
        ];

        $html = view('campaigns.partials.performance-campaign-card', [
            'row' => $row,
        ])->render();

        $this->assertStringNotContainsString('What to do', $html);
        $this->assertStringNotContainsString('Keep the current budget.', $html);
        $this->assertStringNotContainsString('>Action.', $html);
        $this->assertStringNotContainsString('change the visual and copy', $html);
        $this->assertStringContainsString('The ads earn clicks.', $html);
        $this->assertStringContainsString('mb-perf-ad-subtitle">Creative', $html);
        $this->assertStringContainsString('class="mb-perf-ad"', $html);
        $this->assertStringContainsString('Review the Change Before It Touches Your Code', $html);
        $this->assertStringContainsString('See Details', $html);
    }

    /**
     * @return array<string, mixed>
     */
    private function row(?string $aiStatus, ?string $aiStatusLabel): array
    {
        return [
            'status' => 'maintain',
            'ai_status' => $aiStatus,
            'ai_status_label' => $aiStatusLabel,
            'trend' => 'flat',
            'platform' => 'meta',
            'platform_status' => 'active',
            'name' => 'Lucos retargeting',
            'external_campaign_id' => '120251936608840626',
            'account_id' => '1950320145707342',
            'daily_budget' => 20,
            'region_label' => null,
            'headline' => null,
            'ctr_7d' => 0.02,
            'cpc_7d' => 1.25,
            'spend_7d' => 40,
            'impressions_7d' => 1000,
            'clicks_7d' => 20,
            'platform_conversions_7d' => 1,
            'roi_performance' => null,
            'affiliate_matched' => false,
            'verdict_reason' => null,
            'action_summary' => 'Keep the current budget.',
            'learning' => 'Past learning',
            'trend_summary' => null,
            'platform_learning_label' => null,
            'last_error' => null,
            'updated_at' => null,
        ];
    }
}
