<?php

use App\Http\Controllers\TicketController;
use App\Http\Controllers\SmsController;
use App\Http\Controllers\EmailController;
use App\Http\Controllers\ChatController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Communication Module Routes
|--------------------------------------------------------------------------
|
| These routes handle all communication-related functionality:
| - Ticketing System
| - SMS System
| - Email System
| - Chat System
|
| All routes require authentication and proper permissions.
|
| IMPORTANT: Static routes (e.g. /templates, /history, /compose) MUST be
| defined BEFORE wildcard routes (e.g. /{email}) to avoid the wildcard
| capturing the static segments.
|
*/

// ============================================
// PUBLIC CHAT ROUTES (no auth required)
// ============================================
Route::prefix('chat')->name('chat.')->group(function () {
    // Public chat start page for visitors (no-JS fallback)
    Route::get('/start', [ChatController::class, 'publicStart'])->name('public.start');
    Route::post('/start', [ChatController::class, 'publicCreate'])->name('public.create');
    Route::get('/session/{sessionId}', [ChatController::class, 'publicSession'])->name('public.session');
    Route::post('/session/{sessionId}/message', [ChatController::class, 'publicMessage'])->name('public.message');
    Route::post('/session/{sessionId}/end', [ChatController::class, 'publicEnd'])->name('public.end');

    // JSON API endpoints used by the floating chat widget (AJAX)
    Route::post('/api/start', [ChatController::class, 'publicCreateJson'])->name('public.api.start');
    Route::post('/api/session/{sessionId}/message', [ChatController::class, 'publicMessageJson'])->name('public.api.message');
    Route::post('/api/session/{sessionId}/end', [ChatController::class, 'publicEndJson'])->name('public.api.end');

    // Polling endpoint — visitor polls for new messages (no auth)
    Route::get('/session/{sessionId}/poll', [ChatController::class, 'publicPoll'])->name('public.poll');
});

