<?php

namespace FleekDash\Routes\Groups;

use FleekDash\Controllers\PackageUploadController;
use FleekDash\Controllers\PluginsController;
use FleekDash\Controllers\ThemesController;
use FleekDash\Controllers\ThemesPalettesController;
use FleekDash\Core\Capabilities\ThemeCapabilities;
use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;

defined('ABSPATH') || exit;

class ThemeRoutes
{
    private const ROUTE_THEMES_THEME_ID = 'themes/{themeId}';

    public static function register(Route $_route): void // NOSONAR php:S1172 - Route API callback signature
    {
        // CRITICAL: Register most specific POST routes FIRST to prevent routing conflicts
        // WordPress REST API matches routes in registration order, and PUT routes use EDITABLE
        // which also accepts POST, causing /themes/apply to match /themes/{themeId}
        Route::post(
            'themes/apply',
            [ThemesController::class, 'apply'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        // WordPress.org installed theme upgrade (update-core iframe). MUST be before themes/{themeId}
        // or POST .../themes/update is captured by EDITABLE themes/{themeId} as themeId "update".
        Route::post(
            'themes/update',
            [PluginsController::class, 'update_wordpress_theme'],
            RouteAuthMiddleware::admin(),
        );

        // ZIP upload from theme-install.php (clean JSON — no wp-admin HTML noise)
        Route::post(
            'themes/upload',
            [PackageUploadController::class, 'uploadTheme'],
            RouteAuthMiddleware::admin(),
        );

        // Themes CRUD - GET routes (all public for login page and theme preview)
        Route::get('themes', [ThemesController::class, 'list'], RouteAuthMiddleware::public());
        Route::get(
            'themes/active',
            [ThemesController::class, 'active'],
            RouteAuthMiddleware::public(),
        );
        Route::get('themes/palettes', [ThemesPalettesController::class, 'listPalettes'], RouteAuthMiddleware::public());
        Route::get(
            'themes/palette-sync',
            [ThemesPalettesController::class, 'getSyncedPalette'],
            RouteAuthMiddleware::public(),
        );
        Route::get(self::ROUTE_THEMES_THEME_ID, [ThemesController::class, 'get'], RouteAuthMiddleware::public());

        // Themes CRUD - POST routes (after specific routes)
        Route::post(
            'themes',
            [ThemesController::class, 'create'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        // Themes CRUD - PUT routes (EDITABLE also accepts POST, so register AFTER specific POST routes)
        Route::put(
            self::ROUTE_THEMES_THEME_ID,
            [ThemesController::class, 'update'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        Route::delete(
            self::ROUTE_THEMES_THEME_ID,
            [ThemesController::class, 'delete'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        // Palette management
        Route::post(
            'themes/palettes',
            [ThemesPalettesController::class, 'createPalette'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        Route::put(
            'themes/palettes/{paletteId}',
            [ThemesPalettesController::class, 'updatePalette'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        Route::delete(
            'themes/palettes/{paletteId}',
            [ThemesPalettesController::class, 'deletePalette'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        // Export / Import
        Route::post(
            'themes/export/{themeId}',
            [ThemesController::class, 'export'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );

        Route::post(
            'themes/import',
            [ThemesController::class, 'import'],
            RouteAuthMiddleware::capability(ThemeCapabilities::MANAGE_FLEEKDASH_THEMES),
        );
    }
}
