<?php

namespace App\Tests\Service;

use App\Service\TrackingService;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class TrackingServiceTest extends TestCase
{
    private TrackingService $service;
    private LoggerInterface $logger;

    protected function setUp(): void
    {
        $this->logger = $this->createMock(LoggerInterface::class);
        $this->service = new TrackingService($this->logger);
    }

    public function testTrackPageView(): void
    {
        $this->logger->expects($this->once())->method('info');
        $this->service->trackPageView('site1', '/home', 'visitor1');
    }

    public function testTrackPageViewWithMetadata(): void
    {
        $this->logger->expects($this->once())->method('info');
        $this->service->trackPageView('site1', '/about', 'visitor2', ['referrer' => 'google']);
    }

    public function testTrackEvent(): void
    {
        $this->logger->expects($this->once())->method('info');
        $this->service->trackEvent('site1', 'click', 'visitor1');
    }

    public function testTrackEventWithMetadata(): void
    {
        $this->logger->expects($this->once())->method('info');
        $this->service->trackEvent('site1', 'purchase', 'visitor1', ['amount' => 99]);
    }

    public function testGetVisitorStats(): void
    {
        $from = new \DateTimeImmutable('2026-01-01');
        $to = new \DateTimeImmutable('2026-01-31');
        $result = $this->service->getVisitorStats('site1', $from, $to);

        $this->assertSame('site1', $result['siteId']);
        $this->assertSame('2026-01-01 - 2026-01-31', $result['period']);
        $this->assertSame(0, $result['visitors']);
        $this->assertSame(0, $result['uniqueVisitors']);
        $this->assertSame(0.0, $result['bounceRate']);
    }

    public function testGetPageViews(): void
    {
        $from = new \DateTimeImmutable('2026-03-01');
        $to = new \DateTimeImmutable('2026-03-31');
        $result = $this->service->getPageViews('site2', $from, $to);

        $this->assertSame('site2', $result['siteId']);
        $this->assertSame('2026-03-01 - 2026-03-31', $result['period']);
        $this->assertSame(0, $result['totalViews']);
        $this->assertSame([], $result['pages']);
    }
}