Route::middleware(['auth', 'verified'])->group(function () {

    // ============================================
    // TICKETING SYSTEM ROUTES
    // ============================================
    Route::prefix('tickets')->name('tickets.')->group(function () {
        Route::get('/', [TicketController::class, 'index'])->name('index');
        Route::get('/create', [TicketController::class, 'create'])->name('create');
        Route::post('/', [TicketController::class, 'store'])->name('store');

        // Static routes BEFORE wildcard /{ticket}
        Route::get('/{ticket}', [TicketController::class, 'show'])->name('show');
        Route::get('/{ticket}/edit', [TicketController::class, 'edit'])->name('edit');
        Route::put('/{ticket}', [TicketController::class, 'update'])->name('update');
        Route::delete('/{ticket}', [TicketController::class, 'destroy'])->name('destroy');

        // Ticket status and assignment
        Route::put('/{ticket}/status', [TicketController::class, 'updateStatus'])->name('update-status');
        Route::put('/{ticket}/assign', [TicketController::class, 'assign'])->name('assign');
        Route::post('/{ticket}/reply', [TicketController::class, 'reply'])->name('reply');
    });

    // ============================================
    // SMS SYSTEM ROUTES
    // ============================================
    Route::prefix('sms')->name('sms.')->group(function () {
        Route::get('/', [SmsController::class, 'index'])->name('index');

        // Static routes BEFORE wildcard /{sms}
        Route::get('/compose', [SmsController::class, 'create'])->name('compose');
        Route::get('/history', [SmsController::class, 'history'])->name('history');

        // Template routes (all static, must be before /{sms})
        Route::get('/templates', [SmsController::class, 'templates'])->name('templates');
        Route::get('/templates/create', [SmsController::class, 'createTemplate'])->name('create-template');
        Route::post('/templates', [SmsController::class, 'storeTemplate'])->name('store-template');
        Route::get('/templates/{template}/edit', [SmsController::class, 'editTemplate'])->name('edit-template');
        Route::put('/templates/{template}', [SmsController::class, 'updateTemplate'])->name('update-template');
        Route::delete('/templates/{template}', [SmsController::class, 'destroyTemplate'])->name('destroy-template');

        Route::get('/users-by-role/{role}', [SmsController::class, 'getUsersByRole'])->name('users-by-role');

        Route::post('/', [SmsController::class, 'store'])->name('store');

        // Wildcard routes LAST
        Route::get('/{sms}', [SmsController::class, 'show'])->name('show');
        Route::delete('/{sms}', [SmsController::class, 'destroy'])->name('destroy');
    });

    // ============================================
    // EMAIL SYSTEM ROUTES
    // ============================================
    Route::prefix('emails')->name('emails.')->group(function () {
        Route::get('/', [EmailController::class, 'index'])->name('index');

        // Static routes BEFORE wildcard /{email}
        Route::get('/compose', [EmailController::class, 'create'])->name('compose');
        Route::get('/history', [EmailController::class, 'history'])->name('history');
        Route::get('/users-by-role/{role}', [EmailController::class, 'getUsersByRole'])->name('users-by-role');

        // Template routes (all static, must be before /{email})
        Route::get('/templates', [EmailController::class, 'templates'])->name('templates');
        Route::get('/templates/create', [EmailController::class, 'createTemplate'])->name('create-template');
        Route::post('/templates', [EmailController::class, 'storeTemplate'])->name('store-template');
        Route::get('/templates/{template}/edit', [EmailController::class, 'editTemplate'])->name('edit-template');
        Route::put('/templates/{template}', [EmailController::class, 'updateTemplate'])->name('update-template');
        Route::delete('/templates/{template}', [EmailController::class, 'destroyTemplate'])->name('destroy-template');

        Route::post('/', [EmailController::class, 'store'])->name('store');

        // Wildcard routes LAST
        Route::get('/{email}', [EmailController::class, 'show'])->name('show');
        Route::delete('/{email}', [EmailController::class, 'destroy'])->name('destroy');

        // Reply functionality (after wildcard is fine since they have sub-paths)
        Route::get('/{email}/reply', [EmailController::class, 'reply'])->name('reply');
        Route::post('/{email}/reply', [EmailController::class, 'sendReply'])->name('send-reply');
    });

    // ============================================
    // CHAT SYSTEM ROUTES
    // ============================================
    Route::prefix('chat')->name('chat.')->group(function () {
        Route::get('/', [ChatController::class, 'index'])->name('index');

        // Static routes BEFORE any wildcards
        Route::get('/categories', [ChatController::class, 'categories'])->name('categories');
        Route::post('/categories', [ChatController::class, 'storeCategory'])->name('store-category');
        Route::put('/categories/{category}', [ChatController::class, 'updateCategory'])->name('update-category');
        Route::delete('/categories/{category}', [ChatController::class, 'destroyCategory'])->name('destroy-category');

        Route::get('/agents', [ChatController::class, 'agents'])->name('agents');
        Route::post('/agents', [ChatController::class, 'appointAgent'])->name('appoint-agent');
        Route::delete('/agents/{agent}', [ChatController::class, 'removeAgent'])->name('remove-agent');

        Route::get('/conversations', [ChatController::class, 'conversations'])->name('conversations');
        Route::get('/conversations/{conversation}', [ChatController::class, 'showConversation'])->name('conversation.show');
        Route::post('/conversations/{conversation}/assign', [ChatController::class, 'assignAgent'])->name('conversation.assign');
        Route::post('/conversations/{conversation}/message', [ChatController::class, 'sendMessage'])->name('conversation.message');
        Route::post('/conversations/{conversation}/end', [ChatController::class, 'endConversation'])->name('conversation.end');
        Route::post('/conversations/{conversation}/transfer', [ChatController::class, 'transferConversation'])->name('conversation.transfer');

        Route::post('/status', [ChatController::class, 'updateStatus'])->name('update-status');
        Route::get('/agent-dashboard', [ChatController::class, 'agentDashboard'])->name('agent-dashboard');

        // Agent polling endpoint — fetch new messages without full page reload
        Route::get('/conversations/{conversation}/poll', [ChatController::class, 'agentPoll'])->name('conversation.poll');
    });
});
