<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\WorkReportController;
use App\Http\Controllers\AnnouncementController;
use App\Http\Controllers\ProjectController;
use App\Http\Controllers\LeaveController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Admin\DepartmentController as AdminDepartmentController;
use App\Http\Controllers\Admin\SettingController as AdminSettingController;

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

// Authenticated Routes
Route::middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // Attendance Routes
    Route::get('/attendance', [AttendanceController::class, 'index'])->name('attendance.index');
    Route::post('/attendance/check-in', [AttendanceController::class, 'checkIn'])->name('attendance.checkin');
    Route::post('/attendance/check-out', [AttendanceController::class, 'checkOut'])->name('attendance.checkout');
    Route::post('/attendance/break', [AttendanceController::class, 'toggleBreak'])->name('attendance.break');

    // Work Report Routes
    Route::get('/reports', [WorkReportController::class, 'index'])->name('reports.index');
    Route::get('/reports/create', [WorkReportController::class, 'create'])->name('reports.create');
    Route::get('/reports/{report}', [WorkReportController::class, 'show'])->name('reports.show');
    Route::post('/reports', [WorkReportController::class, 'store'])->name('reports.store');

    // Announcement Routes
    Route::get('/announcements', [AnnouncementController::class, 'index'])->name('announcements.index');
    Route::get('/announcements/{announcement}', [AnnouncementController::class, 'show'])->name('announcements.show');

    // Project Routes
    Route::get('/projects', [ProjectController::class, 'index'])->name('projects.index');
    Route::get('/projects/{project}', [ProjectController::class, 'show'])->name('projects.show');

    // Task Routes
    Route::get('/tasks', [\App\Http\Controllers\TaskController::class, 'index'])->name('tasks.index');
    Route::get('/project-tasks/{task}', [\App\Http\Controllers\ProjectTaskController::class, 'show'])->name('project-tasks.show');
    Route::post('/project-tasks', [\App\Http\Controllers\ProjectTaskController::class, 'store'])->name('project-tasks.store');
    Route::post('/project-tasks/{task}/status', [\App\Http\Controllers\ProjectTaskController::class, 'updateStatus'])->name('project-tasks.update_status');
    Route::put('/project-tasks/{task}', [\App\Http\Controllers\ProjectTaskController::class, 'update'])->name('project-tasks.update');
    Route::delete('/project-tasks/{task}', [\App\Http\Controllers\ProjectTaskController::class, 'destroy'])->name('project-tasks.destroy');
    Route::post('/project-tasks/{task}/comments', [\App\Http\Controllers\ProjectTaskController::class, 'addComment'])->name('project-tasks.comment');
    Route::post('/project-tasks/{task}/time-logs', [\App\Http\Controllers\ProjectTaskController::class, 'logTime'])->name('project-tasks.log-time');

    // Leave Routes
    Route::get('/leaves', [LeaveController::class, 'index'])->name('leaves.index');
    Route::get('/leaves/create', [LeaveController::class, 'create'])->name('leaves.create');
    Route::post('/leaves', [LeaveController::class, 'store'])->name('leaves.store');

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

    // Common Profile/Settings
    Route::get('/profile', [DashboardController::class, 'profile']);
    Route::get('/settings', [DashboardController::class, 'settings']);

    // Notification Routes
    Route::get('/notifications/recent', [\App\Http\Controllers\NotificationController::class, 'getRecent']);
    Route::post('/notifications/mark-read/{id}', [\App\Http\Controllers\NotificationController::class, 'markAsRead']);
    Route::post('/notifications/mark-all-read', [\App\Http\Controllers\NotificationController::class, 'markAllAsRead']);
    Route::get('/notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');

    // Role-based Admin Group
    Route::middleware(['role:admin|super_admin'])->prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
        
        Route::get('/attendance', [AdminDashboardController::class, 'attendance'])->name('attendance');
        Route::get('/attendance/create', [AdminDashboardController::class, 'createAttendance'])->name('attendance.create');
        Route::post('/attendance', [AdminDashboardController::class, 'storeAttendance'])->name('attendance.store');
        Route::get('/reports', [AdminDashboardController::class, 'reports'])->name('reports');
        Route::post('/reports/{report}/approve', [AdminDashboardController::class, 'approveReport'])->name('reports.approve');
        Route::post('/reports/{report}/reject', [AdminDashboardController::class, 'rejectReport'])->name('reports.reject');
        Route::resource('projects', \App\Http\Controllers\Admin\ProjectController::class);
        Route::resource('announcements', \App\Http\Controllers\Admin\AnnouncementController::class);
        Route::get('/leaves', [AdminDashboardController::class, 'leaves'])->name('leaves');
        Route::post('/leaves/{leave}/approve', [AdminDashboardController::class, 'approveLeave'])->name('leaves.approve');
        Route::post('/leaves/{leave}/reject', [AdminDashboardController::class, 'rejectLeave'])->name('leaves.reject');
        Route::get('/activity-logs', [AdminDashboardController::class, 'activityLogs'])->name('activity-logs');

        // Export Routes
        Route::get('/exports/attendance/pdf', [\App\Http\Controllers\Admin\ExportController::class, 'attendancePdf'])->name('exports.attendance.pdf');
        Route::get('/exports/attendance/excel', [\App\Http\Controllers\Admin\ExportController::class, 'attendanceExcel'])->name('exports.attendance.excel');
        Route::get('/exports/reports/pdf', [\App\Http\Controllers\Admin\ExportController::class, 'reportsPdf'])->name('exports.reports.pdf');
        Route::get('/exports/reports/excel', [\App\Http\Controllers\Admin\ExportController::class, 'reportsExcel'])->name('exports.reports.excel');
        Route::get('/exports/employees/excel', [\App\Http\Controllers\Admin\ExportController::class, 'employeesExcel'])->name('exports.employees.excel');
        Route::get('/exports/report/{report}/pdf', [\App\Http\Controllers\Admin\ExportController::class, 'singleReportPdf'])->name('exports.report.pdf');

        Route::resource('users', AdminUserController::class);
        Route::resource('departments', AdminDepartmentController::class);
        
        Route::get('/settings', [AdminSettingController::class, 'index'])->name('settings.index');
        Route::put('/settings', [AdminSettingController::class, 'update'])->name('settings.update');
        Route::post('/settings/test-notification', [AdminSettingController::class, 'testNotification'])->name('settings.test-notification');
    });
});
