<?php

if (!defined('ABSPATH')) {
    exit;
}

final class TFT_Miniapp_Catalog_Overrides
{
    private static ?array $services = null;

    public static function apply_tours(array $tours): array
    {
        $result = [];
        foreach ($tours as $tour) {
            if (!is_array($tour) || empty($tour['id'])) {
                continue;
            }
            $overridden = self::apply_tour($tour);
            if ($overridden !== null) {
                $result[] = $overridden;
            }
        }
        return $result;
    }

    public static function apply_tour(array $tour): ?array
    {
        $id = (string) ($tour['id'] ?? '');
        $override = self::services()[$id] ?? null;
        if (!is_array($override)) {
            return $tour;
        }
        if (($override['visible'] ?? true) === false) {
            return null;
        }

        $title = trim((string) ($override['title'] ?? ''));
        if ($title !== '') {
            $tour['title'] = $title;
        }

        $price_mode = (string) ($override['priceMode'] ?? '');
        if (in_array($price_mode, ['free', 'request'], true)) {
            $tour = self::apply_price_mode($tour, $price_mode);
        }

        return $tour;
    }

    private static function services(): array
    {
        if (self::$services !== null) {
            return self::$services;
        }

        $file = TFT_MINIAPP_API_PATH . 'data/catalog-overrides.json';
        if (!is_readable($file)) {
            self::$services = [];
            return self::$services;
        }

        $decoded = json_decode((string) file_get_contents($file), true);
        self::$services = is_array($decoded['services'] ?? null)
            ? $decoded['services']
            : [];
        return self::$services;
    }

    private static function apply_price_mode(array $tour, string $mode): array
    {
        $label = $mode === 'free' ? 'Бесплатно' : 'по запросу';
        $amount = $mode === 'free' ? 0 : null;

        foreach (($tour['prices'] ?? []) as $index => $price) {
            if (!is_array($price)) {
                continue;
            }
            foreach (['adult', 'child', 'infant'] as $price_type) {
                if ($price_type === 'infant' && !array_key_exists($price_type, $price)) {
                    continue;
                }
                $price[$price_type] = self::money($amount, $label);
            }
            $tour['prices'][$index] = $price;
        }

        foreach (($tour['variants'] ?? []) as $index => $variant) {
            if (!is_array($variant)) {
                continue;
            }
            foreach (['adult', 'child', 'infant'] as $price_type) {
                if ($price_type === 'infant' && !array_key_exists($price_type, $variant)) {
                    continue;
                }
                $variant[$price_type] = self::money($amount, $label);
            }
            $tour['variants'][$index] = $variant;
        }

        $tour['minAdultPrice'] = $amount;
        return $tour;
    }

    private static function money(?float $amount, string $label): array
    {
        return [
            'amount' => $amount,
            'currency' => 'THB',
            'label' => $label,
            'raw' => $label,
        ];
    }
}
