<?php

use App\Http\Controllers\AppointmentController;
use App\Http\Controllers\BranchController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ServiceCategoryController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\WalkInQueueController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::middleware(['auth', 'active', 'verified'])->group(function () {
    Route::get('/dashboard', DashboardController::class)->middleware('permission:view dashboard')->name('dashboard');

    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::middleware('permission:manage branches')->group(function () {
        Route::resource('branches', BranchController::class);
    });

    Route::middleware('permission:manage customers')->group(function () {
        Route::resource('customers', CustomerController::class);
    });

    Route::middleware('permission:manage staff')->group(function () {
        Route::resource('staff', StaffController::class);
    });

    Route::middleware('permission:manage services')->group(function () {
        Route::resource('service-categories', ServiceCategoryController::class)->except(['show']);
        Route::resource('services', ServiceController::class);
    });

    Route::middleware('permission:manage appointments')->group(function () {
        Route::get('appointments/calendar', [AppointmentController::class, 'calendar'])->name('appointments.calendar');
        Route::post('appointments/{appointment}/check-in', [AppointmentController::class, 'checkIn'])->name('appointments.check-in');
        Route::post('appointments/{appointment}/complete', [AppointmentController::class, 'complete'])->name('appointments.complete');
        Route::post('appointments/{appointment}/cancel', [AppointmentController::class, 'cancel'])->name('appointments.cancel');
        Route::post('appointments/{appointment}/no-show', [AppointmentController::class, 'noShow'])->name('appointments.no-show');
        Route::resource('appointments', AppointmentController::class);
    });

    Route::middleware('permission:manage walk-ins')->group(function () {
        Route::post('walk-ins/{walkIn}/convert', [WalkInQueueController::class, 'convertToAppointment'])->name('walk-ins.convert');
        Route::resource('walk-ins', WalkInQueueController::class)->parameters(['walk-ins' => 'walkIn']);
    });
});

require __DIR__ . '/auth.php';
