<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\OperationUserController;
use App\Http\Controllers\RequestController;
use App\Http\Controllers\BeachReservationController;
use App\Http\Controllers\PropertyController;
use App\Http\Controllers\EquipmentController;
use App\Http\Controllers\EquipmentReservationController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::middleware('guest')->group(function () {
    Route::get('login', [AuthenticatedSessionController::class, 'create'])
        ->name('login');
    Route::post('login', [AuthenticatedSessionController::class, 'store']);
});

Route::middleware('auth')->group(function () {
    Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('logout');

    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');

    // Equipment time slots - accessible to all authenticated users
    Route::get('equipment/{equipment}/time-slots', [EquipmentController::class, 'getTimeSlots'])->name('equipment.time-slots');

    // Beach Reservations - Customer access
    Route::get('my-reservations', [BeachReservationController::class, 'myReservations'])
        ->name('beach-reservations.my-reservations');
    Route::resource('beach-reservations', BeachReservationController::class)
        ->except(['index', 'show', 'edit', 'update', 'destroy']);

    // Equipment Reservations - Owner access
    Route::get('my-equipment-reservations', [EquipmentReservationController::class, 'myReservations'])
        ->name('equipment-reservations.my-reservations');
    Route::resource('equipment-reservations', EquipmentReservationController::class)
        ->except(['index', 'show', 'edit', 'update', 'destroy']);
    
    // Allow users to cancel and complete their own reservations
    Route::post('equipment-reservations/{equipmentReservation}/cancel', [EquipmentReservationController::class, 'cancel'])->name('equipment-reservations.cancel');
    Route::post('equipment-reservations/{equipmentReservation}/complete', [EquipmentReservationController::class, 'complete'])->name('equipment-reservations.complete');
});

Route::middleware(['auth', 'admin'])->group(function () {
    // Operation Users
    Route::get('operation-users', [OperationUserController::class, 'index'])->name('operation-users.index');
    Route::get('operation-users/create', [OperationUserController::class, 'create'])->name('operation-users.create');
    Route::post('operation-users', [OperationUserController::class, 'store'])->name('operation-users.store');
    Route::get('operation-users/{user}/edit', [OperationUserController::class, 'edit'])->name('operation-users.edit');
    Route::put('operation-users/{user}', [OperationUserController::class, 'update'])->name('operation-users.update');
    Route::post('operation-users/{user}/reset-password', [OperationUserController::class, 'resetPassword'])->name('operation-users.reset-password');
    Route::post('operation-users/{user}/toggle-status', [OperationUserController::class, 'toggleStatus'])->name('operation-users.toggle-status');
    Route::delete('operation-users/{user}', [OperationUserController::class, 'destroy'])->name('operation-users.destroy');

    // Customers
    Route::get('customers', [CustomerController::class, 'index'])->name('customers.index');
    Route::get('customers/create', [CustomerController::class, 'create'])->name('customers.create');
    Route::post('customers', [CustomerController::class, 'store'])->name('customers.store');
    Route::get('customers/{user}/edit', [CustomerController::class, 'edit'])->name('customers.edit');
    Route::put('customers/{user}', [CustomerController::class, 'update'])->name('customers.update');
    Route::post('customers/{user}/reset-password', [CustomerController::class, 'resetPassword'])->name('customers.reset-password');
    Route::post('customers/{user}/toggle-status', [CustomerController::class, 'toggleStatus'])->name('customers.toggle-status');
    Route::delete('customers/{user}', [CustomerController::class, 'destroy'])->name('customers.destroy');

    // Beach Reservations - Admin access
    Route::get('beach-reservations', [BeachReservationController::class, 'index'])->name('beach-reservations.index');
    Route::get('beach-reservations/{beachReservation}', [BeachReservationController::class, 'show'])->name('beach-reservations.show');
    Route::put('beach-reservations/{beachReservation}', [BeachReservationController::class, 'update'])->name('beach-reservations.update');
    Route::delete('beach-reservations/{beachReservation}', [BeachReservationController::class, 'destroy'])->name('beach-reservations.destroy');
    Route::post('beach-reservations/{beachReservation}/approve', [BeachReservationController::class, 'approve'])->name('beach-reservations.approve');
    Route::post('beach-reservations/{beachReservation}/reject', [BeachReservationController::class, 'reject'])->name('beach-reservations.reject');

    // Properties - Admin access
    Route::resource('properties', PropertyController::class);
    Route::post('properties/{property}/toggle-active', [PropertyController::class, 'toggleActive'])->name('properties.toggle-active');
    Route::post('properties/{property}/toggle-mobile-display', [PropertyController::class, 'toggleMobileDisplay'])->name('properties.toggle-mobile-display');
    Route::post('properties/{property}/toggle-homepage-display', [PropertyController::class, 'toggleHomepageDisplay'])->name('properties.toggle-homepage-display');
    Route::post('properties/update-sort-order', [PropertyController::class, 'updateSortOrder'])->name('properties.update-sort-order');

    // Equipment - Admin access
    Route::resource('equipment', EquipmentController::class);
    Route::post('equipment/{equipment}/toggle-active', [EquipmentController::class, 'toggleActive'])->name('equipment.toggle-active');
    Route::get('equipment-statistics', [EquipmentController::class, 'statistics'])->name('equipment.statistics');

    // Equipment Reservations - Admin access
    Route::get('equipment-reservations', [EquipmentReservationController::class, 'index'])->name('equipment-reservations.index');
    Route::get('equipment-reservations/create', [EquipmentReservationController::class, 'create'])->name('equipment-reservations.create');
    Route::post('equipment-reservations', [EquipmentReservationController::class, 'store'])->name('equipment-reservations.store');
    Route::get('equipment-reservations/{equipmentReservation}', [EquipmentReservationController::class, 'show'])->name('equipment-reservations.show');
    Route::get('equipment-reservations/{equipmentReservation}/edit', [EquipmentReservationController::class, 'edit'])->name('equipment-reservations.edit');
    Route::put('equipment-reservations/{equipmentReservation}', [EquipmentReservationController::class, 'update'])->name('equipment-reservations.update');
    Route::delete('equipment-reservations/{equipmentReservation}', [EquipmentReservationController::class, 'destroy'])->name('equipment-reservations.destroy');
    Route::get('equipment-reservations-calendar', [EquipmentReservationController::class, 'calendar'])->name('equipment-reservations.calendar');
    Route::get('equipment-reservations-statistics', [EquipmentReservationController::class, 'statistics'])->name('equipment-reservations.statistics');

    Route::resource('operation-users', OperationUserController::class);
    Route::resource('customers', CustomerController::class);
    Route::resource('requests', RequestController::class);
});

// Request routes (accessible by authenticated users)
Route::middleware('auth')->group(function () {
    Route::get('requests/create', [RequestController::class, 'create'])->name('requests.create');
    Route::post('requests', [RequestController::class, 'store'])->name('requests.store');
});
