<?php

declare(strict_types=1);

namespace Tests\Framework\StaticSite;

use App\Framework\Router\HttpRouter;
use App\Framework\Router\Route;
use App\Framework\Router\RouteCollection;
use App\Framework\StaticSite\RouteCollector;
use PHPUnit\Framework\TestCase;

class RouteCollectorTest extends TestCase
{
    public function testCollectRoutes(): void
    {
        // Testdaten vorbereiten
        $routes = [
            new Route(path: '/', controller: 'TestController', action: 'index', methods: ['GET']),
            new Route(path: '/about', controller: 'TestController', action: 'about', methods: ['GET']),
            new Route(path: '/api/data', controller: 'ApiController', action: 'getData', methods: ['GET']),
            new Route(path: '/contact', controller: 'TestController', action: 'contact', methods: ['POST']),
            new Route(path: '/users/{id}', controller: 'UserController', action: 'show', methods: ['GET']),
        ];

        $routeCollection = $this->createMock(RouteCollection::class);
        $routeCollection->method('getAll')->willReturn($routes);

        $router = $this->createMock(HttpRouter::class);
        $router->method('getRoutes')->willReturn($routeCollection);

        // RouteCollector initialisieren
        $collector = new RouteCollector($router);

        // Routen sammeln
        $collectedRoutes = $collector->collectRoutes();

        // Prüfen, ob die richtigen Routen gesammelt wurden (nur GET-Methoden)
        $this->assertContains('/', $collectedRoutes);
        $this->assertContains('/about', $collectedRoutes);
        $this->assertContains('/api/data', $collectedRoutes);
        $this->assertContains('/users/{id}', $collectedRoutes);
        $this->assertNotContains('/contact', $collectedRoutes); // POST-Route sollte nicht enthalten sein
    }

    public function testFilterRoutes(): void
    {
        // Testdaten vorbereiten
        $routes = [
            '/',
            '/about',
            '/api/data',
            '/api/users',
            '/admin/dashboard',
            '/users/{id}',
            '/products/{slug}',
        ];

        $excludePatterns = [
            '/\/api\/.*/',    // API-Routen ausschließen
            '/\/admin\/.*/', // Admin-Routen ausschließen
        ];

        $router = $this->createMock(HttpRouter::class);
        $collector = new RouteCollector($router);

        // Routen filtern
        $filteredRoutes = $collector->filterRoutes($routes, $excludePatterns);

        // Prüfen, ob die richtigen Routen gefiltert wurden
        $this->assertContains('/', $filteredRoutes);
        $this->assertContains('/about', $filteredRoutes);
        $this->assertNotContains('/api/data', $filteredRoutes);    // Sollte durch Pattern ausgeschlossen sein
        $this->assertNotContains('/api/users', $filteredRoutes);   // Sollte durch Pattern ausgeschlossen sein
        $this->assertNotContains('/admin/dashboard', $filteredRoutes); // Sollte durch Pattern ausgeschlossen sein
        $this->assertNotContains('/users/{id}', $filteredRoutes);      // Sollte durch Platzhalter ausgeschlossen sein
        $this->assertNotContains('/products/{slug}', $filteredRoutes);  // Sollte durch Platzhalter ausgeschlossen sein
    }
}
