<?php

use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\LoginController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Server-rendered pages for the Franchise & Admin portals (session auth,
| 'web' guard). The Flutter apps talk to routes/api.php instead - see that
| file's header comment for why the two coexist on one Laravel app.
|
*/

Route::get('/', fn () => redirect()->route('dashboard'));

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'show'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    Route::get('/two-factor', [LoginController::class, 'showTwoFactor'])->name('two-factor.show');
    Route::post('/two-factor', [LoginController::class, 'verifyTwoFactor'])->name('two-factor.verify');
});

Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Franchise Portal - Owner, Staff, Pharmacist share it; franchise.scope
    // stops any of them reaching another store's data via a route-bound id.
    Route::prefix('franchise')
        ->name('franchise.')
        ->middleware(['role:Franchise Owner|Franchise Staff|Pharmacist', 'franchise.scope'])
        ->group(function () {
            Route::view('/dashboard', 'franchise.dashboard')->name('dashboard');
            // Route::resource('orders', Web\Franchise\OrderController::class);
            // Route::resource('inventory', Web\Franchise\InventoryController::class);
        });

    // Admin / Finance Portal
    Route::prefix('admin')
        ->name('admin.')
        ->middleware('role:Super Admin|Finance Admin')
        ->group(function () {
            Route::view('/dashboard', 'admin.dashboard')->name('dashboard');
            // Route::resource('franchises', Web\Admin\FranchiseController::class);
            // Route::resource('products', Web\Admin\ProductController::class);
        });
});
