<?php
// Workflow CRUD routes
if (file_exists(__DIR__.'/workflow/workflow.php')) {
    require __DIR__.'/workflow/workflow.php';
}

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FolderController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\DepartmentSetupQuestionnaireController;

Route::get('/', function () {
    return redirect('/login');
});

Route::get('/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified', 'permission:Access Dashboard'])
    ->name('dashboard');

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

    Route::get('/department-setup-questionnaire', [DepartmentSetupQuestionnaireController::class, 'create'])
        ->name('workflow.department-setup-questionnaire');
    Route::post('/department-setup-questionnaire', [DepartmentSetupQuestionnaireController::class, 'store'])
        ->name('workflow.department-setup-questionnaire.store');
    Route::get('/department-setup-questionnaire/submissions', [DepartmentSetupQuestionnaireController::class, 'index'])
        ->name('workflow.department-setup-questionnaire.submissions');
    Route::get('/department-setup-questionnaire/export-csv', [DepartmentSetupQuestionnaireController::class, 'exportCsv'])
        ->name('workflow.department-setup-questionnaire.export-csv');

Route::controller(DashboardController::class)->group(function () {
        Route::get('/users', 'index')->middleware('permission:View Users')->name('users.index');
    });

    // Profile routes
    Route::get('/profile', [ProfileController::class, 'edit'])->middleware('permission:Edit Profile')->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->middleware('permission:Edit Profile')->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->middleware('permission:Delete Profile')->name('profile.destroy');

    // User management routes
    Route::controller(UserController::class)->group(function () {
        Route::get('/users', 'index')->middleware('permission:View Users')->name('users.index');
        Route::post('/users', 'store')->middleware('permission:Create User')->name('users.store');
        Route::put('/users/{user}', 'update')->middleware('permission:Edit User')->name('users.update');
        Route::delete('/users/{user}', 'destroy')->middleware('permission:Delete User')->name('users.destroy');
    });

    // Role management routes
    Route::controller(RoleController::class)->group(function () {
        Route::get('/roles', 'index')->middleware('permission:View Roles')->name('roles.index');
        Route::get('/roles/create', 'create')->middleware('permission:Create Role')->name('roles.create');
        Route::post('/roles', 'store')->middleware('permission:Create Role')->name('roles.store');
        Route::put('/roles/{role}', 'update')->middleware('permission:Edit Role')->name('roles.update');
        Route::delete('/roles/{role}', 'destroy')->middleware('permission:Delete Role')->name('roles.destroy');
    });

    // Folder management routes
    Route::controller(FolderController::class)->group(function () {
    // Info route for 'my folders' (shared/personal folders)
    Route::get('/my-folders/{folder}/info', 'info')->middleware('permission:View My Folder Info')->name('my-folders.info');
    Route::get('/folders/{folder}/info', 'info')->middleware('permission:View Folder')->name('folders.info');
        Route::get('/folders', 'index')->middleware('permission:View Folder')->name('folders.index');
        Route::get('/folders/create', 'create')->middleware('permission:Create Folder')->name('folders.create');
        Route::post('/folders', 'store')->middleware('permission:Create Folder')->name('folders.store');
        Route::get('/folders/{folder}', 'show')->middleware('permission:View Folder')->name('folders.show');
        Route::get('/folders/{folder}/edit', 'edit')->middleware('permission:Edit Folder')->name('folders.edit');
        Route::put('/folders/{folder}', 'update')->middleware('permission:Edit Folder')->name('folders.update');
        Route::delete('/folders/{folder}', 'destroy')->middleware('auth')->name('folders.destroy');
        Route::post('/folders/share', 'share')->middleware('permission:Share Folder')->name('folders.share');
        Route::get('/folders/{folder}/shared-access', 'getSharedAccess')->middleware('permission:Manage Folder Permissions')->name('folders.get-shared-access');
        Route::post('/folders/{folder}/remove-user-access', 'removeUserAccess')->middleware('permission:Manage Folder Permissions')->name('folders.remove-user-access');
        Route::post('/folders/{folder}/remove-role-access', 'removeRoleAccess')->middleware('permission:Manage Folder Permissions')->name('folders.remove-role-access');
        Route::get('/my-folders', 'myFolders')->middleware('permission:Access Shared Folders')->name('folders.my');
        Route::post('/my-folders/create', 'store')->middleware('permission:Create My Folder')->name('my-folders.store');
        Route::get('/folders/{id}/contents', 'getFolderContents')->middleware('auth')->name('folders.contents');
        Route::put('/folders/{id}/rename', 'renameFolder')->middleware('permission:Edit My Folder')->name('folders.rename');
        Route::delete('/my-folders/{folder}', 'destroy')->middleware('permission:Delete My Folder')->name('folders.destroy');
    });
    
    // File management routes
    Route::controller(\App\Http\Controllers\FileController::class)->group(function () {
        Route::post('/files/update-comment', 'updateComment')->middleware('permission:Upload My Files')->name('files.updateComment');
        Route::post('/files/upload', 'upload')->middleware('permission:Upload My Files')->name('files.upload');
        Route::post('/files/view', 'view')->middleware('permission:Download My Files')->name('files.view');
        Route::post('/files/view-content', 'ajaxViewContent')->middleware('permission:Download My Files')->name('files.view-content');
        Route::post('/files/download', 'download')->middleware('permission:Download My Files')->name('files.download');
        Route::delete('/files/delete', 'delete')->middleware('permission:Delete My Files')->name('files.delete');
        Route::post('/files/metadata', 'getMetadata')->middleware('permission:View File Metadata')->name('files.metadata');
        Route::post('/files/search', 'searchByMetadata')->middleware('permission:Search Files')->name('files.search');
        Route::post('/files/share', 'share')->middleware('permission:Share My Files')->name('files.share');
        Route::get('/files/{file}/shared-access', 'getSharedAccess')->middleware('permission:Manage File Permissions')->name('files.get-shared-access');
        Route::post('/files/{file}/remove-user-access', 'removeUserAccess')->middleware('permission:Manage File Permissions')->name('files.remove-user-access');
        // PDF preview route for AJAX (no download headers)
        Route::get('/files/ajax-preview-pdf', [\App\Http\Controllers\FileController::class, 'ajaxPreviewPdf'])
            ->middleware('permission:Download My Files')
            ->name('files.ajax-preview-pdf');
        Route::get('/files/file-mgt', function () {
        return view('files.file-mgt');
    })->name('files.file-mgt');    
    });

    // File Management Page (Blade view)
    
    
    Route::middleware('auth')->group(function () {
    // Profile routes
    Route::get('/profile', [ProfileController::class, 'edit'])->middleware('permission:Edit Profile')->name('profile.edit');
    Route::post('/notifications/read/{id}', [NotificationController::class, 'read'])->name('notifications.read');
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    // ... other routes ...
});

