<?php

declare(strict_types=1);

function mocker_site_json_path(): string
{
    return dirname(__DIR__) . '/data/site.json';
}

function mocker_site_json_read(): array
{
    $path = mocker_site_json_path();
    if (!is_readable($path)) {
        return [];
    }
    $raw = file_get_contents($path);
    if ($raw === false || $raw === '') {
        return [];
    }
    if (str_starts_with($raw, "\xEF\xBB\xBF")) {
        $raw = substr($raw, 3);
    }
    $data = json_decode($raw, true);

    return is_array($data) ? $data : [];
}

function mocker_join_guide_trim_locale(array $b): array
{
    $steps = [];
    $stepsIn = $b['steps'] ?? [];
    if (is_array($stepsIn)) {
        foreach ($stepsIn as $step) {
            if (count($steps) >= 4) {
                break;
            }
            $steps[] = trim((string) $step);
        }
    }
    while (count($steps) < 4) {
        $steps[] = '';
    }

    return [
        'title' => isset($b['title']) ? trim((string) $b['title']) : '',
        'steps' => array_slice($steps, 0, 4),
    ];
}

function mocker_join_guides_trim_all($raw): array
{
    $out = [
        'events' => ['locales' => ['tr' => mocker_join_guide_trim_locale([]), 'en' => mocker_join_guide_trim_locale([])]],
        'tickets' => ['locales' => ['tr' => mocker_join_guide_trim_locale([]), 'en' => mocker_join_guide_trim_locale([])]],
    ];
    if (!is_array($raw)) {
        return $out;
    }
    foreach (['events', 'tickets'] as $kind) {
        $block = $raw[$kind] ?? null;
        if (!is_array($block)) {
            continue;
        }
        $localesIn = isset($block['locales']) && is_array($block['locales']) ? $block['locales'] : [];
        $tr = isset($localesIn['tr']) && is_array($localesIn['tr']) ? mocker_join_guide_trim_locale($localesIn['tr']) : mocker_join_guide_trim_locale([]);
        $en = isset($localesIn['en']) && is_array($localesIn['en']) ? mocker_join_guide_trim_locale($localesIn['en']) : mocker_join_guide_trim_locale([]);
        $out[$kind] = ['locales' => ['tr' => $tr, 'en' => $en]];
    }

    return $out;
}

function mocker_join_guides_read(): array
{
    $site = mocker_site_json_read();
    $stored = $site['joinGuides'] ?? null;

    return mocker_join_guides_trim_all(is_array($stored) ? $stored : []);
}

function mocker_join_guide_save_kind(string $kind, array $guide): array
{
    if (!in_array($kind, ['events', 'tickets'], true)) {
        throw new InvalidArgumentException('Geçersiz rehber türü.');
    }
    $site = mocker_site_json_read();
    $all = mocker_join_guides_read();
    $localesIn = isset($guide['locales']) && is_array($guide['locales']) ? $guide['locales'] : [];
    $tr = isset($localesIn['tr']) && is_array($localesIn['tr']) ? mocker_join_guide_trim_locale($localesIn['tr']) : mocker_join_guide_trim_locale([]);
    $en = isset($localesIn['en']) && is_array($localesIn['en']) ? mocker_join_guide_trim_locale($localesIn['en']) : mocker_join_guide_trim_locale([]);
    $all[$kind] = ['locales' => ['tr' => $tr, 'en' => $en]];

    $site['joinGuides'] = $all;
    $json = json_encode($site, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    if ($json === false) {
        throw new RuntimeException('JSON encode failed');
    }
    if (file_put_contents(mocker_site_json_path(), $json) === false) {
        throw new RuntimeException('site.json yazılamadı');
    }

    return $all[$kind];
}
