<?php

use App\Http\Controllers\Api\HeaderNotificationController;
use App\Http\Controllers\Api\HeaderSearchController;
use App\Http\Controllers\Api\DiscussionApiController;
use App\Http\Controllers\Api\KnowledgeBaseApiController;
use App\Http\Controllers\Api\SearchApiController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Header AJAX endpoints for realtime notifications and role-scoped search.
| Knowledge Sharing API endpoints for discussions and knowledge base.
| All routes are protected by the auth middleware.
|
*/

Route::middleware('auth')->group(function () {

    // =========================================================================
    // Header — Live Search & Notifications
    // =========================================================================

    Route::get('/header/search', [HeaderSearchController::class, 'search'])
        ->name('api.header.search');

    Route::get('/header/notifications', [HeaderNotificationController::class, 'index'])
        ->name('api.header.notifications');

    Route::post('/header/notifications/read-all', [HeaderNotificationController::class, 'markAllRead'])
        ->name('api.header.notifications.read-all');

    Route::post('/header/notifications/{id}/read', [HeaderNotificationController::class, 'markRead'])
        ->name('api.header.notifications.read');

    // =========================================================================
    // Discussion API
    // =========================================================================

    Route::prefix('discussions')->name('api.discussions.')->group(function () {
        Route::get('/', [DiscussionApiController::class, 'index'])
            ->name('index');

        Route::post('/{discussion}/like', [DiscussionApiController::class, 'toggleLike'])
            ->name('like');

        Route::post('/{discussion}/bookmark', [DiscussionApiController::class, 'toggleBookmark'])
            ->name('bookmark');

        Route::get('/bookmarks', [DiscussionApiController::class, 'bookmarks'])
            ->name('bookmarks');

        Route::get('/likes', [DiscussionApiController::class, 'likes'])
            ->name('likes');
    });

    // =========================================================================
    // Knowledge Base API
    // =========================================================================

    Route::prefix('knowledge-base')->name('api.knowledge-base.')->group(function () {
        Route::get('/', [KnowledgeBaseApiController::class, 'index'])
            ->name('index');

        Route::post('/questions/{question}/helpful', [KnowledgeBaseApiController::class, 'markHelpful'])
            ->name('questions.helpful');

        Route::post('/questions/{question}/bookmark', [KnowledgeBaseApiController::class, 'toggleBookmark'])
            ->name('questions.bookmark');

        Route::get('/questions/{question}/related', [KnowledgeBaseApiController::class, 'related'])
            ->name('questions.related');

        Route::post('/answers/{answer}/accept', [KnowledgeBaseApiController::class, 'acceptAnswer'])
            ->name('answers.accept');

        Route::post('/answers/{answer}/vote', [KnowledgeBaseApiController::class, 'voteAnswer'])
            ->name('answers.vote');
    });

    // =========================================================================
    // Global Search API
    // =========================================================================

    Route::prefix('search')->name('api.search.')->group(function () {
        Route::get('/', [SearchApiController::class, 'search'])
            ->name('index');

        Route::get('/suggestions', [SearchApiController::class, 'suggestions'])
            ->name('suggestions');

        Route::get('/popular', [SearchApiController::class, 'popular'])
            ->name('popular');

        Route::get('/recent', [SearchApiController::class, 'recent'])
            ->name('recent');
    });

    // =========================================================================
    // Health Directory Search API
    // =========================================================================

    // Health institution and digital machine search endpoints are registered
    // in routes/health-directory.php as /api/institutions/search and
    // /api/machines/search — they are public (no auth required) and handled
    // by HealthInstitutionController and DigitalMachineController respectively.

});