Route::get('/notifications/unread-count', function () {
    return response()->json(['unread' => auth()->user()->unreadNotifications()->count()]);
})->middleware('web');

    Route::middleware(['auth', 'verified'])->group(function () {
        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/folder-file-stats', [ReportController::class, 'folderFileStats'])->name('reports.folder-file-stats');
                Route::get('/reports/folder-file-stats/pdf', [ReportController::class, 'folderFileStatsPdf'])->name('reports.folder-file-stats.pdf');
        Route::get('/reports/user-report', [ReportController::class, 'userReport'])->name('reports.user-report');
                Route::get('/reports/user-report/pdf', [ReportController::class, 'userReportPdf'])->name('reports.user-report.pdf');
        Route::get('/reports/workflow-report', [ReportController::class, 'workflowReport'])->name('reports.workflow-report');
                Route::get('/reports/workflow-report/pdf', [ReportController::class, 'workflowReportPdf'])->name('reports.workflow-report.pdf');
                Route::get('/reports/task-report', [ReportController::class, 'taskReport'])->name('reports.task-report');
                Route::get('/reports/task-report/pdf', [ReportController::class, 'taskReportPdf'])->name('reports.task-report.pdf');
    });

     // Workflow report route
    
});


// Workflow routes
if (file_exists(__DIR__.'/workflow/settings.php')) {
    require __DIR__.'/workflow/settings.php';
}
require __DIR__.'/auth.php';
