<?php

/**
 * Routes: SSHIA Module Routes
 * 
 * Controller-based routes for Research, Advocacy, and Partnership modules.
 * All routes use controller-based routing (no closures).
 * 
 * Naming Convention: All models, controllers, and tables use 'r' prefix.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Research\RPublicationController;
use App\Http\Controllers\Research\RReviewController;
use App\Http\Controllers\Advocacy\RCampaignController;
use App\Http\Controllers\Advocacy\RHealthStoryController;
use App\Http\Controllers\Advocacy\RPollController;
use App\Http\Controllers\Partnership\RPartnerController;
use App\Http\Controllers\Partnership\RForumController;

/*
|--------------------------------------------------------------------------
| Research & Innovation Centre Routes
|--------------------------------------------------------------------------
*/

// Public Research Routes (Site Layout)
Route::prefix('research')->name('research.public.')->group(function () {
    Route::get('/', [RPublicationController::class, 'publicIndex'])->name('index');
    Route::get('/submit', [RPublicationController::class, 'publicSubmitForm'])->name('submit');
    Route::post('/submit', [RPublicationController::class, 'publicSubmitStore'])->name('submit.store');
    Route::get('/submit/authenticate', [RPublicationController::class, 'publicSubmitAuth'])->name('submit.auth');
    Route::get('/ajax/search', [RPublicationController::class, 'publicAjaxSearch'])->name('ajax.search');
    Route::get('/featured', [RPublicationController::class, 'featured'])->name('featured');
    Route::get('/{publication}', [RPublicationController::class, 'publicShow'])->name('show');
});

// Review Routes (Manager/Super Manager only)
// IMPORTANT: Must be registered BEFORE the authenticated research group
// to prevent the /{publication} wildcard from swallowing /review.
Route::middleware(['auth', 'verified', 'role:Manager|Super Manager'])
    ->prefix('dashboard/research/review')
    ->name('research.review.')
    ->group(function () {
        Route::get('/pending-count', [RReviewController::class, 'pendingCount'])->name('pending-count');
        Route::get('/', [RReviewController::class, 'index'])->name('index');
        Route::get('/{publication}', [RReviewController::class, 'show'])->name('show');
        Route::post('/{publication}', [RReviewController::class, 'store'])->name('store');
        Route::post('/{publication}/feature', [RReviewController::class, 'feature'])->name('feature');
        Route::delete('/{publication}/feature', [RReviewController::class, 'unfeature'])->name('unfeature');
        Route::get('/{publication}/history', [RReviewController::class, 'history'])->name('history');
        Route::get('/{publication}/scores', [RReviewController::class, 'scores'])->name('scores');
        Route::post('/{publication}/quick-approve', [RReviewController::class, 'quickApprove'])->name('quick-approve');
    });

// Authenticated Research Routes (App Layout)
Route::middleware(['auth', 'verified'])->prefix('dashboard/research')->name('research.')->group(function () {
    // Static routes MUST come before wildcard /{publication}
    Route::get('/', [RPublicationController::class, 'index'])->name('index');
    Route::get('/create', [RPublicationController::class, 'create'])->name('create');
    Route::post('/', [RPublicationController::class, 'store'])->name('store');

    // Pending session → draft (must be before /{publication} wildcard)
    Route::get('/pending/create', [RPublicationController::class, 'createDraftFromPendingSession'])->name('pending.create');

    // AJAX endpoints (must be before /{publication} wildcard)
    Route::post('/search', [RPublicationController::class, 'search'])->name('search.ajax');

    // Wildcard publication routes
    Route::get('/{publication}', [RPublicationController::class, 'show'])->name('show');
    Route::get('/{publication}/edit', [RPublicationController::class, 'edit'])->name('edit');
    Route::put('/{publication}', [RPublicationController::class, 'update'])->name('update');
    Route::delete('/{publication}', [RPublicationController::class, 'destroy'])->name('destroy');

    // Submission actions
    Route::post('/{publication}/submit', [RPublicationController::class, 'submitForReview'])->name('submit');
    Route::delete('/{publication}/files/{file}', [RPublicationController::class, 'deleteFile'])->name('files.destroy');

    // Private file download
    Route::get('/files/{file}/download', [RPublicationController::class, 'downloadFile'])->name('files.download');
});

/*
|--------------------------------------------------------------------------
| Digital Health Advocacy Routes
|--------------------------------------------------------------------------
*/

