<?php

namespace Tests\Unit;

use App\Campaigns\CampaignPerformanceSuggestionDisplay;
use App\Models\CampaignPerformanceSuggestion;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CampaignPerformanceSuggestionDisplayTest extends TestCase
{
    use RefreshDatabase;

    public function test_implementation_plan_markdown_matches_the_board_plan_fields(): void
    {
        $suggestion = CampaignPerformanceSuggestion::query()->create([
            'user_id' => User::factory()->create()->id,
            'platform' => 'meta',
            'external_campaign_id' => '120251936608840626',
            'status' => 'decrease_budget',
            'source' => 'llm',
            'reason' => "Checked the last 7 days.\nDecrease budget stays. Fix the landing page before only cutting spend.\nCTR is 1.25% on 10 clicks.",
            'snapshot' => [
                'decision_engine' => 'v2',
                'reason' => 'ROAS below minimum on lagged spend.',
                'recommended_budget_change_percent' => -10,
                'daily_budget' => 80,
                'last_7_days' => ['spend' => 80, 'clicks' => 150, 'impressions' => 10000],
                'marketing_diagnosis' => [
                    'primary_lever' => 'fix_landing_page',
                    'plan' => [
                        'Checked the last 7 days.',
                        'Decrease budget stays. Fix the landing page before only cutting spend.',
                        'CTR is 1.25% on 10 clicks.',
                    ],
                ],
            ],
            'suggested_at' => now(),
        ]);

        $markdown = CampaignPerformanceSuggestionDisplay::implementationPlanMarkdown($suggestion);

        $this->assertNotNull($markdown);
        $this->assertStringContainsString('**Hold or reduce budget while you strengthen the conversion path.**', $markdown);
        $this->assertStringContainsString('**Blocker:**', $markdown);
        $this->assertStringNotContainsString('No delivery yet', $markdown);
        $this->assertSame('ROAS below minimum on lagged spend.', CampaignPerformanceSuggestionDisplay::engineVerdictReason($suggestion));
    }
}
