<?php

use App\Http\Controllers\AgentController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\FileController;
use App\Http\Controllers\MarketingRequestController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\RequestTypeController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\WorkflowController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return auth()->check() ? redirect()->route('dashboard') : redirect()->route('login');
});

// Authentication Routes
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
});

Route::post('/logout', [AuthController::class, 'logout'])->name('logout')->middleware('auth');

// Protected Application Routes
Route::middleware(['auth'])->group(function () {

    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Marketing Requests Routes
    Route::get('/requests', [MarketingRequestController::class, 'index'])->name('requests.index');
    Route::get('/requests/create', [MarketingRequestController::class, 'create'])->name('requests.create')->middleware('role:harcourts_admin,super_admin');
    Route::post('/requests', [MarketingRequestController::class, 'store'])->name('requests.store')->middleware('role:harcourts_admin,super_admin');
    Route::get('/requests/{request}', [MarketingRequestController::class, 'show'])->name('requests.show');
    Route::post('/requests/{request}/cancel', [MarketingRequestController::class, 'cancel'])->name('requests.cancel');

    // Workflow State Action Routes
    Route::post('/requests/{request}/start-work', [WorkflowController::class, 'startWork'])->name('requests.start-work');
    Route::post('/requests/{request}/upload-proof', [WorkflowController::class, 'uploadProof'])->name('requests.upload-proof');
    Route::post('/requests/{request}/request-changes', [WorkflowController::class, 'requestChanges'])->name('requests.request-changes');
    Route::post('/requests/{request}/start-revision', [WorkflowController::class, 'startRevision'])->name('requests.start-revision');
    Route::post('/requests/{request}/approve', [WorkflowController::class, 'approve'])->name('requests.approve');
    Route::post('/requests/{request}/complete', [WorkflowController::class, 'complete'])->name('requests.complete');

    // Notifications & Polling
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    Route::get('/notifications/poll', [NotificationController::class, 'poll'])->name('notifications.poll');
    Route::post('/notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.read');
    Route::post('/notifications/read-all', [NotificationController::class, 'markAllAsRead'])->name('notifications.read-all');

    // File Downloads & Viewing
    Route::get('/files/proofs/{version}/download', [FileController::class, 'downloadProof'])->name('files.proof.download');
    Route::get('/files/proofs/{version}/view', [FileController::class, 'viewProof'])->name('files.proof.view');
    Route::get('/files/attachments/{attachment}/download', [FileController::class, 'downloadAttachment'])->name('files.attachment.download');

    // Internal Reports (Designer & Super Admin only)
    Route::get('/reports', [ReportController::class, 'index'])->name('reports.index')->middleware('role:designer,super_admin');

    // Super Admin Routes
    Route::middleware(['role:super_admin'])->prefix('admin')->name('admin.')->group(function () {
        // User Management
        Route::get('/users', [UserController::class, 'index'])->name('users.index');
        Route::get('/users/create', [UserController::class, 'create'])->name('users.create');
        Route::post('/users', [UserController::class, 'store'])->name('users.store');
        Route::get('/users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
        Route::put('/users/{user}', [UserController::class, 'update'])->name('users.update');
        Route::post('/users/{user}/toggle-status', [UserController::class, 'toggleStatus'])->name('users.toggle-status');
        Route::post('/users/{user}/reset-password', [UserController::class, 'resetPassword'])->name('users.reset-password');

        // Agent Directory
        Route::get('/agents', [AgentController::class, 'index'])->name('agents.index');
        Route::get('/agents/create', [AgentController::class, 'create'])->name('agents.create');
        Route::post('/agents', [AgentController::class, 'store'])->name('agents.store');
        Route::get('/agents/{agent}/edit', [AgentController::class, 'edit'])->name('agents.edit');
        Route::put('/agents/{agent}', [AgentController::class, 'update'])->name('agents.update');
        Route::post('/agents/{agent}/toggle-status', [AgentController::class, 'toggleStatus'])->name('agents.toggle-status');

        // Request Types Management
        Route::get('/request-types', [RequestTypeController::class, 'index'])->name('request-types.index');
        Route::post('/request-types', [RequestTypeController::class, 'store'])->name('request-types.store');
        Route::put('/request-types/{requestType}', [RequestTypeController::class, 'update'])->name('request-types.update');
    });
});
