<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Public\PublicDiscussionController;
use App\Http\Controllers\Public\PublicKnowledgeBaseController;
use App\Http\Controllers\Public\PublicContributorRegistrationController;
use App\Http\Controllers\Contributor\ContributorDashboardController;
use App\Http\Controllers\KnowledgeSharing\DiscussionController;
use App\Http\Controllers\KnowledgeSharing\DiscussionReplyController;
use App\Http\Controllers\KnowledgeSharing\DiscussionTopicController;
use App\Http\Controllers\KnowledgeSharing\KnowledgeBaseAnswerController;
use App\Http\Controllers\KnowledgeSharing\KnowledgeBaseQuestionController;
use App\Http\Controllers\KnowledgeSharing\KnowledgeBaseCategoryController;

/*
|--------------------------------------------------------------------------
| Knowledge Sharing Module Routes
|--------------------------------------------------------------------------
|
| This file contains all routes for the Knowledge Sharing module including:
| - Public discussion board routes
| - Public knowledge base routes  
| - Contributor registration routes
| - Contributor dashboard routes
| - Authenticated management routes
|
| IMPORTANT: All routes use controller-based routing (no closures) for
| route caching compatibility in production.
|
*/

/*
|--------------------------------------------------------------------------
| Public Routes (No Authentication Required)
|--------------------------------------------------------------------------
*/

