<?php

namespace MintyPHP\Tests\Domain\Taxonomy;

use MintyPHP\Domain\Taxonomy\ImportAuditStatus;
use MintyPHP\Domain\Taxonomy\MailLogStatus;
use MintyPHP\Domain\Taxonomy\ScheduledJobRunStatus;
use MintyPHP\Domain\Taxonomy\ScheduledJobStatus;
use MintyPHP\Domain\Taxonomy\ScheduledJobTriggerType;
use MintyPHP\Domain\Taxonomy\SchedulerRuntimeResult;
use MintyPHP\Domain\Taxonomy\SystemAuditChannel;
use MintyPHP\Domain\Taxonomy\SystemAuditOutcome;
use MintyPHP\Domain\Taxonomy\TenantStatus;
use MintyPHP\Domain\Taxonomy\UserLifecycleAction;
use MintyPHP\Domain\Taxonomy\UserLifecycleStatus;
use MintyPHP\Domain\Taxonomy\UserLifecycleTriggerType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class TaxonomyEnumContractTest extends TestCase
{
    /**
     * @return iterable<string, array{0: class-string<\BackedEnum>, 1: list<string>}>
     */
    public static function enumProvider(): iterable
    {
        yield 'SystemAuditOutcome' => [SystemAuditOutcome::class, ['success', 'failed', 'denied']];
        yield 'SystemAuditChannel' => [SystemAuditChannel::class, ['web', 'api', 'scheduler', 'cli']];
        yield 'UserLifecycleAction' => [UserLifecycleAction::class, ['deactivate', 'delete', 'restore']];
        yield 'UserLifecycleTriggerType' => [UserLifecycleTriggerType::class, ['manual', 'cron', 'system']];
        yield 'UserLifecycleStatus' => [UserLifecycleStatus::class, ['success', 'skipped', 'failed']];
        yield 'ImportAuditStatus' => [ImportAuditStatus::class, ['running', 'success', 'partial', 'failed']];
        yield 'ScheduledJobStatus' => [ScheduledJobStatus::class, ['running', 'success', 'failed', 'skipped']];
        yield 'ScheduledJobRunStatus' => [ScheduledJobRunStatus::class, ['success', 'failed', 'skipped']];
        yield 'ScheduledJobTriggerType' => [ScheduledJobTriggerType::class, ['scheduler', 'manual']];
        yield 'MailLogStatus' => [MailLogStatus::class, ['queued', 'sent', 'failed']];
        yield 'TenantStatus' => [TenantStatus::class, ['active', 'inactive']];
        yield 'SchedulerRuntimeResult' => [SchedulerRuntimeResult::class, ['ok', 'lock_not_acquired', 'unexpected_error']];
    }

    #[DataProvider('enumProvider')]
    public function testValuesReturnExpectedList(string $enumClass, array $expectedValues): void
    {
        $this->assertSame($expectedValues, $enumClass::values());
    }

    #[DataProvider('enumProvider')]
    public function testTryNormalizeAndNormalizeOr(string $enumClass, array $expectedValues): void
    {
        $firstValue = $expectedValues[0];
        $firstCase = $enumClass::from($firstValue);

        $this->assertSame($firstValue, $enumClass::tryNormalize($firstValue)?->value);
        $this->assertSame($firstValue, $enumClass::tryNormalize(strtoupper($firstValue))?->value);
        $this->assertSame($firstValue, $enumClass::tryNormalize(' ' . strtoupper($firstValue) . ' ')?->value);
        $this->assertNull($enumClass::tryNormalize(''));
        $this->assertNull($enumClass::tryNormalize('not-valid'));

        $this->assertSame($firstValue, $enumClass::normalizeOr('not-valid', $firstCase)->value);
        $this->assertSame($firstValue, $enumClass::normalizeOr($firstValue, $firstCase)->value);
    }

    #[DataProvider('enumProvider')]
    public function testBadgeVariantAndLabelTokenAreDefined(string $enumClass, array $expectedValues): void
    {
        foreach ($expectedValues as $value) {
            $case = $enumClass::from($value);

            $labelToken = $case->labelToken();
            $badgeVariant = $case->badgeVariant();

            $this->assertIsString($labelToken);
            $this->assertNotSame('', trim($labelToken));
            $this->assertIsString($badgeVariant);
            $this->assertNotSame('', trim($badgeVariant));
        }
    }
}
