<?php

use App\Http\Controllers\Admin\AttendanceController as AdminAttendanceController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\EmployeeController as AdminEmployeeController;
use App\Http\Controllers\Admin\LeaveRequestController as AdminLeaveRequestController;
use App\Http\Controllers\Admin\LogController as AdminLogController;
use App\Http\Controllers\Admin\OfficeLocationController as AdminOfficeLocationController;
use App\Http\Controllers\Admin\ReportController as AdminReportController;
use App\Http\Controllers\Admin\SettingController as AdminSettingController;
use App\Http\Controllers\Admin\ShiftController as AdminShiftController;
use App\Http\Controllers\Admin\WhatsAppController as AdminWhatsAppController;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\Employee\AttendanceController as EmployeeAttendanceController;
use App\Http\Controllers\Employee\DashboardController as EmployeeDashboardController;
use App\Http\Controllers\Employee\LeaveRequestController as EmployeeLeaveRequestController;
use App\Http\Controllers\Employee\ProfileController as EmployeeProfileController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

// WhatsApp Fonnte Inbound Webhook (Automated Location & Message Attendance)
Route::post('/api/whatsapp/webhook', [\App\Http\Controllers\Api\WhatsAppWebhookController::class, 'handle'])->name('whatsapp.webhook');
Route::match(['get', 'post'], '/whatsapp/webhook', [\App\Http\Controllers\Api\WhatsAppWebhookController::class, 'handle']);

// Direct Universal QR Scan Route (Accessed when scanning with phone camera or browser)
Route::get('/attend/qr/{officeLocation}', [\App\Http\Controllers\AttendanceQrDirectController::class, 'handleDirectScan'])->name('attend.qr.direct');

// Root redirect
Route::get('/', function () {
    if (Auth::check()) {
        $user = Auth::user();
        if ($user->isAdmin()) {
            return redirect()->route('admin.dashboard');
        }
        return redirect()->route('employee.dashboard');
    }
    return redirect()->route('login');
});

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

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

// ==========================================
// ADMIN ROUTES (Super Admin & HR Admin)
// ==========================================
Route::prefix('admin')->name('admin.')->middleware(['auth', 'role:super_admin,admin'])->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');

    // Employee Management
    Route::get('/employees/export', [AdminEmployeeController::class, 'export'])->name('employees.export');
    Route::resource('employees', AdminEmployeeController::class);

    // Attendance Management
    Route::get('/attendance/export', [AdminAttendanceController::class, 'export'])->name('attendance.export');
    Route::resource('attendance', AdminAttendanceController::class)->only(['index', 'store', 'update', 'destroy']);

    // Shifts & Schedules
    Route::post('/shifts/assign', [AdminShiftController::class, 'assignSchedule'])->name('shifts.assign');
    Route::resource('shifts', AdminShiftController::class)->only(['index', 'store', 'update', 'destroy']);

    // Office Locations & QR Code Live Stream
    Route::get('/qr-attendance/live-stream', [\App\Http\Controllers\Admin\QrAttendanceController::class, 'liveStream'])->name('qr.live-stream');
    Route::get('/qr-attendance', [\App\Http\Controllers\Admin\QrAttendanceController::class, 'index'])->name('qr.index');
    Route::resource('office-locations', AdminOfficeLocationController::class)->only(['index', 'store', 'update', 'destroy']);

    // Leave Requests
    Route::get('/leave-requests', [AdminLeaveRequestController::class, 'index'])->name('leave-requests.index');
    Route::post('/leave-requests/{leaveRequest}/approve', [AdminLeaveRequestController::class, 'approve'])->name('leave-requests.approve');
    Route::post('/leave-requests/{leaveRequest}/reject', [AdminLeaveRequestController::class, 'reject'])->name('leave-requests.reject');

    // Reports & Monthly Recap
    Route::get('/reports/monthly/export', [AdminReportController::class, 'exportMonthly'])->name('reports.monthly.export');
    Route::get('/reports/monthly', [AdminReportController::class, 'monthly'])->name('reports.monthly');
    Route::get('/reports', [AdminReportController::class, 'index'])->name('reports.index');

    // ==========================================
    // SUPER ADMIN ONLY
    // ==========================================
    Route::middleware('role:super_admin')->group(function () {
        // Attendance Settings
        Route::get('/settings/attendance', [AdminSettingController::class, 'attendance'])->name('settings.attendance');
        Route::put('/settings/attendance', [AdminSettingController::class, 'updateAttendance'])->name('settings.attendance.update');

        // WhatsApp Gateway
        Route::get('/whatsapp', [AdminWhatsAppController::class, 'index'])->name('whatsapp.index');
        Route::put('/whatsapp', [AdminWhatsAppController::class, 'update'])->name('whatsapp.update');
        Route::post('/whatsapp/test-send', [AdminWhatsAppController::class, 'testSend'])->name('whatsapp.test-send');

        // Notification & Audit Logs
        Route::get('/notification-logs', [AdminLogController::class, 'notifications'])->name('notification-logs.index');
        Route::get('/audit-logs', [AdminLogController::class, 'audit'])->name('audit-logs.index');
    });
});

// ==========================================
// EMPLOYEE ROUTES (Karyawan)
// ==========================================
Route::prefix('employee')->name('employee.')->middleware(['auth', 'role:employee'])->group(function () {
    Route::get('/dashboard', [EmployeeDashboardController::class, 'index'])->name('dashboard');

    // Presensi Scan QR Code
    Route::get('/scan-qr', [EmployeeAttendanceController::class, 'scanQrPage'])->name('attendance.scan-qr');
    Route::post('/attendance/scan-qr', [EmployeeAttendanceController::class, 'processQrScan'])->name('attendance.scan-qr.post');

    // Presensi Check-In & Check-Out Selfie Camera
    Route::get('/attendance/check-in', [EmployeeAttendanceController::class, 'checkInPage'])->name('attendance.check-in');
    Route::post('/attendance/check-in', [EmployeeAttendanceController::class, 'processCheckIn'])->name('attendance.check-in.post');
    Route::get('/attendance/check-out', [EmployeeAttendanceController::class, 'checkOutPage'])->name('attendance.check-out');
    Route::post('/attendance/check-out', [EmployeeAttendanceController::class, 'processCheckOut'])->name('attendance.check-out.post');
    Route::get('/attendance/history', [EmployeeAttendanceController::class, 'history'])->name('attendance.history');

    // Leave & Permission Requests
    Route::resource('leave-requests', EmployeeLeaveRequestController::class)->only(['index', 'store', 'destroy']);

    // Profile & Password
    Route::get('/profile', [EmployeeProfileController::class, 'index'])->name('profile.index');
    Route::put('/profile', [EmployeeProfileController::class, 'update'])->name('profile.update');
});
