<?php

namespace Tests\Unit;

use App\Campaigns\PerformanceChart;
use App\Campaigns\PerformanceDay;
use Illuminate\Support\Carbon;
use Tests\TestCase;

class CampaignPerformanceChartTest extends TestCase
{
    public function test_it_builds_spend_and_click_lines(): void
    {
        $this->travelTo('2026-09-22 12:00:00');

        $chart = PerformanceChart::fromDays(collect([
            new PerformanceDay(
                stat_date: Carbon::parse('2026-09-21')->startOfDay(),
                spend: 8.50,
                impressions: 400,
                clicks: 2,
            ),
            new PerformanceDay(
                stat_date: Carbon::parse('2026-09-22')->startOfDay(),
                spend: 12.10,
                impressions: 800,
                clicks: 4,
            ),
        ]));

        $this->assertFalse($chart['empty']);
        $this->assertSame('2026-09-21', $chart['first_date']);
        $this->assertSame('2026-09-22', $chart['last_date']);
        $this->assertSame(2, $chart['day_count']);
        $this->assertCount(2, $chart['days']);
        $this->assertSame(8.5, $chart['days'][0]['spend']);
        $this->assertSame(4, $chart['days'][1]['clicks']);
        $this->assertCount(2, $chart['dots_spend']);
        $this->assertCount(2, $chart['dots_clicks']);
        $this->assertSame(2, count(explode(' ', $chart['points_spend'])));
        $this->assertSame(2, count(explode(' ', $chart['points_clicks'])));
        $this->assertStringStartsWith('M ', $chart['area']);
    }

    public function test_empty_history_has_no_series(): void
    {
        $chart = PerformanceChart::fromDays(collect());

        $this->assertTrue($chart['empty']);
        $this->assertSame([], $chart['days']);
        $this->assertSame('', $chart['points_spend']);
        $this->assertSame('', $chart['points_clicks']);
    }
}
