<?php
declare(strict_types=1);

const SLOT_MIN_BET = 1;
const SLOT_MAX_BET = 1000;

const SLOT_SYMBOLS = ['cherry', 'lemon', 'bell', 'diamond', 'seven', 'jackpot'];

function slot_random_symbol(): string
{
    $u = random_int(0, 999);
    if ($u < 233) {
        return 'cherry';
    }
    if ($u < 456) {
        return 'lemon';
    }
    if ($u < 639) {
        return 'bell';
    }
    if ($u < 792) {
        return 'diamond';
    }
    if ($u < 895) {
        return 'seven';
    }

    return 'jackpot';
}

function slot_multiplier_for_triple(string $sym): int
{
    return match ($sym) {
        'seven' => 50,
        'diamond' => 25,
        'bell' => 15,
        'lemon' => 10,
        'cherry' => 8,
        default => 0,
    };
}

function slot_is_jackpot_triple(array $reels): bool
{
    return count($reels) === 3
        && ($reels[0] ?? '') === 'jackpot'
        && ($reels[1] ?? '') === 'jackpot'
        && ($reels[2] ?? '') === 'jackpot';
}

function slot_symbols_compatible(string $a, string $b): bool
{
    if ($a === 'jackpot' || $b === 'jackpot') {
        return false;
    }

    return $a === $b;
}

/** Üçlü tam satır sembolü (yalnızca aynı sembol). */
function slot_resolve_line_symbol(array $reels): ?string
{
    if (count($reels) !== 3) {
        return null;
    }
    if (slot_is_jackpot_triple($reels)) {
        return 'jackpot';
    }
    $a = (string) ($reels[0] ?? '');
    $b = (string) ($reels[1] ?? '');
    $c = (string) ($reels[2] ?? '');
    if ($a === 'jackpot' || $b === 'jackpot' || $c === 'jackpot') {
        return null;
    }
    if ($a === $b && $b === $c) {
        return $a;
    }

    return null;
}

function slot_pair_multiplier(array $reels): int
{
    if (count($reels) !== 3 || slot_is_jackpot_triple($reels)) {
        return 0;
    }
    $lineSym = slot_resolve_line_symbol($reels);
    if ($lineSym !== null && $lineSym !== 'jackpot') {
        $triple = slot_multiplier_for_triple($lineSym);
        if ($triple > 0) {
            return 0;
        }
    }
    $pairs = [
        [$reels[0], $reels[1]],
        [$reels[1], $reels[2]],
    ];
    foreach ($pairs as [$a, $b]) {
        if (!slot_symbols_compatible($a, $b)) {
            continue;
        }
        $sym = $a;
        if ($sym === 'jackpot') {
            continue;
        }
        $triple = slot_multiplier_for_triple($sym);
        if ($triple > 0) {
            return max(2, (int) floor($triple / 2));
        }
    }

    return 0;
}

/**
 * @return array{multiplier: int, winType: string, symbol: ?string, jackpotSymbols: bool}
 */
function slot_evaluate_spin(array $reels): array
{
    if (count($reels) !== 3) {
        return ['multiplier' => 0, 'winType' => 'none', 'symbol' => null, 'jackpotSymbols' => false];
    }
    if (slot_is_jackpot_triple($reels)) {
        return ['multiplier' => 0, 'winType' => 'jackpot_symbols', 'symbol' => 'jackpot', 'jackpotSymbols' => true];
    }
    $sym = slot_resolve_line_symbol($reels);
    if ($sym !== null && $sym !== 'jackpot') {
        $mult = slot_multiplier_for_triple($sym);
        if ($mult > 0) {
            return ['multiplier' => $mult, 'winType' => 'triple', 'symbol' => $sym, 'jackpotSymbols' => false];
        }
    }
    $pairMult = slot_pair_multiplier($reels);
    if ($pairMult > 0) {
        return ['multiplier' => $pairMult, 'winType' => 'pair', 'symbol' => $sym, 'jackpotSymbols' => false];
    }

    return ['multiplier' => 0, 'winType' => 'none', 'symbol' => null, 'jackpotSymbols' => false];
}

/** @deprecated Use slot_evaluate_spin */
function slot_evaluate_multiplier(array $reels): int
{
    return slot_evaluate_spin($reels)['multiplier'];
}

function slot_spin_reels(): array
{
    return [slot_random_symbol(), slot_random_symbol(), slot_random_symbol()];
}

function slot_paytable_public(): array
{
    return [
        'seven' => ['label' => '777', 'mult' => 50, 'type' => 'triple'],
        'diamond' => ['label' => 'Elmas ×3', 'mult' => 25, 'type' => 'triple'],
        'bell' => ['label' => 'Zil ×3', 'mult' => 15, 'type' => 'triple'],
        'lemon' => ['label' => 'Limon ×3', 'mult' => 10, 'type' => 'triple'],
        'cherry' => ['label' => 'Kiraz ×3', 'mult' => 8, 'type' => 'triple'],
        'jackpot' => ['label' => 'Jackpot ×3', 'mult' => 0, 'type' => 'jackpot'],
        'pair' => ['label' => 'İkili (yan yana)', 'mult' => 0, 'type' => 'pair', 'note' => 'Üçlü ödemenin yarısı'],
    ];
}

function slot_spin_message(array $eval, int $linePayout, ?array $jackpotWin): string
{
    if ($jackpotWin !== null) {
        $tier = strtoupper((string) ($jackpotWin['tier'] ?? ''));
        $amt = (int) ($jackpotWin['amount'] ?? 0);

        return match ($jackpotWin['tier'] ?? '') {
            'grand' => "GRAND JACKPOT! +{$amt} puan — efsane kazanç!",
            'major' => "MAJOR JACKPOT! +{$amt} puan!",
            'minor' => "MINOR JACKPOT! +{$amt} puan!",
            'mini' => "MINI JACKPOT! +{$amt} puan!",
            default => "JACKPOT ({$tier})! +{$amt} puan!",
        };
    }
    $mult = (int) ($eval['multiplier'] ?? 0);
    $type = (string) ($eval['winType'] ?? 'none');
    if ($mult >= 50) {
        return 'MEGA! Üç yedi — muhteşem ödül!';
    }
    if ($mult >= 25) {
        return 'Üç elmas — büyük kazanç!';
    }
    if ($mult >= 15) {
        return 'Üç zil — güzel!';
    }
    if ($mult >= 10) {
        return 'Üç limon — iyi gidiyorsun.';
    }
    if ($mult >= 8 && $type === 'triple') {
        return 'Üçlü eşleşme — kazandın!';
    }
    if ($mult > 0 && $type === 'pair') {
        return 'İkili eşleşme — küçük ödül!';
    }
    if ($linePayout > 0) {
        return 'Kazandın!';
    }

    return 'Şansına güven, bir dahaki sefere.';
}
