<?php

declare(strict_types=1);

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

final class ClientCodeRangeTest extends TestCase
{
    protected function setUp(): void
    {
        require_once dirname(__DIR__, 2) . '/lib/client_code_range.php';
    }

    public function testExpandsZeroPaddedPrefixRange(): void
    {
        $codes = qdb_expand_client_code_range('C001', 'C005');
        $this->assertSame(['C001', 'C002', 'C003', 'C004', 'C005'], $codes);
    }

    public function testExpandsSuffixRange(): void
    {
        $codes = qdb_expand_client_code_range('A01Demo', 'A03Demo');
        $this->assertSame(['A01Demo', 'A02Demo', 'A03Demo'], $codes);
    }

    public function testExpandsDemoSuffixWithTwoDigitPadding(): void
    {
        $codes = qdb_expand_client_code_range('C01Demo', 'C05Demo');
        $this->assertSame(['C01Demo', 'C02Demo', 'C03Demo', 'C04Demo', 'C05Demo'], $codes);
        $this->assertSame(
            array_map(static fn (int $n): string => 'C' . str_pad((string) $n, 2, '0', STR_PAD_LEFT) . 'Demo', range(1, 35)),
            qdb_expand_client_code_range('C01Demo', 'C35Demo'),
        );
    }

    public function testUsesLastNumericSegmentWhenMultiplePresent(): void
    {
        $codes = qdb_expand_client_code_range('RANGE-ab12-001', 'RANGE-ab12-003');
        $this->assertSame(['RANGE-ab12-001', 'RANGE-ab12-002', 'RANGE-ab12-003'], $codes);
    }

    public function testExpandsSingleCodeRange(): void
    {
        $codes = qdb_expand_client_code_range('X9Y', 'X9Y');
        $this->assertSame(['X9Y'], $codes);
    }

    public function testRejectsMismatchedPrefix(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        qdb_expand_client_code_range('C001', 'D001');
    }

    public function testRejectsDescendingRange(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        qdb_expand_client_code_range('C050', 'C001');
    }

    public function testRejectsRangeWithoutDigits(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        qdb_expand_client_code_range('CLIENT', 'CLIENT');
    }
}
