<?php

use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\TaskController;
use App\Http\Controllers\TeamController;
use App\Http\Controllers\CommentController;
use App\Http\Controllers\AttachmentController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ExportController;
use App\Http\Controllers\FormBuilderController;
use App\Http\Controllers\FormSubmissionController;

// Auth routes
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Public Form routes (no auth)
Route::get('/forms/{slug}', [FormSubmissionController::class, 'publicForm'])->name('forms.public');
Route::post('/forms/{slug}/submit', [FormSubmissionController::class, 'submit'])->name('forms.submit');

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

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

    // Profile
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');

    // Projects
    Route::resource('projects', ProjectController::class);
    Route::get('/projects/{project}/settings', [ProjectController::class, 'settings'])->name('projects.settings');
    Route::post('/projects/{project}/members', [ProjectController::class, 'addMember'])->name('projects.members.add');
    Route::delete('/projects/{project}/members/{user}', [ProjectController::class, 'removeMember'])->name('projects.members.remove');
    Route::patch('/projects/{project}/archive', [ProjectController::class, 'archive'])->name('projects.archive');

    // Tasks
    Route::post('/tasks', [TaskController::class, 'store'])->name('tasks.store');
    Route::get('/tasks/{task}', [TaskController::class, 'show'])->name('tasks.show');
    Route::put('/tasks/{task}', [TaskController::class, 'update'])->name('tasks.update');
    Route::delete('/tasks/{task}', [TaskController::class, 'destroy'])->name('tasks.destroy');
    Route::patch('/tasks/{task}/status', [TaskController::class, 'updateStatus'])->name('tasks.update-status');
    Route::post('/tasks/reorder', [TaskController::class, 'reorder'])->name('tasks.reorder');
    Route::patch('/tasks/{task}/toggle-complete', [TaskController::class, 'toggleComplete'])->name('tasks.toggle-complete');
    Route::patch('/subtasks/{task}', [TaskController::class, 'updateSubtask'])->name('subtasks.update');
    Route::delete('/subtasks/{task}', [TaskController::class, 'destroySubtask'])->name('subtasks.destroy');
    Route::post('/subtasks/reorder', [TaskController::class, 'reorderSubtasks'])->name('subtasks.reorder');
    Route::post('/tasks/{task}/tags/{tag}', [TaskController::class, 'attachTag'])->name('tasks.tags.attach');
    Route::delete('/tasks/{task}/tags/{tag}', [TaskController::class, 'detachTag'])->name('tasks.tags.detach');

    // Task Bulk Actions
    Route::post('/tasks/bulk-action', [TaskController::class, 'bulkAction'])->name('tasks.bulk-action');

    // Task Dependencies
    Route::post('/tasks/{task}/dependencies', [TaskController::class, 'addDependency'])->name('tasks.dependencies.add');
    Route::delete('/tasks/{task}/dependencies/{dependency}', [TaskController::class, 'removeDependency'])->name('tasks.dependencies.remove');

    // CSV Export
    Route::get('/projects/{project}/export/csv', [ExportController::class, 'projectCsv'])->name('projects.export.csv');
    Route::get('/reports/export/csv', [ExportController::class, 'reportCsv'])->name('reports.export.csv');

    // Teams
    Route::resource('teams', TeamController::class);
    Route::post('/teams/{team}/members', [TeamController::class, 'addMember'])->name('teams.add-member');
    Route::delete('/teams/{team}/members/{user}', [TeamController::class, 'removeMember'])->name('teams.remove-member');

    // Comments
    Route::post('/tasks/{task}/comments', [CommentController::class, 'store'])->name('comments.store');
    Route::delete('/comments/{comment}', [CommentController::class, 'destroy'])->name('comments.destroy');

    // Attachments
    Route::post('/tasks/{task}/attachments', [AttachmentController::class, 'store'])->name('attachments.store');
    Route::delete('/attachments/{attachment}', [AttachmentController::class, 'destroy'])->name('attachments.destroy');

    // Search
    Route::get('/search', [SearchController::class, 'index'])->name('search');

    // Reports
    Route::get('/reports', [ReportController::class, 'index'])->name('reports');

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

    // Admin
    Route::prefix('admin')->name('admin.')->group(function () {
        Route::get('/', [AdminController::class, 'index'])->name('index');

        // User Management
        Route::get('/users', [AdminController::class, 'users'])->name('users');
        Route::get('/users/create', [AdminController::class, 'createUser'])->name('users.create');
        Route::post('/users', [AdminController::class, 'storeUser'])->name('users.store');
        Route::get('/users/{user}/edit', [AdminController::class, 'editUser'])->name('users.edit');
        Route::put('/users/{user}', [AdminController::class, 'updateUser'])->name('users.update');
        Route::delete('/users/{user}', [AdminController::class, 'deleteUser'])->name('users.delete');
        Route::patch('/users/{user}/toggle', [AdminController::class, 'toggleUserActive'])->name('users.toggle');

        // Roles & Permissions
        Route::get('/roles', [AdminController::class, 'roles'])->name('roles');
        Route::post('/roles', [AdminController::class, 'storeRole'])->name('roles.store');
        Route::put('/roles/{role}', [AdminController::class, 'updateRolePermissions'])->name('roles.update');
        Route::delete('/roles/{role}', [AdminController::class, 'deleteRole'])->name('roles.delete');

        // Companies
        Route::get('/companies', [AdminController::class, 'companies'])->name('companies');

        // Projects
        Route::get('/projects', [AdminController::class, 'projects'])->name('projects');
        Route::patch('/projects/{project}/archive', [AdminController::class, 'archiveProject'])->name('projects.archive');
        Route::delete('/projects/{project}', [AdminController::class, 'deleteProject'])->name('projects.delete');

        // Teams
        Route::get('/teams', [AdminController::class, 'adminTeams'])->name('teams');
        Route::delete('/teams/{team}', [AdminController::class, 'deleteTeam'])->name('teams.delete');

        // Tags
        Route::get('/tags', [AdminController::class, 'tags'])->name('tags');
        Route::post('/tags', [AdminController::class, 'storeTag'])->name('tags.store');
        Route::put('/tags/{tag}', [AdminController::class, 'updateTag'])->name('tags.update');
        Route::delete('/tags/{tag}', [AdminController::class, 'deleteTag'])->name('tags.delete');

        // Activity Log
        Route::get('/activity-log', [AdminController::class, 'activityLog'])->name('activity-log');

        // Settings (Branding)
        Route::get('/settings', [AdminController::class, 'settings'])->name('settings');
        Route::put('/settings', [AdminController::class, 'updateSettings'])->name('settings.update');

        // Admin Forms
        Route::get('/forms', [AdminController::class, 'forms'])->name('forms');
    });

    // Form Builder
    Route::resource('form-builder', FormBuilderController::class);
    Route::post('/form-builder/{form}/duplicate', [FormBuilderController::class, 'duplicate'])->name('form-builder.duplicate');
    Route::put('/form-builder/{form}/fields', [FormBuilderController::class, 'updateFields'])->name('form-builder.fields.update');
    Route::get('/form-builder/{form}/preview', [FormBuilderController::class, 'preview'])->name('form-builder.preview');
    Route::get('/form-builder/{form}/submissions', [FormBuilderController::class, 'submissions'])->name('form-builder.submissions');
    Route::get('/form-builder/{form}/submissions/export', [FormBuilderController::class, 'exportSubmissions'])->name('form-builder.submissions.export');
    Route::get('/form-builder/{form}/submissions/{submission}', [FormBuilderController::class, 'showSubmission'])->name('form-builder.submission.show');
    Route::put('/form-builder/{form}/task-mapping', [FormBuilderController::class, 'updateTaskMapping'])->name('form-builder.task-mapping');
    Route::put('/form-builder/{form}/escalation-rules', [FormBuilderController::class, 'updateEscalationRules'])->name('form-builder.escalation-rules');
});
