<?php

if (!function_exists('platform_trans')) {
    /**
     * Get platform-specific translation for the current locale
     *
     * @param string $key Translation key (e.g., 'platform_users', 'platform_name')
     * @param string|null $locale Optional locale override
     * @param mixed $default Default value if key is not found
     * @return string
     */
    function platform_trans($key, $locale = null, $default = null)
    {
        $locale = $locale ?? app()->getLocale();
        $baseLanguage = timebank_config('base_language', 'en');

        // Try to get translation for current locale
        $translation = timebank_config("platform_translations.{$locale}.{$key}");

        // Fallback to base language if not found
        if ($translation === null && $locale !== $baseLanguage) {
            $translation = timebank_config("platform_translations.{$baseLanguage}.{$key}");
        }

        // Return default if still not found
        return $translation ?? $default ?? $key;
    }
}

if (!function_exists('platform_name')) {
    /**
     * Get the platform name for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_name($locale = null)
    {
        return platform_trans('platform_name', $locale, 'Timebank.cc');
    }
}

if (!function_exists('platform_name_short')) {
    /**
     * Get the short platform name for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_name_short($locale = null)
    {
        return platform_trans('platform_name_short', $locale, 'Timebank');
    }
}

if (!function_exists('platform_name_legal')) {
    /**
     * Get the legal platform name for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_name_legal($locale = null)
    {
        return platform_trans('platform_name_legal', $locale, 'association Timebank.cc');
    }
}

if (!function_exists('platform_slogan')) {
    /**
     * Get the platform slogan for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_slogan($locale = null)
    {
        return platform_trans('platform_slogan', $locale, 'Your time is currency');
    }
}

if (!function_exists('platform_user')) {
    /**
     * Get the singular platform user term for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_user($locale = null)
    {
        return platform_trans('platform_user', $locale, 'Timebanker');
    }
}

if (!function_exists('platform_users')) {
    /**
     * Get the plural platform users term for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_users($locale = null)
    {
        return platform_trans('platform_users', $locale, 'Timebankers');
    }
}

if (!function_exists('platform_principles')) {
    /**
     * Get the platform principles term for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_principles($locale = null)
    {
        return platform_trans('platform_principles', $locale, 'Timebank principles');
    }
}

if (!function_exists('platform_currency_name')) {
    /**
     * Get the singular platform currency name for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_currency_name($locale = null)
    {
        return platform_trans('platform_currency_name', $locale, 'Hour');
    }
}

if (!function_exists('platform_currency_name_plural')) {
    /**
     * Get the plural platform currency name for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_currency_name_plural($locale = null)
    {
        return platform_trans('platform_currency_name_plural', $locale, 'Hours');
    }
}

if (!function_exists('platform_currency_symbol')) {
    /**
     * Get the platform currency symbol for the current locale
     *
     * @param string|null $locale Optional locale override
     * @return string
     */
    function platform_currency_symbol($locale = null)
    {
        return platform_trans('platform_currency_symbol', $locale, 'H');
    }
}

if (!function_exists('trans_with_platform')) {
    /**
     * Translate a string and replace platform-specific placeholders
     *
     * Replaces:
     * - @PLATFORM_NAME@ with platform_name()
     * - @PLATFORM_NAME_SHORT@ with platform_name_short()
     * - @PLATFORM_NAME_LEGAL@ with platform_name_legal()
     * - @PLATFORM_SLOGAN@ with platform_slogan()
     * - @PLATFORM_USER@ with platform_user()
     * - @PLATFORM_USERS@ with platform_users()
     * - @PLATFORM_PRINCIPLES@ with platform_principles()
     * - @PLATFORM_CURRENCY_NAME@ with platform_currency_name()
     * - @PLATFORM_CURRENCY_NAME_PLURAL@ with platform_currency_name_plural()
     * - @PLATFORM_CURRENCY_SYMBOL@ with platform_currency_symbol()
     *
     * @param string $key Translation key
     * @param array $replace Additional replacements
     * @param string|null $locale Optional locale override
     * @return string
     */
    function trans_with_platform($key, $replace = [], $locale = null)
    {
        $translation = __($key, $replace, $locale);

        // Replace platform-specific placeholders
        $replacements = [
            '@PLATFORM_NAME@' => platform_name($locale),
            '@PLATFORM_NAME_SHORT@' => platform_name_short($locale),
            '@PLATFORM_NAME_LEGAL@' => platform_name_legal($locale),
            '@PLATFORM_SLOGAN@' => platform_slogan($locale),
            '@PLATFORM_USER@' => platform_user($locale),
            '@PLATFORM_USERS@' => platform_users($locale),
            '@PLATFORM_PRINCIPLES@' => platform_principles($locale),
            '@PLATFORM_CURRENCY_NAME@' => platform_currency_name($locale),
            '@PLATFORM_CURRENCY_NAME_PLURAL@' => platform_currency_name_plural($locale),
            '@PLATFORM_CURRENCY_SYMBOL@' => platform_currency_symbol($locale),
        ];

        return str_replace(
            array_keys($replacements),
            array_values($replacements),
            $translation
        );
    }
}
