<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\FloorplanController;
use App\Http\Controllers\ReservationController;
use App\Http\Controllers\OperationsController;

// OPS controllers (MUST be at top, never inside groups)
use App\Http\Controllers\Ops\OpsFloorplanController;
use App\Http\Controllers\Ops\OpsReservationController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

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

/*
|--------------------------------------------------------------------------
| Language switch
|--------------------------------------------------------------------------
*/
Route::get('/lang/{locale}', function (string $locale) {
    $supported = ['en', 'bg'];
    if (!in_array($locale, $supported, true)) {
        $locale = 'bg';
    }
    session(['locale' => $locale]);
    return redirect()->back();
})->name('lang.switch');

/*
|--------------------------------------------------------------------------
| Dashboard
|--------------------------------------------------------------------------
*/
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

/*
|--------------------------------------------------------------------------
| Profile
|--------------------------------------------------------------------------
*/
Route::middleware('auth')->group(function () {
    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');
});

/*
|--------------------------------------------------------------------------
| ADMIN – SuperAdmin only
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {

    // Users
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    Route::post('/users', [UserController::class, 'store'])->name('users.store');

    // Floorplans
    Route::get('/floorplans', [FloorplanController::class, 'index'])->name('floorplans.index');
    Route::post('/floorplans', [FloorplanController::class, 'store'])->name('floorplans.store');
    Route::get('/floorplans/{floorplan}', [FloorplanController::class, 'show'])->name('floorplans.show');

    // Tables
    Route::post('/floorplans/{floorplan}/tables', [FloorplanController::class, 'createTable'])->name('floorplans.tables.create');
    Route::put('/tables/{table}', [FloorplanController::class, 'updateTable'])->name('tables.update');
    Route::post('/floorplans/{floorplan}/tables/assign-waiter', [FloorplanController::class, 'assignWaiter'])
        ->name('floorplans.tables.assignWaiter');

    // Reservation ↔ table sync (admin)
    Route::post('/reservations/{reservation}/sync-tables', [FloorplanController::class, 'syncReservationTables'])
        ->name('reservations.tables.sync');
});

/*
|--------------------------------------------------------------------------
| RESERVATIONS (classic admin views)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->group(function () {

    Route::get('/reservations', [ReservationController::class, 'index'])->name('reservations.index');
    Route::post('/reservations', [ReservationController::class, 'store'])->name('reservations.store');
    Route::get('/reservations/{reservation}/edit', [ReservationController::class, 'edit'])->name('reservations.edit');
    Route::put('/reservations/{reservation}', [ReservationController::class, 'update'])->name('reservations.update');

    Route::post('/reservations/{reservation}/tables', [ReservationController::class, 'updateTables'])
        ->name('reservations.tables.update');

    Route::post('/reservations/{reservation}/toggle-seated', [ReservationController::class, 'toggleSeated'])
        ->name('reservations.toggleSeated');

    Route::post('/reservations/{reservation}/lock-move', [ReservationController::class, 'toggleLockMove'])
        ->name('reservations.lockmove');
});

/*
|--------------------------------------------------------------------------
| OPS – Floorplan-based operations (MAIN WORKFLOW)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])
    ->prefix('ops')
    ->name('ops.')
    ->group(function () {

        // Main ops floorplan
        Route::get('/', [OpsFloorplanController::class, 'show'])
            ->middleware('role:SuperAdmin|Manager|Upravitel|Hostess|Waiter')
            ->name('floorplan');

        // Ops reservations (AJAX)
        Route::post('/reservations', [OpsReservationController::class, 'store'])
            ->name('reservations.store');

        Route::post('/reservations/{reservation}/toggle-seated', [OpsReservationController::class, 'toggleSeated'])
            ->name('reservations.toggleSeated');

        Route::post('/reservations/{reservation}/toggle-lock', [OpsReservationController::class, 'toggleLock'])
            ->name('reservations.toggleLock');
            Route::post('/logout', function () {
    Auth::logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/login');
})->name('logout');
    });
    require __DIR__.'/auth.php';

