<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\RoomController;
use App\Http\Controllers\BookingController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\StaffTypeController;
use App\Http\Controllers\ShiftController;
use App\Http\Controllers\IdCardTypeController;
use App\Http\Controllers\ComplaintController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RoomTypeController;
use App\Http\Controllers\ReceiptController;
use App\Http\Controllers\CustomerController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// Authentication Routes
Route::get('/', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::get('/logout', [AuthController::class, 'logout'])->name('logout');

// Protected Routes
Route::middleware(['auth'])->group(function () {
    // Dashboard - Requires dashboard permission
    Route::middleware(['permission:view-dashboard'])->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        // Site Settings - Same permission as dashboard
        Route::get('/site-settings', [App\Http\Controllers\SiteSettingController::class, 'index'])->name('site-settings.index');
        Route::put('/site-settings', [App\Http\Controllers\SiteSettingController::class, 'update'])->name('site-settings.update');
    });

    // Profile Management - Everyone can access their own profile
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');

    // Room Management - Requires room permissions
    Route::middleware(['permission:view-rooms'])->group(function () {
        Route::get('/rooms', [RoomController::class, 'index'])->name('rooms.index');
        Route::get('/rooms/create', [RoomController::class, 'create'])->name('rooms.create');
        Route::get('/rooms/{room}', [RoomController::class, 'show'])->name('rooms.show');
        Route::get('/rooms/{room}/edit', [RoomController::class, 'edit'])->name('rooms.edit');
    });

    Route::middleware(['permission:create-rooms'])->group(function () {
        Route::post('/rooms', [RoomController::class, 'store'])->name('rooms.store');
    });

    Route::middleware(['permission:edit-rooms'])->group(function () {
        Route::put('/rooms/{room}', [RoomController::class, 'update'])->name('rooms.update');
        Route::patch('/rooms/{room}/status', [RoomController::class, 'updateStatus'])->name('rooms.update-status');
    });

    Route::middleware(['permission:delete-rooms'])->group(function () {
        Route::delete('/rooms/{room}', [RoomController::class, 'destroy'])->name('rooms.destroy');
    });

    // Room Type Management - Requires room permissions
    Route::middleware(['permission:view-rooms'])->group(function () {
        Route::get('/room-types', [RoomTypeController::class, 'index'])->name('room-types.index');
        Route::get('/room-types/{roomType}', [RoomTypeController::class, 'show'])->name('room-types.show');
    });

    Route::middleware(['permission:create-rooms'])->group(function () {
        Route::get('/room-types-create', [RoomTypeController::class, 'create'])->name('room-types.create');
        Route::post('/room-types', [RoomTypeController::class, 'store'])->name('room-types.store');
    });

    Route::middleware(['permission:edit-rooms'])->group(function () {
        Route::get('/room-types/{roomType}/edit', [RoomTypeController::class, 'edit'])->name('room-types.edit');
        Route::put('/room-types/{roomType}', [RoomTypeController::class, 'update'])->name('room-types.update');
    });

    Route::middleware(['permission:delete-rooms'])->group(function () {
        Route::delete('/room-types/{roomType}', [RoomTypeController::class, 'destroy'])->name('room-types.destroy');
    });

    // Front Desk - Requires front desk permission
    Route::middleware(['permission:view-front-desk'])->group(function () {
        Route::get('/bookings/room-board', [BookingController::class, 'roomBoard'])->name('bookings.room-board');
    });

    // Booking Management - Requires booking permissions
    Route::middleware(['permission:view-bookings'])->group(function () {
        Route::get('/bookings', [BookingController::class, 'index'])->name('bookings.index');
        Route::get('/bookings/{booking}', [BookingController::class, 'show'])->name('bookings.show');
        Route::get('/bookings/{booking}/edit', [BookingController::class, 'edit'])->name('bookings.edit');
        Route::get('/api/room-price', [BookingController::class, 'getRoomPrice'])->name('bookings.get-room-price');
    });

    Route::middleware(['permission:create-bookings'])->group(function () {
        Route::get('/bookings-create', [BookingController::class, 'create'])->name('bookings.create');
        Route::post('/bookings', [BookingController::class, 'store'])->name('bookings.store');
        Route::get('/api/latest-booking-customer', [BookingController::class, 'getLatestBookingCustomer'])->name('bookings.latest-customer');
    });

    Route::middleware(['permission:edit-bookings'])->group(function () {
        Route::put('/bookings/{booking}', [BookingController::class, 'update'])->name('bookings.update');
        Route::patch('/bookings/{booking}/check-in', [BookingController::class, 'checkIn'])->name('bookings.check-in');
        Route::patch('/bookings/{booking}/check-out', [BookingController::class, 'checkOut'])->name('bookings.check-out');
    });

    Route::middleware(['permission:delete-bookings'])->group(function () {
        Route::delete('/bookings/{booking}', [BookingController::class, 'destroy'])->name('bookings.destroy');
    });

    // Receipt Management - Requires booking permissions
    Route::middleware(['permission:view-bookings'])->group(function () {
        Route::get('/receipts', [ReceiptController::class, 'index'])->name('receipts.index');
        Route::get('/receipts/{receipt}', [ReceiptController::class, 'show'])->name('receipts.show');
        Route::get('/receipts/{receipt}/download', [ReceiptController::class, 'download'])->name('receipts.download');
        Route::get('/receipts/{receipt}/print', [ReceiptController::class, 'print'])->name('receipts.print');
        Route::get('/receipts/{receipt}/thermal', [ReceiptController::class, 'thermalPrint'])->name('receipts.thermal');
        Route::get('/bookings/{booking}/receipt', [ReceiptController::class, 'showByBooking'])->name('receipts.show-by-booking');
        Route::get('/bookings/{booking}/receipt/download', [ReceiptController::class, 'downloadByBooking'])->name('receipts.download-by-booking');
        Route::get('/bookings/{booking}/receipt/print', [ReceiptController::class, 'printByBooking'])->name('receipts.print-by-booking');
        Route::get('/bookings/{booking}/receipt/thermal', [ReceiptController::class, 'thermalPrintByBooking'])->name('receipts.thermal-by-booking');
    });

    // Customer Management - Requires booking permissions
    Route::middleware(['permission:view-bookings'])->group(function () {
        Route::get('/customers', [CustomerController::class, 'index'])->name('customers.index');
        Route::get('/customers/{customer}/report', [CustomerController::class, 'viewReport'])->name('customers.report');
        Route::post('/customers/{customer}/advance-payment', [CustomerController::class, 'addAdvancePayment'])
            ->name('customers.advancePayment');
    });

    // Staff Management - Requires staff permissions
    Route::middleware(['permission:view-staff'])->group(function () {
        Route::get('/staff', [StaffController::class, 'index'])->name('staff.index');
        Route::get('/staff/{staff}', [StaffController::class, 'show'])->name('staff.show');
    });

    Route::middleware(['permission:create-staff'])->group(function () {
        Route::get('/staff-create', [StaffController::class, 'create'])->name('staff.create');
        Route::post('/staff', [StaffController::class, 'store'])->name('staff.store');
    });

    Route::middleware(['permission:edit-staff'])->group(function () {
        Route::get('/staff/{staff}/edit', [StaffController::class, 'edit'])->name('staff.edit');
        Route::put('/staff/{staff}', [StaffController::class, 'update'])->name('staff.update');
    });

    Route::middleware(['permission:delete-staff'])->group(function () {
        Route::delete('/staff/{staff}', [StaffController::class, 'destroy'])->name('staff.destroy');
    });

    // Staff Type Management - Requires staff permissions
    Route::middleware(['permission:view-staff'])->group(function () {
        Route::get('/staff-types', [StaffTypeController::class, 'index'])->name('staff-types.index');
    });

    Route::middleware(['permission:create-staff'])->group(function () {
        Route::get('/staff-types/create', [StaffTypeController::class, 'create'])->name('staff-types.create');
        Route::post('/staff-types', [StaffTypeController::class, 'store'])->name('staff-types.store');
    });

    Route::middleware(['permission:edit-staff'])->group(function () {
        Route::get('/staff-types/{staffType}/edit', [StaffTypeController::class, 'edit'])->name('staff-types.edit');
        Route::put('/staff-types/{staffType}', [StaffTypeController::class, 'update'])->name('staff-types.update');
    });

    Route::middleware(['permission:delete-staff'])->group(function () {
        Route::delete('/staff-types/{staffType}', [StaffTypeController::class, 'destroy'])->name('staff-types.destroy');
    });

    // Shift Management - Requires staff permissions
    Route::middleware(['permission:view-staff'])->group(function () {
        Route::get('/shifts', [ShiftController::class, 'index'])->name('shifts.index');
    });

    Route::middleware(['permission:create-staff'])->group(function () {
        Route::get('/shifts/create', [ShiftController::class, 'create'])->name('shifts.create');
        Route::post('/shifts', [ShiftController::class, 'store'])->name('shifts.store');
    });

    Route::middleware(['permission:edit-staff'])->group(function () {
        Route::get('/shifts/{shift}/edit', [ShiftController::class, 'edit'])->name('shifts.edit');
        Route::put('/shifts/{shift}', [ShiftController::class, 'update'])->name('shifts.update');
    });

    Route::middleware(['permission:delete-staff'])->group(function () {
        Route::delete('/shifts/{shift}', [ShiftController::class, 'destroy'])->name('shifts.destroy');
    });

    // ID Card Type Management - Requires staff permissions
    Route::middleware(['permission:view-staff'])->group(function () {
        Route::get('/id-card-types', [IdCardTypeController::class, 'index'])->name('id-card-types.index');
    });

    Route::middleware(['permission:create-staff'])->group(function () {
        Route::get('/id-card-types/create', [IdCardTypeController::class, 'create'])->name('id-card-types.create');
        Route::post('/id-card-types', [IdCardTypeController::class, 'store'])->name('id-card-types.store');
    });

    Route::middleware(['permission:edit-staff'])->group(function () {
        Route::get('/id-card-types/{idCardType}/edit', [IdCardTypeController::class, 'edit'])->name('id-card-types.edit');
        Route::put('/id-card-types/{idCardType}', [IdCardTypeController::class, 'update'])->name('id-card-types.update');
    });

    Route::middleware(['permission:delete-staff'])->group(function () {
        Route::delete('/id-card-types/{idCardType}', [IdCardTypeController::class, 'destroy'])->name('id-card-types.destroy');
    });

    // Complaint Management - Requires complaint permissions
    Route::middleware(['permission:view-complaints'])->group(function () {
        Route::get('/complaints', [ComplaintController::class, 'index'])->name('complaints.index');
        Route::get('/complaints/{complaint}', [ComplaintController::class, 'show'])->name('complaints.show');
    });

    Route::middleware(['permission:create-complaints'])->group(function () {
        Route::get('/complaints-create', [ComplaintController::class, 'create'])->name('complaints.create');
        Route::post('/complaints', [ComplaintController::class, 'store'])->name('complaints.store');
    });

    Route::middleware(['permission:edit-complaints'])->group(function () {
        Route::get('/complaints/{complaint}/edit', [ComplaintController::class, 'edit'])->name('complaints.edit');
        Route::put('/complaints/{complaint}', [ComplaintController::class, 'update'])->name('complaints.update');
    });

    Route::middleware(['permission:delete-complaints'])->group(function () {
        Route::delete('/complaints/{complaint}', [ComplaintController::class, 'destroy'])->name('complaints.destroy');
    });

    // Role Management - Requires role permissions
    Route::middleware(['permission:view-roles'])->group(function () {
        Route::get('/roles', [RoleController::class, 'index'])->name('roles.index');
        Route::get('/roles/{role}', [RoleController::class, 'show'])->name('roles.show');
    });

    Route::middleware(['permission:create-roles'])->group(function () {
        Route::get('/roles-create', [RoleController::class, 'create'])->name('roles.create');
        Route::post('/roles', [RoleController::class, 'store'])->name('roles.store');
    });

    Route::middleware(['permission:edit-roles'])->group(function () {
        Route::get('/roles/{role}/edit', [RoleController::class, 'edit'])->name('roles.edit');
        Route::put('/roles/{role}', [RoleController::class, 'update'])->name('roles.update');
    });

    Route::middleware(['permission:delete-roles'])->group(function () {
        Route::delete('/roles/{role}', [RoleController::class, 'destroy'])->name('roles.destroy');
    });

    // Permission Management - Requires permission management permissions
    Route::middleware(['permission:view-permissions'])->group(function () {
        Route::get('/permissions', [PermissionController::class, 'index'])->name('permissions.index');
        Route::get('/permissions/{permission}', [PermissionController::class, 'show'])->name('permissions.show');
    });

    Route::middleware(['permission:create-permissions'])->group(function () {
        Route::get('/permissions/create', [PermissionController::class, 'create'])->name('permissions.create');
        Route::post('/permissions', [PermissionController::class, 'store'])->name('permissions.store');
    });

    Route::middleware(['permission:edit-permissions'])->group(function () {
        Route::get('/permissions/{permission}/edit', [PermissionController::class, 'edit'])->name('permissions.edit');
        Route::put('/permissions/{permission}', [PermissionController::class, 'update'])->name('permissions.update');
    });

    Route::middleware(['permission:delete-permissions'])->group(function () {
        Route::delete('/permissions/{permission}', [PermissionController::class, 'destroy'])->name('permissions.destroy');
    });

    // Statistics - Requires statistics permission
    Route::middleware(['permission:view-statistics'])->group(function () {
        Route::get('/statistics', [DashboardController::class, 'statistics'])->name('statistics');
    });

    // Reports - Requires reports permission
    Route::middleware(['permission:view-reports'])->group(function () {
        Route::get('/reports', [App\Http\Controllers\ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports-booking', [App\Http\Controllers\ReportController::class, 'bookingReport'])->name('reports.booking');
        Route::get('/reports-financial', [App\Http\Controllers\ReportController::class, 'financialReport'])->name('reports.financial');
        Route::get('/reports-cash-in-hand', [App\Http\Controllers\ReportController::class, 'cashInHandReport'])->name('reports.cashInHand');
        Route::get('/reports-cash-in-bank', [App\Http\Controllers\ReportController::class, 'cashInBankReport'])->name('reports.cashInBank');
        Route::get('/reports-cash-in-mpesa', [App\Http\Controllers\ReportController::class, 'cashInMPESAReport'])->name('reports.cashInMPESA');
        Route::get('/reports-room', [App\Http\Controllers\ReportController::class, 'roomReport'])->name('reports.room');
        Route::get('/reports-cashier', [App\Http\Controllers\ReportController::class, 'cashierReport'])->name('reports.cashier');
        Route::get('/reports-dashboard', [App\Http\Controllers\ReportController::class, 'dashboardReport'])->name('reports.dashboard');
    });
});


