<?php

use App\Http\Controllers\Admin\ReportController as AdminReportController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\StaffController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\TaskController;
use Illuminate\Support\Facades\Route;

Route::redirect('/', '/dashboard');

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

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

Route::middleware('auth')->group(function (): void {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::get('/attendance', [AttendanceController::class, 'index'])->name('attendance.index');
    Route::post('/attendance/start', [AttendanceController::class, 'start'])->name('attendance.start');
    Route::post('/attendance/stop', [AttendanceController::class, 'stop'])->name('attendance.stop');
    Route::post('/attendance/lunch/start', [AttendanceController::class, 'lunchStart'])->name('attendance.lunch.start');
    Route::post('/attendance/lunch/end', [AttendanceController::class, 'lunchEnd'])->name('attendance.lunch.end');
    Route::post('/attendance/daily-log', [AttendanceController::class, 'dailyLog'])->name('attendance.daily-log');

    Route::get('/tasks', [TaskController::class, 'index'])->name('tasks.index');
    Route::post('/tasks/{task}/progress', [TaskController::class, 'updateProgress'])->name('tasks.progress');
    Route::post('/tasks/{task}/submit', [TaskController::class, 'submit'])->name('tasks.submit');

    // Task Marketplace (Staff)
    Route::get('/marketplace', [TaskController::class, 'marketplace'])->name('marketplace.index');
    Route::post('/marketplace/{task}/request', [TaskController::class, 'requestTask'])->name('marketplace.request');

    Route::get('/salary-report', [ReportController::class, 'salary'])->name('reports.salary');
});

Route::middleware(['auth', 'role:admin'])->group(function (): void {
    Route::get('/admin/tasks', [TaskController::class, 'adminIndex'])->name('admin.tasks.index');
    Route::post('/admin/tasks', [TaskController::class, 'store'])->name('admin.tasks.store');
    Route::post('/admin/tasks/{task}/assign', [TaskController::class, 'assignTask'])->name('admin.tasks.assign');
    Route::post('/admin/submissions/{submission}/review', [TaskController::class, 'review'])->name('admin.submissions.review');

    // Task Requests (Marketplace Approvals)
    Route::get('/admin/task-requests', [TaskController::class, 'adminRequestsIndex'])->name('admin.task-requests.index');
    Route::post('/admin/task-requests/{taskRequest}/approve', [TaskController::class, 'approveRequest'])->name('admin.task-requests.approve');
    Route::post('/admin/task-requests/{taskRequest}/reject', [TaskController::class, 'rejectRequest'])->name('admin.task-requests.reject');
    
    // Staff Management Routes
    Route::resource('/admin/staff', StaffController::class);

    // Admin Reports
    Route::get('/admin/attendance-report', [AdminReportController::class, 'monthlyAttendance'])->name('admin.reports.monthly');
    Route::post('/admin/attendance-report/mark-present', [AdminReportController::class, 'markPresent'])->name('admin.reports.mark-present');

    // General Company Settings
    Route::get('/admin/settings', [SettingController::class, 'index'])->name('admin.settings.index');
    Route::post('/admin/settings', [SettingController::class, 'update'])->name('admin.settings.update');
});
