<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Controller;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin\AbandonedCartController;
use CpCreation\VitiCore\Marketing\Model\AbandonedCartReminder;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;

final class AbandonedCartControllerTest extends TestCase
{
    public function testValidReminderFiltersAreNormalized(): void
    {
        $ruleId = Uuid::uuid4()->toString();
        $filters = $this->resolveFilters(new Request([
            'search' => '  Client@Example.com  ',
            'status' => AbandonedCartReminder::STATUS_SENT,
            'rule' => $ruleId,
        ]));

        self::assertSame([
            'search' => 'Client@Example.com',
            'status' => AbandonedCartReminder::STATUS_SENT,
            'rule' => $ruleId,
        ], $filters);
    }

    public function testInvalidReminderFiltersAreIgnored(): void
    {
        $filters = $this->resolveFilters(new Request([
            'status' => 'unknown',
            'rule' => 'not-a-uuid',
        ]));

        self::assertSame([
            'search' => '',
            'status' => '',
            'rule' => '',
        ], $filters);
    }

    /**
     * @return array{search: string, status: string, rule: string}
     */
    private function resolveFilters(Request $request): array
    {
        $reflection = new \ReflectionClass(AbandonedCartController::class);
        $controller = $reflection->newInstanceWithoutConstructor();
        $resolveFilters = $reflection->getMethod('resolveReminderFilters');

        /** @var array{search: string, status: string, rule: string} $filters */
        $filters = $resolveFilters->invoke($controller, $request);

        return $filters;
    }
}