// Public Advocacy Routes (Site Layout)
Route::prefix('advocacy')->name('advocacy.public.')->group(function () {
    Route::get('/', [RCampaignController::class, 'publicIndex'])->name('index');
    Route::get('/campaigns/{campaign}', [RCampaignController::class, 'publicShow'])->name('campaigns.show');
    
    // Public Stories
    Route::get('/stories', [RHealthStoryController::class, 'publicIndex'])->name('stories.index');
    Route::get('/stories/{story}', [RHealthStoryController::class, 'publicShow'])->name('stories.show');
    Route::post('/stories/{story}/share', [RHealthStoryController::class, 'share'])->name('stories.share');
    
    // Public Story Submission
    Route::get('/submit-story', [RHealthStoryController::class, 'create'])->name('stories.create');
    Route::post('/submit-story', [RHealthStoryController::class, 'store'])->name('stories.store');
    
    // Public Poll Voting (AJAX)
    Route::get('/polls/active', [RPollController::class, 'active'])->name('polls.active');
    Route::post('/polls/{poll}/vote', [RPollController::class, 'vote'])->name('polls.vote');
    Route::get('/polls/{poll}/results', [RPollController::class, 'results'])->name('polls.results');
});

// Authenticated Advocacy Routes (App Layout)
Route::middleware(['auth', 'verified'])->prefix('dashboard/advocacy')->name('advocacy.')->group(function () {
    // Campaign Management
    Route::prefix('campaigns')->name('campaigns.')->group(function () {
        Route::get('/', [RCampaignController::class, 'index'])->name('index');
        Route::get('/create', [RCampaignController::class, 'create'])->name('create');
        Route::post('/', [RCampaignController::class, 'store'])->name('store');
        Route::get('/{campaign}', [RCampaignController::class, 'show'])->name('show');
        Route::get('/{campaign}/edit', [RCampaignController::class, 'edit'])->name('edit');
        Route::put('/{campaign}', [RCampaignController::class, 'update'])->name('update');
        Route::delete('/{campaign}', [RCampaignController::class, 'destroy'])->name('destroy');
        Route::post('/{campaign}/toggle-status', [RCampaignController::class, 'toggleStatus'])->name('toggle-status');
        Route::post('/{campaign}/toggle-featured', [RCampaignController::class, 'toggleFeatured'])->name('toggle-featured');
    });
    
    // Story Moderation
    Route::prefix('stories')->name('stories.')->group(function () {
        Route::get('/', [RHealthStoryController::class, 'index'])->name('index');
        // Alias: 'advocacy.stories.moderation' → same as 'advocacy.stories.index'
        Route::get('/moderation', [RHealthStoryController::class, 'index'])->name('moderation');
        Route::get('/{story}', [RHealthStoryController::class, 'show'])->name('show');
        // Alias: 'advocacy.stories.moderate' → same as 'advocacy.stories.show'
        Route::get('/{story}/moderate', [RHealthStoryController::class, 'show'])->name('moderate');
        Route::post('/{story}/approve', [RHealthStoryController::class, 'approve'])->name('approve');
        Route::post('/{story}/reject', [RHealthStoryController::class, 'reject'])->name('reject');
        Route::post('/{story}/toggle-featured', [RHealthStoryController::class, 'toggleFeatured'])->name('toggle-featured');
        Route::delete('/{story}', [RHealthStoryController::class, 'destroy'])->name('destroy');
    });
    
    // Poll Management
    Route::prefix('polls')->name('polls.')->group(function () {
        Route::get('/', [RPollController::class, 'index'])->name('index');
        Route::get('/create', [RPollController::class, 'create'])->name('create');
        Route::post('/', [RPollController::class, 'store'])->name('store');
        Route::get('/{poll}', [RPollController::class, 'show'])->name('show');
        Route::get('/{poll}/edit', [RPollController::class, 'edit'])->name('edit');
        Route::put('/{poll}', [RPollController::class, 'update'])->name('update');
        Route::delete('/{poll}', [RPollController::class, 'destroy'])->name('destroy');
        Route::post('/{poll}/close', [RPollController::class, 'close'])->name('close');
    });
});

/*
|--------------------------------------------------------------------------
| Partnership & Collaboration Portal Routes
|--------------------------------------------------------------------------
*/

// Public Partner Directory (Site Layout)
Route::prefix('partners')->name('partners.public.')->group(function () {
    Route::get('/', [RPartnerController::class, 'publicIndex'])->name('index');
    Route::get('/search', [RPartnerController::class, 'publicSearch'])->name('search');
    // Static routes MUST come before wildcard /{partner} to avoid being swallowed
    Route::get('/become-partner', [RPartnerController::class, 'becomePartner'])->name('become-partner');
    Route::post('/apply', [RPartnerController::class, 'apply'])->name('apply');
    Route::get('/{partner}', [RPartnerController::class, 'publicShow'])->name('show');
});