// Contributor Registration Routes
Route::prefix('contributor')->name('public.contributor.')->group(function () {
    // Become a contributor landing page
    Route::get('/become', [PublicContributorRegistrationController::class, 'showBecomeContributor'])->name('become');
    
    // Registration
    Route::get('/register', [PublicContributorRegistrationController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [PublicContributorRegistrationController::class, 'register'])->name('register.post');
    
    // Login
    Route::get('/login', [PublicContributorRegistrationController::class, 'showLoginForm'])->name('login');
    
    // Email check (AJAX)
    Route::post('/check-email', [PublicContributorRegistrationController::class, 'checkEmail'])->name('check-email');
});

// Discussion Boards Public Routes
Route::prefix('discussions')->name('public.discussions.')->group(function () {
    // Landing page
    Route::get('/', [PublicDiscussionController::class, 'index'])->name('index');
    
    // Search
    Route::get('/search', [PublicDiscussionController::class, 'search'])->name('search');

    // AJAX real-time search (no auth required)
    Route::get('/ajax-search', [PublicDiscussionController::class, 'ajaxSearch'])->name('ajax-search');
    
    // Topic listing
    Route::get('/topic/{slug}', [PublicDiscussionController::class, 'topic'])->name('topic');
    
    // Single discussion
    Route::get('/{slug}', [PublicDiscussionController::class, 'show'])->name('show');
});

// Knowledge Base Public Routes
Route::prefix('knowledge-base')->name('public.knowledge-base.')->group(function () {
    // Landing page
    Route::get('/', [PublicKnowledgeBaseController::class, 'index'])->name('index');
    
    // Search
    Route::get('/search', [PublicKnowledgeBaseController::class, 'search'])->name('search');

    // AJAX real-time search (no auth required)
    Route::get('/ajax-search', [PublicKnowledgeBaseController::class, 'ajaxSearch'])->name('ajax-search');
    
    // Category listing
    Route::get('/category/{slug}', [PublicKnowledgeBaseController::class, 'category'])->name('category');
    
    // Tag listing
    Route::get('/tag/{tag}', [PublicKnowledgeBaseController::class, 'tag'])->name('tag');
    
    // Single question
    Route::get('/{slug}', [PublicKnowledgeBaseController::class, 'show'])->name('show');
});

/*
|--------------------------------------------------------------------------
| Contributor Dashboard Routes (Authentication + Contributor Role Required)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified'])->prefix('contributor')->name('contributor.')->group(function () {
    // Contributor Dashboard
    Route::get('/dashboard', [ContributorDashboardController::class, 'index'])->name('dashboard');
    
    // My Discussions
    Route::get('/discussions', [ContributorDashboardController::class, 'discussions'])->name('discussions');
    
    // My Replies
    Route::get('/replies', [ContributorDashboardController::class, 'replies'])->name('replies');
});

/*
|--------------------------------------------------------------------------
| Authenticated Routes (Authentication Required)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified'])->prefix('dashboard/knowledge-sharing')->name('knowledge-sharing.')->group(function () {
    
    /*
    |----------------------------------------------------------------------
    | Discussion Boards Management Routes
    |----------------------------------------------------------------------
    */
    Route::prefix('discussions')->name('discussions.')->group(function () {
        // Discussion CRUD
        Route::get('/', [DiscussionController::class, 'index'])->name('index')
            ->middleware('permission:view discussions');
        
        Route::get('/create', [DiscussionController::class, 'create'])->name('create')
            ->middleware('permission:create discussions');
        
        Route::post('/', [DiscussionController::class, 'store'])->name('store')
            ->middleware('permission:create discussions');

        // Static routes MUST come before /{discussion} wildcard to avoid route conflicts
        Route::get('/all-replies', [DiscussionReplyController::class, 'index'])->name('replies.index')
            ->middleware('permission:view discussions');

        Route::post('/bulk-delete', [DiscussionController::class, 'bulkDelete'])->name('bulk-delete')
            ->middleware('permission:delete discussions');

        // Topic Management Routes — static prefix, must be before /{discussion} wildcard
        Route::prefix('topics')->name('topics.')->group(function () {
            // Static routes before /{topic} wildcard
            Route::post('/update-order', [DiscussionTopicController::class, 'updateOrder'])->name('update-order')
                ->middleware('permission:edit discussion topics');

            Route::get('/', [DiscussionTopicController::class, 'index'])->name('index')
                ->middleware('permission:view discussion topics');
            
            Route::get('/create', [DiscussionTopicController::class, 'create'])->name('create')
                ->middleware('permission:create discussion topics');
            
            Route::post('/', [DiscussionTopicController::class, 'store'])->name('store')
                ->middleware('permission:create discussion topics');
            
            Route::get('/{topic}', [DiscussionTopicController::class, 'show'])->name('show')
                ->middleware('permission:view discussion topics');
            
            Route::get('/{topic}/edit', [DiscussionTopicController::class, 'edit'])->name('edit')
                ->middleware('permission:edit discussion topics');
            
            Route::put('/{topic}', [DiscussionTopicController::class, 'update'])->name('update')
                ->middleware('permission:edit discussion topics');
            
            Route::delete('/{topic}', [DiscussionTopicController::class, 'destroy'])->name('destroy')
                ->middleware('permission:delete discussion topics');
            
            Route::post('/{topic}/toggle-active', [DiscussionTopicController::class, 'toggleActive'])->name('toggle-active')
                ->middleware('permission:edit discussion topics');
        });

        // Wildcard discussion routes — must come AFTER all static prefixes above
        Route::get('/{discussion}', [DiscussionController::class, 'show'])->name('show')
            ->middleware('permission:view discussions');
        
        Route::get('/{discussion}/edit', [DiscussionController::class, 'edit'])->name('edit')
            ->middleware('permission:edit discussions');
        
        Route::put('/{discussion}', [DiscussionController::class, 'update'])->name('update')
            ->middleware('permission:edit discussions');
        
        Route::delete('/{discussion}', [DiscussionController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete discussions');
        
        // Feature/Pin actions
        Route::post('/{discussion}/toggle-featured', [DiscussionController::class, 'toggleFeatured'])->name('toggle-featured')
            ->middleware('permission:feature discussions');
        
        Route::post('/{discussion}/toggle-pinned', [DiscussionController::class, 'togglePinned'])->name('toggle-pinned')
            ->middleware('permission:pin discussions');

        // Discussion Reply Routes
        Route::prefix('{discussion}/replies')->name('replies.')->group(function () {
            Route::get('/create', [DiscussionReplyController::class, 'create'])->name('create');
            Route::post('/', [DiscussionReplyController::class, 'store'])->name('store');
            Route::get('/{reply}', [DiscussionReplyController::class, 'show'])->name('show');
            Route::get('/{reply}/edit', [DiscussionReplyController::class, 'edit'])->name('edit');
            Route::put('/{reply}', [DiscussionReplyController::class, 'update'])->name('update');
            Route::delete('/{reply}', [DiscussionReplyController::class, 'destroy'])->name('destroy');
        });
    });
    
    /*
    |----------------------------------------------------------------------
    | Knowledge Base Management Routes
    |----------------------------------------------------------------------
    */
    Route::prefix('knowledge-base')->name('knowledge-base.')->group(function () {
        // Question CRUD
        Route::get('/', [KnowledgeBaseQuestionController::class, 'index'])->name('index')
            ->middleware('permission:view knowledge base questions');
        
        Route::get('/create', [KnowledgeBaseQuestionController::class, 'create'])->name('create')
            ->middleware('permission:create knowledge base questions');
        
        Route::post('/', [KnowledgeBaseQuestionController::class, 'store'])->name('store')
            ->middleware('permission:create knowledge base questions');

        // Static routes MUST come before /{question} wildcard to avoid route conflicts
        Route::get('/all-answers', [KnowledgeBaseAnswerController::class, 'index'])->name('answers.index')
            ->middleware('permission:view knowledge base questions');

        // Category Management Routes — static prefix, must be before /{question} wildcard
        Route::prefix('categories')->name('categories.')->group(function () {
            // Static routes before /{category} wildcard
            Route::post('/update-order', [KnowledgeBaseCategoryController::class, 'updateOrder'])->name('update-order')
                ->middleware('permission:edit knowledge base categories');

            Route::get('/', [KnowledgeBaseCategoryController::class, 'index'])->name('index')
                ->middleware('permission:manage knowledge base categories');
            
            Route::get('/create', [KnowledgeBaseCategoryController::class, 'create'])->name('create')
                ->middleware('permission:create knowledge base categories');
            
            Route::post('/', [KnowledgeBaseCategoryController::class, 'store'])->name('store')
                ->middleware('permission:create knowledge base categories');
            
            Route::get('/{category}', [KnowledgeBaseCategoryController::class, 'show'])->name('show')
                ->middleware('permission:view knowledge base categories');
            
            Route::get('/{category}/edit', [KnowledgeBaseCategoryController::class, 'edit'])->name('edit')
                ->middleware('permission:edit knowledge base categories');
            
            Route::put('/{category}', [KnowledgeBaseCategoryController::class, 'update'])->name('update')
                ->middleware('permission:edit knowledge base categories');
            
            Route::delete('/{category}', [KnowledgeBaseCategoryController::class, 'destroy'])->name('destroy')
                ->middleware('permission:delete knowledge base categories');
            
            Route::post('/{category}/toggle-active', [KnowledgeBaseCategoryController::class, 'toggleActive'])->name('toggle-active')
                ->middleware('permission:edit knowledge base categories');
            
            Route::post('/{category}/move-questions', [KnowledgeBaseCategoryController::class, 'moveQuestions'])->name('move-questions')
                ->middleware('permission:edit knowledge base categories');
        });

        // Wildcard question routes — must come AFTER all static prefixes above
        Route::get('/{question}', [KnowledgeBaseQuestionController::class, 'show'])->name('show')
            ->middleware('permission:view knowledge base questions');
        
        Route::get('/{question}/edit', [KnowledgeBaseQuestionController::class, 'edit'])->name('edit')
            ->middleware('permission:edit knowledge base questions');
        
        Route::put('/{question}', [KnowledgeBaseQuestionController::class, 'update'])->name('update')
            ->middleware('permission:edit knowledge base questions');
        
        Route::delete('/{question}', [KnowledgeBaseQuestionController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete knowledge base questions');
        
        // Publish/Archive actions
        Route::post('/{question}/publish', [KnowledgeBaseQuestionController::class, 'publish'])->name('publish')
            ->middleware('permission:publish knowledge base questions');
        
        Route::post('/{question}/archive', [KnowledgeBaseQuestionController::class, 'archive'])->name('archive')
            ->middleware('permission:archive knowledge base questions');
        
        Route::post('/{question}/toggle-featured', [KnowledgeBaseQuestionController::class, 'toggleFeatured'])->name('toggle-featured')
            ->middleware('permission:feature knowledge base questions');
        
        // Knowledge Base Answer Routes
        Route::prefix('{question}/answers')->name('answers.')->group(function () {
            Route::get('/create', [KnowledgeBaseAnswerController::class, 'create'])->name('create');
            Route::post('/', [KnowledgeBaseAnswerController::class, 'store'])->name('store');
            Route::get('/{answer}', [KnowledgeBaseAnswerController::class, 'show'])->name('show');
            Route::get('/{answer}/edit', [KnowledgeBaseAnswerController::class, 'edit'])->name('edit');
            Route::put('/{answer}', [KnowledgeBaseAnswerController::class, 'update'])->name('update');
            Route::delete('/{answer}', [KnowledgeBaseAnswerController::class, 'destroy'])->name('destroy');
            Route::post('/{answer}/accept', [KnowledgeBaseAnswerController::class, 'accept'])->name('accept');
            Route::post('/{answer}/helpful', [KnowledgeBaseAnswerController::class, 'toggleHelpful'])->name('helpful');
        });
    });
});

/*
|--------------------------------------------------------------------------
| API Routes (For AJAX/Alpine.js Interactions)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->prefix('api/knowledge-sharing')->name('api.knowledge-sharing.')->group(function () {
    
    // Discussion API endpoints
    Route::prefix('discussions')->name('discussions.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Api\DiscussionApiController::class, 'index'])->name('index');
        Route::post('/{discussion}/like', [\App\Http\Controllers\Api\DiscussionApiController::class, 'toggleLike'])->name('like');
        Route::post('/{discussion}/bookmark', [\App\Http\Controllers\Api\DiscussionApiController::class, 'toggleBookmark'])->name('bookmark');
        // Reply API
        Route::post('/{discussion}/replies', [\App\Http\Controllers\Api\DiscussionReplyApiController::class, 'store'])->name('replies.store');
        Route::put('/{discussion}/replies/{reply}', [\App\Http\Controllers\Api\DiscussionReplyApiController::class, 'update'])->name('replies.update');
        Route::delete('/{discussion}/replies/{reply}', [\App\Http\Controllers\Api\DiscussionReplyApiController::class, 'destroy'])->name('replies.destroy');
        Route::post('/replies/{reply}/like', [\App\Http\Controllers\Api\DiscussionReplyApiController::class, 'toggleLike'])->name('replies.like');
    });
    
    // Knowledge Base API endpoints
    Route::prefix('knowledge-base')->name('knowledge-base.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Api\KnowledgeBaseApiController::class, 'index'])->name('index');
        Route::post('/{question}/helpful', [\App\Http\Controllers\Api\KnowledgeBaseApiController::class, 'markHelpful'])->name('helpful');
        Route::post('/answers/{answer}/accept', [\App\Http\Controllers\Api\KnowledgeBaseApiController::class, 'acceptAnswer'])->name('accept-answer');
        // Answer API
        Route::post('/{question}/answers', [\App\Http\Controllers\Api\KnowledgeBaseAnswerApiController::class, 'store'])->name('answers.store');
        Route::put('/{question}/answers/{answer}', [\App\Http\Controllers\Api\KnowledgeBaseAnswerApiController::class, 'update'])->name('answers.update');
        Route::delete('/{question}/answers/{answer}', [\App\Http\Controllers\Api\KnowledgeBaseAnswerApiController::class, 'destroy'])->name('answers.destroy');
        Route::post('/answers/{answer}/helpful', [\App\Http\Controllers\Api\KnowledgeBaseAnswerApiController::class, 'toggleHelpful'])->name('answers.helpful');
        // Bookmark (existing)
        Route::post('/{question}/bookmark', [\App\Http\Controllers\Api\KnowledgeBaseApiController::class, 'toggleBookmark'])->name('bookmark');
    });
    
    // Search API
    Route::get('/search', [\App\Http\Controllers\Api\SearchApiController::class, 'search'])->name('search');
    Route::get('/search/suggestions', [\App\Http\Controllers\Api\SearchApiController::class, 'suggestions'])->name('search.suggestions');
});
