<?php

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\SearchController;
use FleekDash\Controllers\SearchPreviewController;
use FleekDash\Controllers\SearchSettingsController;

/**
 * Search Routes
 *
 * API routes for search functionality:
 * - Universal search (CMDK)
 * - Search settings
 * - Search preview
 * - Index management
 *
 * @package FleekDash\Routes\Groups
 * @since 2.2.0
 */
class SearchRoutes
{
    /**
     * Register all search-related routes
     *
     * @return void
     */
    public static function register()
    {
        // Universal search endpoint
        Route::get('search', [SearchController::class, 'universalSearch'], RouteAuthMiddleware::public(), [
            'q' => [
                'required' => true,
                'type' => 'string',
                'description' => 'Search query',
                'minLength' => 2,
            ],
            'types' => [
                'required' => false,
                'type' => 'array',
                'description' => 'Object types to search',
                'default' => ['post', 'user', 'menu', 'setting'],
                'items' => [
                    'type' => 'string',
                    'enum' => ['post', 'user', 'menu', 'setting']
                ]
            ],
            'limit' => [
                'required' => false,
                'type' => 'integer',
                'description' => 'Maximum results to return',
                'default' => 20,
                'minimum' => 1,
                'maximum' => 50
            ]
        ]);

        // Search preview
        Route::get('search/preview/{type}/{id}', [SearchPreviewController::class, 'getPreview'], RouteAuthMiddleware::public(), [
            'type' => [
                'required' => true,
                'type' => 'string',
                'description' => 'Object type'
            ],
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => 'Object ID',
                'minimum' => 1
            ]
        ]);

        // Rebuild index (admin)
        Route::post('search/rebuild-index', [SearchController::class, 'rebuildIndex'], RouteAuthMiddleware::admin(), [
            'types' => [
                'required' => false,
                'type' => 'array',
                'description' => 'Object types to rebuild',
                'default' => ['post', 'user', 'menu', 'setting'],
                'items' => [
                    'type' => 'string',
                    'enum' => ['post', 'user', 'menu', 'setting']
                ]
            ]
        ]);

        // Search status
        Route::get('search/status', function () {
            $manager = new \FleekDash\Core\SearchIndexManager();
            return \rest_ensure_response($manager->getIndexingStatus());
        }, RouteAuthMiddleware::capability('manage_options'));

        // Search cache stats
        Route::get('search/cache-stats', function () {
            $cache_service = new \FleekDash\Services\SearchCacheService();
            return \rest_ensure_response($cache_service->getCacheStats());
        }, RouteAuthMiddleware::capability('manage_options'));

        // Search settings routes - migrated from Api.php
        Route::get('search/settings', [SearchSettingsController::class, 'getSettings'],
            RouteAuthMiddleware::authenticated());

        Route::post('search/settings', [SearchSettingsController::class, 'updateSettings'],
            RouteAuthMiddleware::admin());

        // Search synonyms
        Route::get('search/synonyms', [SearchSettingsController::class, 'getSynonyms'],
            RouteAuthMiddleware::public());

        // Admin menu ingestion
        Route::post('search/ingest-admin-menu', [SearchController::class, 'ingestAdminMenu'],
            RouteAuthMiddleware::public(), [
            'items' => [
                'required' => true,
                'type' => 'array',
                'description' => 'Admin menu items structure from client'
            ]
        ]);
    }
}
