<?php
/**
 * Localization Routes
 *
 * API routes for language management and text overrides.
 * Migrated from: LanguageAPI.php + TextOverridesAPI.php
 *
 * @package FleekDash V2.2
 * @since   V2.2 Refactoring - Phase 4
 */

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\LanguageController;
use FleekDash\Controllers\LanguageDownloadController;
use FleekDash\Controllers\TextOverridesController;

class LocalizationRoutes
{
    /** Locale pattern: 2-3 lowercase letters, optional _XX (e.g. en_US, pt_BR) */
    private const LOCALE_PATTERN_STRICT = '^[a-z]{2}(_[A-Z]{2})?$';

    /** Locale pattern: word chars and underscores (flexible) */
    private const LOCALE_PATTERN_LOOSE = '[\w_]+';

    public static function register(Route $route)
    {
        // ============================================
        // LANGUAGE MANAGEMENT (10 routes)
        // ============================================

        // Get all languages (installed + downloadable from R2)
        $route->get('/languages', [LanguageDownloadController::class, 'getAvailableLanguages'],
            RouteAuthMiddleware::authenticated());

        // Download language pack from R2
        $route->post('/languages/{locale}/download', [LanguageDownloadController::class, 'downloadLanguage'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_STRICT]
        ]);

        // Delete language pack
        $route->delete('/languages/{locale}', [LanguageDownloadController::class, 'deleteLanguage'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_STRICT]
        ]);

        // Check for language pack updates
        $route->post('/languages/check-updates', [LanguageDownloadController::class, 'checkUpdates'],
            RouteAuthMiddleware::authenticated());

        // Get download progress
        $route->get('/languages/{locale}/progress', [LanguageDownloadController::class, 'getDownloadProgress'],
            RouteAuthMiddleware::authenticated(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_STRICT]
        ]);

        // Legacy routes (kept for backwards compatibility)
        // Update language order
        $route->post('/languages/order', [LanguageController::class, 'updateLanguageOrder'],
            RouteAuthMiddleware::admin());

        // Toggle language
        $route->post('/languages/{locale}/toggle', [LanguageController::class, 'toggleLanguage'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE]
        ]);

        // Get translation strings
        $route->get('/languages/{locale}/strings', [LanguageController::class, 'getTranslationStrings'],
            RouteAuthMiddleware::authenticated(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE]
        ]);

        // Update translation string
        $route->put('/languages/{locale}/strings/{id}', [LanguageController::class, 'updateTranslationString'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE],
            'id' => ['required' => true, 'type' => 'integer', 'minimum' => 1]
        ]);

        // ============================================
        // TEXT OVERRIDES (4 routes)
        // ============================================

        // Get text overrides
        $route->get('/text-overrides/{locale}', [TextOverridesController::class, 'getOverrides'],
            RouteAuthMiddleware::authenticated(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE]
        ]);

        // Get override strings
        $route->get('/text-overrides/{locale}/strings', [TextOverridesController::class, 'getAllStrings'],
            RouteAuthMiddleware::authenticated(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE]
        ]);

        // Update override string
        $route->put('/text-overrides/{locale}/strings/{string_id}', [TextOverridesController::class, 'updateOverride'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE],
            'string_id' => ['required' => true, 'type' => 'string', 'pattern' => '[\w\-]+']
        ]);

        // Delete override string
        $route->delete('/text-overrides/{locale}/strings/{string_id}', [TextOverridesController::class, 'deleteOverride'],
            RouteAuthMiddleware::admin(), [
            'locale' => ['required' => true, 'type' => 'string', 'pattern' => self::LOCALE_PATTERN_LOOSE],
            'string_id' => ['required' => true, 'type' => 'string', 'pattern' => '[\w\-]+']
        ]);
    }
}
