<?php

namespace Tests\Unit;

use App\Library\CampaignDecision\CampaignBudgetRecommendation;
use App\Library\CampaignDecision\CampaignDecisionVerdict;
use Tests\TestCase;

class CampaignBudgetRecommendationTest extends TestCase
{
    public function test_a_budget_cut_names_the_percent_and_the_new_daily_amount(): void
    {
        $recommendation = CampaignBudgetRecommendation::describe(
            CampaignDecisionVerdict::DecreaseBudget,
            -10,
            80,
        );

        $this->assertSame(-8.0, $recommendation['change_amount']);
        $this->assertSame(72.0, $recommendation['recommended_daily_budget']);
        $this->assertSame(
            'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day).',
            $recommendation['summary'],
        );
    }

    public function test_a_budget_increase_names_the_percent_and_the_new_daily_amount(): void
    {
        $recommendation = CampaignBudgetRecommendation::describe(
            CampaignDecisionVerdict::ScaleUp,
            10,
            20,
        );

        $this->assertSame(
            'Increase the daily budget from $20.00 to $22.00 ($2.00 more per day).',
            $recommendation['summary'],
        );
    }

    public function test_a_budget_change_without_a_current_budget_still_states_the_percent(): void
    {
        $recommendation = CampaignBudgetRecommendation::describe(
            CampaignDecisionVerdict::DecreaseBudget,
            -10,
            null,
        );

        $this->assertNull($recommendation['recommended_daily_budget']);
        $this->assertSame('Decrease the daily budget by 10%.', $recommendation['summary']);
    }

    public function test_pause_maintain_and_learning_state_what_happens_to_the_current_budget(): void
    {
        $this->assertSame(
            'Pause the campaign and stop all of the $50.00 daily budget.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::Pause, 0, 50)['summary'],
        );
        $this->assertSame(
            'Keep the daily budget at $50.00.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::Maintain, 0, 50)['summary'],
        );
        $this->assertSame(
            'Leave the $50.00 daily budget unchanged while the campaign is learning.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::Learning, 0, 50)['summary'],
        );
        $this->assertSame(
            'Leave the budget unchanged until the next check.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::Watch, 0, null)['summary'],
        );
        $this->assertSame(
            'Do not change the budget until the data issue is fixed.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::DataIssue, 0, null)['summary'],
        );
        $this->assertSame(
            'Do not change the $15.00 daily budget until there is enough data.',
            CampaignBudgetRecommendation::describe(CampaignDecisionVerdict::InsufficientData, 0, 15)['summary'],
        );
    }

    public function test_a_saved_suggestion_uses_its_recorded_percent_and_daily_budget(): void
    {
        $recommendation = CampaignBudgetRecommendation::fromStored('decrease_budget', [
            'recommended_budget_change_percent' => -20,
            'daily_budget' => 40,
        ]);

        $this->assertSame(
            'Decrease the daily budget from $40.00 to $32.00 ($8.00 less per day).',
            $recommendation['summary'] ?? null,
        );
    }

    public function test_a_vague_decrease_sentence_is_rewritten_with_the_budget_change(): void
    {
        $text = CampaignBudgetRecommendation::applyToText(
            '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.',
            'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day).',
        );

        $this->assertSame(
            'Decrease the daily budget from $80.00 to $72.00 ($8.00 less per day): the last 7 hours show $0 spend and 0 clicks, while outcome data shows $69.83 spent with 0 orders and $0 revenue.',
            $text,
        );
    }
}
