<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Controller;

use Ferienpass\AdminBundle\Controller\AdminSearchController;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class AdminSearchControllerTest extends TestCase
{
    public function testSearchModeDetection(): void
    {
        $controller = new AdminSearchController();

        // We test mode detection via reflection since it's embedded in the search method
        $ref = new \ReflectionMethod($controller, 'search');
        $params = $ref->getParameters();

        // Verify the method signature accepts Request and EntityManagerInterface
        self::assertCount(2, $params);
        self::assertSame('request', $params[0]->getName());
        self::assertSame('entityManager', $params[1]->getName());
    }

    #[DataProvider('modeDetectionProvider')]
    public function testQueryPrefixModeDetection(string $input, ?string $expectedMode, string $expectedQuery): void
    {
        // Test the mode detection logic that's in the search method
        $query = $input;
        $mode = null;

        if (str_starts_with($query, '@')) {
            $mode = 'user';
            $query = mb_substr($query, 1);
        } elseif (str_starts_with($query, '#')) {
            $mode = 'participant';
            $query = mb_substr($query, 1);
        } elseif (str_starts_with($query, '>')) {
            $mode = 'offer';
            $query = mb_substr($query, 1);
        } elseif (str_starts_with($query, '€')) {
            $mode = 'payment';
            $query = mb_substr($query, 1);
        }

        self::assertSame($expectedMode, $mode);
        self::assertSame($expectedQuery, $query);
    }

    public static function modeDetectionProvider(): iterable
    {
        yield 'no prefix' => ['test query', null, 'test query'];
        yield 'user prefix' => ['@john', 'user', 'john'];
        yield 'participant prefix' => ['#smith', 'participant', 'smith'];
        yield 'offer prefix' => ['>swimming', 'offer', 'swimming'];
        yield 'payment prefix' => ['€1234', 'payment', '1234'];
        yield 'empty after prefix' => ['@', 'user', ''];
    }

    public function testShortQueryReturnsEmptyResults(): void
    {
        // Queries shorter than 2 characters should return empty results
        $query = 'a';
        self::assertTrue(mb_strlen($query) < 2);
    }
}
