<?php

namespace Tests\Unit;

use App\Campaigns\PerformanceVerdict;
use App\Campaigns\PerformanceWindowVerdict;
use Tests\TestCase;

class PerformanceWindowVerdictTest extends TestCase
{
    public function test_it_returns_no_action_during_platform_learning_on_hour_windows(): void
    {
        $rollup = [
            'window' => '3h',
            'from' => '2026-09-23 09:00:00',
            'to' => '2026-09-23 11:00:00',
            'spend' => 12.0,
            'impressions' => 100,
            'clicks' => 0,
            'cpc' => null,
            'ctr' => 0.0,
            'buckets' => 3,
            'expected_buckets' => 3,
            'insufficient_data' => false,
        ];

        $result = (new PerformanceWindowVerdict)->forWindow($rollup, [
            'days_live' => 10,
            'max_test_spend' => 40,
            'learning_window_days' => 3,
            'lifetime' => ['spend' => 100],
            'daily_budget' => 20,
            'delivery_status' => 'LEARNING',
            'rules' => ['zero_click_spend_floor' => 10, 'max_cpc' => 2],
        ]);

        $this->assertSame(PerformanceVerdict::NoAction->value, $result['verdict']);
    }

    public function test_it_pauses_when_spend_hits_floor_with_zero_clicks(): void
    {
        $rollup = [
            'window' => '7d',
            'from' => '2026-09-17 00:00:00',
            'to' => '2026-09-23 00:00:00',
            'spend' => 12.0,
            'impressions' => 100,
            'clicks' => 0,
            'cpc' => null,
            'ctr' => 0.0,
            'buckets' => 7,
            'expected_buckets' => 7,
            'insufficient_data' => false,
        ];

        $result = (new PerformanceWindowVerdict)->forWindow($rollup, [
            'days_live' => 10,
            'max_test_spend' => 40,
            'learning_window_days' => 3,
            'lifetime' => ['spend' => 100],
            'daily_budget' => 20,
            'platform_status' => 'ACTIVE',
            'rules' => ['zero_click_spend_floor' => 10, 'max_cpc' => 2],
        ]);

        $this->assertSame(PerformanceVerdict::Pause->value, $result['verdict']);
    }
}
