<?php

declare(strict_types=1);

namespace App\Framework\Random;

/**
 * Deterministic random generator for testing purposes.
 *
 * This generator produces predictable output which is useful for testing
 * scenarios where you need consistent, reproducible random values.
 */
final class TestableRandomGenerator implements RandomGenerator
{
    private int $counter = 0;

    public function __construct(
        private readonly string $seed = 'test'
    ) {
    }

    public function bytes(int $length): string
    {
        $result = '';
        for ($i = 0; $i < $length; $i++) {
            $result .= chr(($this->getNextValue() % 256));
        }

        return $result;
    }

    public function int(int $min, int $max): int
    {
        $range = $max - $min + 1;

        return $min + ($this->getNextValue() % $range);
    }

    public function float(int $min, int $max): float
    {
        return ($this->getNextValue() % 1000000) / 1000000.0;
    }

    /**
     * Reset the generator to its initial state.
     */
    public function reset(): void
    {
        $this->counter = 0;
    }

    private function getNextValue(): int
    {
        $value = crc32($this->seed . $this->counter);
        $this->counter++;

        return abs($value);
    }

    public function hex(int $length): string
    {
        return bin2hex($this->bytes($length));
    }
}
