<?php

use App\Http\Controllers\Api\ActivityLogController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\BacklogController;
use App\Http\Controllers\Api\CalendarController;
use App\Http\Controllers\Api\ChecklistController;
use App\Http\Controllers\Api\CommentController;
use App\Http\Controllers\Api\DashboardController;
use App\Http\Controllers\Api\DepartmentController;
use App\Http\Controllers\Api\FileController;
use App\Http\Controllers\Api\MeetingController;
use App\Http\Controllers\Api\NotificationController;
use App\Http\Controllers\Api\PermissionController;
use App\Http\Controllers\Api\ProjectController;
use App\Http\Controllers\Api\PwaController;
use App\Http\Controllers\Api\ReportController;
use App\Http\Controllers\Api\RoleController;
use App\Http\Controllers\Api\SearchController;
use App\Http\Controllers\Api\SettingController;
use App\Http\Controllers\Api\SprintController;
use App\Http\Controllers\Api\SubtaskController;
use App\Http\Controllers\Api\TaskController;
use App\Http\Controllers\Api\UserController;
use Illuminate\Support\Facades\Route;

Route::post('/login', [AuthController::class, 'login'])->middleware('throttle:login');
Route::post('/forgot-password', [AuthController::class, 'forgotPassword'])->middleware('throttle:login');
Route::post('/reset-password', [AuthController::class, 'resetPassword'])->middleware('throttle:sensitive');

