<?php
declare(strict_types=1);

const ROULETTE_MIN_BET = 1;
const ROULETTE_MAX_BET = 1000;

const ROULETTE_REDS = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36];

const ROULETTE_WHEEL_ORDER = [
    0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10,
    5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26,
];

function roulette_is_red(int $n): bool
{
    return $n >= 1 && $n <= 36 && in_array($n, ROULETTE_REDS, true);
}

function roulette_is_black(int $n): bool
{
    return $n >= 1 && $n <= 36 && !roulette_is_red($n);
}

function roulette_spin_number(): int
{
    return random_int(0, 36);
}

function roulette_win_angle_deg(int $winning): float
{
    $idx = array_search($winning, ROULETTE_WHEEL_ORDER, true);
    if ($idx === false) {
        return 0.0;
    }
    $n = count(ROULETTE_WHEEL_ORDER);

    return (($idx + 0.5) / $n) * 360.0;
}

function roulette_evaluate_bet(string $betType, mixed $betValue, int $w): array
{
    $t = strtolower(trim($betType));

    if ($t === 'straight') {
        $n = (int) $betValue;
        if ($n < 0 || $n > 36) {
            return ['ok' => false, 'error' => 'bad_value'];
        }

        return ['ok' => true, 'mult' => $n === $w ? 36 : 0];
    }

    if ($t === 'red') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }

        return ['ok' => true, 'mult' => roulette_is_red($w) ? 2 : 0];
    }

    if ($t === 'black') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }

        return ['ok' => true, 'mult' => roulette_is_black($w) ? 2 : 0];
    }

    if ($t === 'even') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }
        $win = $w >= 2 && $w <= 36 && $w % 2 === 0;

        return ['ok' => true, 'mult' => $win ? 2 : 0];
    }

    if ($t === 'odd') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }
        $win = $w % 2 === 1;

        return ['ok' => true, 'mult' => $win ? 2 : 0];
    }

    if ($t === 'low') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }

        return ['ok' => true, 'mult' => ($w >= 1 && $w <= 18) ? 2 : 0];
    }

    if ($t === 'high') {
        if ($w === 0) {
            return ['ok' => true, 'mult' => 0];
        }

        return ['ok' => true, 'mult' => ($w >= 19 && $w <= 36) ? 2 : 0];
    }

    if ($t === 'dozen') {
        $d = (int) $betValue;
        if ($d < 1 || $d > 3) {
            return ['ok' => false, 'error' => 'bad_value'];
        }
        if ($w < 1 || $w > 36) {
            return ['ok' => true, 'mult' => 0];
        }
        $bucket = (int) ceil($w / 12.0);
        $win = $bucket === $d;

        return ['ok' => true, 'mult' => $win ? 3 : 0];
    }

    if ($t === 'column') {
        $c = (int) $betValue;
        if ($c < 1 || $c > 3) {
            return ['ok' => false, 'error' => 'bad_value'];
        }
        if ($w < 1 || $w > 36) {
            return ['ok' => true, 'mult' => 0];
        }
        $col = (($w - 1) % 3) + 1;
        $win = $col === $c;

        return ['ok' => true, 'mult' => $win ? 3 : 0];
    }

    return ['ok' => false, 'error' => 'bad_type'];
}

function roulette_public_meta(): array
{
    return [
        'minBet' => ROULETTE_MIN_BET,
        'maxBet' => ROULETTE_MAX_BET,
        'wheelOrder' => ROULETTE_WHEEL_ORDER,
        'reds' => ROULETTE_REDS,
        'payouts' => [
            'straight' => '35:1 (36× bahis iade)',
            'red' => '1:1',
            'black' => '1:1',
            'even' => '1:1',
            'odd' => '1:1',
            'low' => '1:1 (1–18)',
            'high' => '1:1 (19–36)',
            'dozen' => '2:1',
            'column' => '2:1',
        ],
    ];
}