// Authenticated Partnership Routes (App Layout)
Route::middleware(['auth', 'verified'])->prefix('dashboard/partner-management')->name('partners.')->group(function () {
    // ── Static / non-wildcard routes MUST come before /{partner} wildcard ──

    // Partner Management index & create
    Route::get('/', [RPartnerController::class, 'index'])->name('index');
    Route::get('/create', [RPartnerController::class, 'create'])->name('create');
    Route::post('/', [RPartnerController::class, 'store'])->name('store');

    // Expiring (static path — must be before /{partner} wildcard)
    Route::get('/expiring', [RPartnerController::class, 'expiring'])->name('expiring');

    // Forum (static prefix — must be before /{partner} wildcard)
    Route::prefix('forum')->name('forum.')->group(function () {
        Route::get('/', [RForumController::class, 'index'])->name('index');
        Route::get('/create', [RForumController::class, 'create'])->name('create');
        Route::post('/', [RForumController::class, 'store'])->name('store');

        // AJAX real-time search
        Route::get('/search', [RForumController::class, 'search'])->name('search');

        // Reply actions (static prefix — must be before /{thread} wildcard)
        Route::get('/replies/{reply}/edit', [RForumController::class, 'editReply'])->name('replies.edit');
        Route::put('/replies/{reply}', [RForumController::class, 'updateReply'])->name('replies.update');
        Route::delete('/replies/{reply}', [RForumController::class, 'destroyReply'])->name('replies.destroy');
        Route::post('/replies/{reply}/best-answer', [RForumController::class, 'markBestAnswer'])->name('replies.best-answer');
        Route::post('/replies/{reply}/helpful', [RForumController::class, 'markHelpful'])->name('replies.helpful');

        // Attachment management
        Route::get('/attachments/{attachment}/download', [RForumController::class, 'downloadAttachment'])->name('attachments.download');
        Route::delete('/attachments/{attachment}', [RForumController::class, 'deleteAttachment'])->name('attachments.destroy');

        // Wildcard thread routes (after all static forum routes)
        Route::get('/{thread}', [RForumController::class, 'show'])->name('show');
        Route::delete('/{thread}', [RForumController::class, 'destroy'])->name('destroy');
        Route::post('/{thread}/reply', [RForumController::class, 'reply'])->name('reply');
        Route::post('/{thread}/toggle-lock', [RForumController::class, 'toggleLock'])->name('toggle-lock');
        Route::post('/{thread}/toggle-pin', [RForumController::class, 'togglePin'])->name('toggle-pin');
        Route::post('/{thread}/mark-read', [RForumController::class, 'markRead'])->name('mark-read');
        Route::post('/{thread}/mark-unread', [RForumController::class, 'markUnread'])->name('mark-unread');
    });

    // ── Wildcard partner routes (must come LAST) ──
    Route::get('/{partner}', [RPartnerController::class, 'show'])->name('show');
    Route::get('/{partner}/edit', [RPartnerController::class, 'edit'])->name('edit');
    Route::put('/{partner}', [RPartnerController::class, 'update'])->name('update');
    Route::delete('/{partner}', [RPartnerController::class, 'destroy'])->name('destroy');
    Route::post('/{partner}/verify', [RPartnerController::class, 'verify'])->name('verify');
    Route::post('/{partner}/toggle-featured', [RPartnerController::class, 'toggleFeatured'])->name('toggle-featured');
});

/*
|--------------------------------------------------------------------------
| API Routes for AJAX Operations
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->prefix('api')->name('api.')->group(function () {
    // Research API
    Route::prefix('research')->name('research.')->group(function () {
        Route::post('/search', [RPublicationController::class, 'search'])->name('search');
    });
    
    // Advocacy API
    Route::prefix('advocacy')->name('advocacy.')->group(function () {
        Route::post('/polls/{poll}/vote', [RPollController::class, 'vote'])->name('polls.vote');
        Route::get('/polls/{poll}/results', [RPollController::class, 'results'])->name('polls.results');
    });
    
    // Partnership API
    Route::prefix('partners')->name('partners.')->group(function () {
        Route::post('/{partner}/verify', [RPartnerController::class, 'verify'])->name('verify');
        Route::post('/{partner}/toggle-featured', [RPartnerController::class, 'toggleFeatured'])->name('toggle-featured');
        Route::post('/forum/{thread}/toggle-lock', [RForumController::class, 'toggleLock'])->name('forum.toggle-lock');
        Route::post('/forum/{thread}/toggle-pin', [RForumController::class, 'togglePin'])->name('forum.toggle-pin');
        Route::post('/forum/{thread}/mark-read', [RForumController::class, 'markRead'])->name('forum.mark-read');
        Route::post('/forum/{thread}/mark-unread', [RForumController::class, 'markUnread'])->name('forum.mark-unread');
        Route::post('/forum/replies/{reply}/best-answer', [RForumController::class, 'markBestAnswer'])->name('forum.replies.best-answer');
        Route::post('/forum/replies/{reply}/helpful', [RForumController::class, 'markHelpful'])->name('forum.replies.helpful');
        Route::delete('/forum/attachments/{attachment}', [RForumController::class, 'deleteAttachment'])->name('forum.attachments.destroy');
    });
});
