<?php
/**
 * Rating Context - Country/federation-specific formatting and terminology
 *
 * @package Chess_Podium
 */

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

final class ChessPodium_RatingContext
{
    /**
     * Get tournament country code (US, GB, etc.).
     *
     * @param object|null $tournament Tournament object.
     * @return string Country code.
     */
    public static function get_tournament_country(?object $tournament): string
    {
        if (!$tournament) {
            return '';
        }
        $code = $tournament->country_code ?? '';
        if ($code !== '') {
            return strtoupper(substr((string) $code, 0, 2));
        }
        $fed = $tournament->federation ?? '';
        if ($fed !== '') {
            return strtoupper(substr((string) $fed, 0, 2));
        }
        return '';
    }

    /**
     * Get tournament rating system (fide, uscf, ecf).
     *
     * @param object|null $tournament Tournament object.
     * @return string Rating system.
     */
    public static function get_tournament_rating_system(?object $tournament): string
    {
        if (!$tournament) {
            return 'fide';
        }
        $sys = $tournament->rating_system ?? '';
        $sys = strtolower(trim((string) $sys));
        if (in_array($sys, ['fide', 'uscf', 'ecf'], true)) {
            return $sys;
        }
        return 'fide';
    }

    /**
     * Format date by country (US: MM/DD/YYYY, GB: DD/MM/YYYY, default: YYYY-MM-DD).
     *
     * @param string      $date    Date string.
     * @param string|null $country Country code.
     * @return string Formatted date.
     */
    public static function format_date_by_country(string $date, ?string $country = null): string
    {
        if ($date === '') {
            return '';
        }
        $ts = strtotime($date);
        if ($ts === false) {
            return $date;
        }
        $country = $country !== null ? strtoupper(substr($country, 0, 2)) : '';
        if ($country === 'US') {
            return date('m/d/Y', $ts);
        }
        if ($country === 'GB' || $country === 'UK') {
            return date('d/m/Y', $ts);
        }
        return date('Y-m-d', $ts);
    }

    /**
     * Get localized term: Rank/Standing, Round/Rd, Draw/Tie.
     *
     * @param string      $term    Term key: rank, round, draw.
     * @param string|null $country Country code.
     * @return string Localized term.
     */
    public static function get_term(string $term, ?string $country = null): string
    {
        $country = $country !== null ? strtoupper(substr($country, 0, 2)) : '';
        $term = strtolower($term);

        if ($term === 'rank') {
            return $country === 'US' ? __('Standing', 'chess-podium') : __('Rank', 'chess-podium');
        }
        if ($term === 'round') {
            return $country === 'US' ? __('Rd', 'chess-podium') : __('Round', 'chess-podium');
        }
        if ($term === 'draw' || $term === 'tie') {
            return ($country === 'GB' || $country === 'UK') ? __('Draw', 'chess-podium') : __('Tie', 'chess-podium');
        }
        return $term;
    }
}
