<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Support\Authorization\PermissionCatalog;
use App\Http\Controllers\language\LanguageController;
use App\Http\Controllers\pages\HomePage;
use App\Http\Controllers\pages\Page2;
use App\Http\Controllers\pages\MiscError;
use App\Http\Controllers\authentications\LoginBasic;

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

    return redirect()->to(PermissionCatalog::firstAccessibleRouteFor(Auth::user()));
})->name('root');

// Legacy demo pages
Route::get('/home', [HomePage::class, 'index'])->name('pages-home');
Route::get('/page-2', [Page2::class, 'index'])->name('pages-page-2');

// locale
Route::get('/lang/{locale}', [LanguageController::class, 'swap']);
Route::get('/pages/misc-error', [MiscError::class, 'index'])->name('pages-misc-error');

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginBasic::class, 'index'])->name('login');
    Route::post('/login', [LoginBasic::class, 'authenticate'])->name('login.attempt');
});

Route::post('/logout', [LoginBasic::class, 'logout'])->middleware('auth')->name('auth-logout');

// Backward-compatible redirects for old Vuexy paths
Route::redirect('/auth/login-basic', '/login');
Route::redirect('/auth/register-basic', '/login');
Route::redirect('/register', '/login');

require __DIR__ . '/admin.php';
require __DIR__ . '/owner.php';