Route::middleware(['auth:sanctum', 'resource.access'])->group(function () {
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::get('/user', [AuthController::class, 'user']);
    Route::put('/user/profile', [AuthController::class, 'updateProfile']);
    Route::post('/user/avatar', [AuthController::class, 'updateAvatar']);
    Route::put('/user/password', [AuthController::class, 'changePassword'])->middleware('throttle:sensitive');

    Route::get('/pwa/home', [PwaController::class, 'home']);
    Route::get('/pwa/profile', [PwaController::class, 'profile']);
    Route::get('/pwa/sprints', [PwaController::class, 'sprints'])->middleware('permission:sprints.view');
    Route::get('/pwa/task-options', [PwaController::class, 'taskOptions'])->middleware('permission:tasks.create');
    Route::get('/pwa/projects', [PwaController::class, 'projects'])->middleware('permission:projects.view');
    Route::get('/pwa/projects/{project}', [PwaController::class, 'projectDetail'])->middleware('permission:projects.view');
    Route::patch('/pwa/projects/{project}/status', [PwaController::class, 'updateProjectStatus'])->middleware('permission:projects.edit');
    Route::get('/pwa/notifications', [PwaController::class, 'notifications'])->middleware('permission:notifications.view');
    Route::patch('/pwa/notifications/{notification}/read', [PwaController::class, 'notificationRead'])->middleware('permission:notifications.view');
    Route::patch('/pwa/notifications/{notification}/unread', [PwaController::class, 'notificationUnread'])->middleware('permission:notifications.view');
    Route::patch('/pwa/notifications/{notification}/remind', [PwaController::class, 'notificationRemind'])->middleware('permission:notifications.view');
    Route::patch('/pwa/notifications/{notification}/dismiss', [PwaController::class, 'notificationDismiss'])->middleware('permission:notifications.view');
    Route::patch('/pwa/notifications/read-all', [PwaController::class, 'notificationsReadAll'])->middleware('permission:notifications.view');
    Route::get('/pwa/tasks', [PwaController::class, 'tasks'])->middleware('permission:tasks.view');
    Route::get('/pwa/tasks/{task}', [PwaController::class, 'taskDetail'])->middleware('permission:tasks.view');
    Route::patch('/pwa/tasks/{task}/status', [PwaController::class, 'updateTaskStatus'])->middleware('permission:tasks.edit');
    Route::post('/pwa/tasks/{task}/comments', [PwaController::class, 'addTaskComment'])->middleware('permission:comments.create');
    Route::post('/pwa/tasks/{task}/attachments', [PwaController::class, 'addTaskAttachment'])->middleware('permission:files.upload');
    Route::get('/pwa/tasks/{task}/attachments/{file}', [PwaController::class, 'downloadTaskAttachment'])->middleware('permission:files.view');

    Route::get('/dashboard/summary', [DashboardController::class, 'summary'])->middleware('permission:reports.view');
    Route::get('/dashboard/charts', [DashboardController::class, 'charts'])->middleware('permission:reports.view');
    Route::get('/dashboard/monitoring', [DashboardController::class, 'monitoring'])->middleware('permission:reports.view');
    Route::get('/calendar/events', [CalendarController::class, 'events']);

    Route::get('/users', [UserController::class, 'index'])->middleware(['permission:team.view', 'throttle:sensitive']);
    Route::post('/users', [UserController::class, 'store'])->middleware(['permission:team.create', 'throttle:sensitive']);
    Route::get('/users/{user}', [UserController::class, 'show'])->middleware(['permission:team.view', 'throttle:sensitive']);
    Route::put('/users/{user}', [UserController::class, 'update'])->middleware(['permission:team.edit', 'throttle:sensitive']);
    Route::patch('/users/{user}', [UserController::class, 'update'])->middleware(['permission:team.edit', 'throttle:sensitive']);
    Route::delete('/users/{user}', [UserController::class, 'destroy'])->middleware(['permission:team.delete', 'throttle:sensitive']);
    Route::patch('/users/{user}/status', [UserController::class, 'updateStatus'])->middleware(['permission:team.edit', 'throttle:sensitive']);
    Route::put('/users/{user}/status', [UserController::class, 'updateStatus'])->middleware(['permission:team.edit', 'throttle:sensitive']);
    Route::put('/users/{user}/roles', [UserController::class, 'syncRoles'])->middleware(['permission:roles.edit', 'throttle:sensitive']);
    Route::get('/users/{user}/workload', [UserController::class, 'updateWorkload'])->middleware('permission:team.view');
    Route::get('/users/{user}/tasks', [UserController::class, 'taskStats'])->middleware('permission:team.view');

    Route::get('/projects', [ProjectController::class, 'index'])->middleware('permission:projects.view');
    Route::post('/projects', [ProjectController::class, 'store'])->middleware('permission:projects.create');
    Route::get('/projects/{project}', [ProjectController::class, 'show'])->middleware('permission:projects.view');
    Route::put('/projects/{project}', [ProjectController::class, 'update'])->middleware('permission:projects.edit');
    Route::patch('/projects/{project}', [ProjectController::class, 'update'])->middleware('permission:projects.edit');
    Route::delete('/projects/{project}', [ProjectController::class, 'destroy'])->middleware('permission:projects.delete');
    Route::post('/projects/{project}/archive', [ProjectController::class, 'archive'])->middleware('permission:projects.edit');
    Route::post('/projects/{project}/restore', [ProjectController::class, 'restore'])->middleware('permission:projects.edit');
    Route::post('/projects/{project}/members', [ProjectController::class, 'addMember'])->middleware('permission:projects.assign');
    Route::delete('/projects/{project}/members/{user}', [ProjectController::class, 'removeMember'])->middleware('permission:projects.assign');
    Route::post('/projects/{project}/update-progress', [ProjectController::class, 'updateProgress'])->middleware('permission:projects.edit');

    Route::get('/tasks', [TaskController::class, 'index'])->middleware('permission:tasks.view');
    Route::post('/tasks', [TaskController::class, 'store'])->middleware('permission:tasks.create');
    Route::get('/tasks/{task}', [TaskController::class, 'show'])->middleware('permission:tasks.view');
    Route::put('/tasks/{task}', [TaskController::class, 'update'])->middleware('permission:tasks.edit');
    Route::patch('/tasks/{task}', [TaskController::class, 'update'])->middleware('permission:tasks.edit');
    Route::delete('/tasks/{task}', [TaskController::class, 'destroy'])->middleware('permission:tasks.delete');
    Route::put('/tasks/{task}/status', [TaskController::class, 'updateStatus'])->middleware('permission:tasks.edit');
    Route::put('/tasks/{task}/assignee', [TaskController::class, 'updateAssignee'])->middleware('permission:tasks.assign');
    Route::post('/tasks/reorder', [TaskController::class, 'reorder'])->middleware('permission:tasks.edit');
    Route::get('/my-tasks', [TaskController::class, 'myTasks']);
    Route::get('/delayed-tasks', [TaskController::class, 'delayedTasks']);

    Route::apiResource('tasks.checklists', ChecklistController::class)->shallow()->middleware('permission:tasks.edit');
    Route::put('/checklists/{checklist}/toggle', [ChecklistController::class, 'toggleComplete'])->middleware('permission:tasks.edit');

    Route::apiResource('tasks.subtasks', SubtaskController::class)->shallow()->middleware('permission:tasks.edit');
    Route::put('/subtasks/{subtask}/status', [SubtaskController::class, 'updateStatus'])->middleware('permission:tasks.edit');

    Route::get('/sprints', [SprintController::class, 'index'])->middleware('permission:sprints.view');
    Route::post('/sprints', [SprintController::class, 'store'])->middleware('permission:sprints.create');
    Route::get('/sprints/{sprint}', [SprintController::class, 'show'])->middleware('permission:sprints.view');
    Route::get('/sprints/{sprint}/workspace', [SprintController::class, 'workspace'])->middleware('permission:sprints.view');
    Route::put('/sprints/{sprint}', [SprintController::class, 'update'])->middleware('permission:sprints.edit');
    Route::patch('/sprints/{sprint}', [SprintController::class, 'update'])->middleware('permission:sprints.edit');
    Route::delete('/sprints/{sprint}', [SprintController::class, 'destroy'])->middleware('permission:sprints.delete');
    Route::get('/sprints/{sprint}/available-tasks', [SprintController::class, 'availableTasks'])->middleware('permission:sprints.view');
    Route::post('/sprints/{sprint}/tasks', [SprintController::class, 'addTask'])->middleware('permission:sprints.edit');
    Route::delete('/sprints/{sprint}/tasks/{task}', [SprintController::class, 'removeTask'])->middleware('permission:sprints.edit');
    Route::put('/sprints/{sprint}/tasks/{task}/status', [SprintController::class, 'updateTaskStatus'])->middleware('permission:sprints.edit');
    Route::post('/sprints/{sprint}/start', [SprintController::class, 'start'])->middleware('permission:sprints.edit');
    Route::post('/sprints/{sprint}/end', [SprintController::class, 'end'])->middleware('permission:sprints.edit');
    Route::post('/sprints/{sprint}/cancel', [SprintController::class, 'cancel'])->middleware('permission:sprints.edit');
    Route::get('/sprints/{sprint}/report', [SprintController::class, 'report'])->middleware('permission:sprints.view');
    Route::put('/sprints/{sprint}/retrospective', [SprintController::class, 'updateRetrospective'])->middleware('permission:sprints.edit');

    Route::get('/backlog-items', [BacklogController::class, 'index'])->middleware('permission:backlog.view');
    Route::post('/backlog-items', [BacklogController::class, 'store'])->middleware('permission:backlog.create');
    Route::get('/backlog-items/{backlogItem}', [BacklogController::class, 'show'])->middleware('permission:backlog.view');
    Route::put('/backlog-items/{backlogItem}', [BacklogController::class, 'update'])->middleware('permission:backlog.edit');
    Route::patch('/backlog-items/{backlogItem}', [BacklogController::class, 'update'])->middleware('permission:backlog.edit');
    Route::delete('/backlog-items/{backlogItem}', [BacklogController::class, 'destroy'])->middleware('permission:backlog.delete');
    Route::post('/backlog-items/{backlogItem}/convert-to-task', [BacklogController::class, 'convertToTask'])->middleware('permission:backlog.edit');

    Route::get('/meeting-types', [MeetingController::class, 'types'])->middleware('permission:meetings.view');
    Route::get('/meetings', [MeetingController::class, 'index'])->middleware('permission:meetings.view');
    Route::post('/meetings', [MeetingController::class, 'store'])->middleware('permission:meetings.create');
    Route::get('/meetings/{meeting}', [MeetingController::class, 'show'])->middleware('permission:meetings.view');
    Route::get('/meetings/{meeting}/workspace', [MeetingController::class, 'workspace'])->middleware('permission:meetings.view');
    Route::put('/meetings/{meeting}', [MeetingController::class, 'update'])->middleware('permission:meetings.edit');
    Route::patch('/meetings/{meeting}', [MeetingController::class, 'update'])->middleware('permission:meetings.edit');
    Route::delete('/meetings/{meeting}', [MeetingController::class, 'destroy'])->middleware('permission:meetings.delete');
    Route::post('/meetings/{meeting}/start', [MeetingController::class, 'start'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/complete', [MeetingController::class, 'complete'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/decisions', [MeetingController::class, 'addDecision'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/structured-action-items', [MeetingController::class, 'addStructuredActionItem'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/blockers', [MeetingController::class, 'addBlocker'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/participants', [MeetingController::class, 'addParticipant'])->middleware('permission:meetings.edit');
    Route::delete('/meetings/{meeting}/participants/{user}', [MeetingController::class, 'removeParticipant'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/action-items', [MeetingController::class, 'addActionItem'])->middleware('permission:meetings.edit');
    Route::put('/meetings/{meeting}/action-items/{actionItem}', [MeetingController::class, 'updateActionItem'])->middleware('permission:meetings.edit');
    Route::post('/meetings/{meeting}/action-items/{actionItem}/convert-to-task', [MeetingController::class, 'convertActionItemToTask'])->middleware('permission:meetings.edit');

    Route::get('/comments', [CommentController::class, 'index'])->middleware('permission:comments.view');
    Route::post('/comments', [CommentController::class, 'store'])->middleware('permission:comments.create');
    Route::put('/comments/{comment}', [CommentController::class, 'update'])->middleware('permission:comments.edit');
    Route::delete('/comments/{comment}', [CommentController::class, 'destroy'])->middleware('permission:comments.delete');

    Route::get('/files', [FileController::class, 'index'])->middleware('permission:files.view');
    Route::post('/files', [FileController::class, 'store'])->middleware('permission:files.upload');
    Route::get('/files/{file}', [FileController::class, 'show'])->middleware('permission:files.view');
    Route::delete('/files/{file}', [FileController::class, 'destroy'])->middleware('permission:files.delete');

    Route::get('/activity-logs', [ActivityLogController::class, 'index'])->middleware('permission:reports.view');
    Route::get('/activity-logs/{activityLog}', [ActivityLogController::class, 'show'])->middleware('permission:reports.view');

    Route::get('/notifications', [NotificationController::class, 'index'])->middleware('permission:notifications.view');
    Route::post('/notifications/{notification}/read', [NotificationController::class, 'markAsRead'])->middleware('permission:notifications.view');
    Route::get('/notifications/{notification}/preview', [NotificationController::class, 'preview'])->middleware('permission:notifications.view');
    Route::patch('/notifications/{notification}/archive', [NotificationController::class, 'archive'])->middleware('permission:notifications.view');
    Route::patch('/notifications/{notification}/restore', [NotificationController::class, 'restore'])->middleware('permission:notifications.view');
    Route::post('/notifications/read-all', [NotificationController::class, 'markAllAsRead'])->middleware('permission:notifications.view');
    Route::delete('/notifications/{notification}', [NotificationController::class, 'destroy'])->middleware('permission:notifications.view');

    Route::prefix('reports')->middleware('permission:reports.view')->group(function () {
        Route::get('/project-status', [ReportController::class, 'projectStatus']);
        Route::get('/delayed-tasks', [ReportController::class, 'delayedTasks']);
        Route::get('/team-performance', [ReportController::class, 'teamPerformance']);
        Route::get('/workload', [ReportController::class, 'workload']);
        Route::get('/sprint-progress', [ReportController::class, 'sprintProgress']);
        Route::get('/time-estimate', [ReportController::class, 'timeEstimate']);
        Route::get('/recent-activities', [ReportController::class, 'recentActivities']);
        Route::get('/risky-projects', [ReportController::class, 'riskyProjects']);
    });

    Route::post('/departments/{department}/members', [DepartmentController::class, 'addMember'])->middleware('permission:team.edit');
    Route::put('/departments/{department}/members/{user}', [DepartmentController::class, 'updateMember'])->middleware('permission:team.edit');
    Route::delete('/departments/{department}/members/{user}', [DepartmentController::class, 'removeMember'])->middleware('permission:team.edit');
    Route::post('/departments/{department}/members/{user}/transfer', [DepartmentController::class, 'transferMember'])->middleware('permission:team.edit');
    Route::get('/departments', [DepartmentController::class, 'index'])->middleware('permission:team.view');
    Route::post('/departments', [DepartmentController::class, 'store'])->middleware('permission:team.create');
    Route::get('/departments/{department}', [DepartmentController::class, 'show'])->middleware('permission:team.view');
    Route::put('/departments/{department}', [DepartmentController::class, 'update'])->middleware('permission:team.edit');
    Route::patch('/departments/{department}', [DepartmentController::class, 'update'])->middleware('permission:team.edit');
    Route::delete('/departments/{department}', [DepartmentController::class, 'destroy'])->middleware('permission:team.delete');

    Route::get('/roles', [RoleController::class, 'index'])->middleware(['permission:roles.view', 'throttle:sensitive']);
    Route::post('/roles', [RoleController::class, 'store'])->middleware(['permission:roles.create', 'throttle:sensitive']);
    Route::get('/roles/{role}', [RoleController::class, 'show'])->middleware(['permission:roles.view', 'throttle:sensitive']);
    Route::put('/roles/{role}', [RoleController::class, 'update'])->middleware(['permission:roles.edit', 'throttle:sensitive']);
    Route::patch('/roles/{role}', [RoleController::class, 'update'])->middleware(['permission:roles.edit', 'throttle:sensitive']);
    Route::delete('/roles/{role}', [RoleController::class, 'destroy'])->middleware(['permission:roles.delete', 'throttle:sensitive']);
    Route::post('/roles/{role}/permissions', [RoleController::class, 'assignPermissions'])->middleware(['permission:roles.edit', 'throttle:sensitive']);
    Route::get('/roles/{role}/permissions', [RoleController::class, 'getPermissions'])->middleware('permission:roles.view');
    Route::apiResource('permissions', PermissionController::class)->only(['index', 'show'])->middleware('permission:roles.view');

    Route::get('/settings', [SettingController::class, 'index'])->middleware('permission:settings.view');
    Route::post('/settings', [SettingController::class, 'store'])->middleware(['permission:settings.edit', 'throttle:sensitive']);
    Route::put('/settings/{setting}', [SettingController::class, 'update'])->middleware(['permission:settings.edit', 'throttle:sensitive']);

    Route::get('/search', [SearchController::class, 'search']);
});
