<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\BK_PromotionController;
use App\Http\Controllers\BK_AziendaController;
use App\Http\Controllers\PromotionController;
use App\Http\Controllers\BK_DashboardController;
use App\Http\Controllers\FT_HomeController;
use App\Http\Controllers\BK_UserController;
use App\Http\Controllers\BK_CategoryController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

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

// Homepage -> Reindirizza alla dashboard se loggato, altrimenti login
Route::get('/', function () {
    return redirect()->route('user.dashboard');
})->name('home');

Route::get(
    '/logout',
    [AuthenticatedSessionController::class, 'destroy']
)->name('logout');

// Route protette: profilo
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');
});

// Reindirizzamento in base al ruolo
Route::get('/redirect', function () {
    $user = Auth::user();

    if ($user->role === 'admin') {
        return redirect()->route('admin.dashboard');
    } else {
        return redirect()->route('user.dashboard');
    }
})->middleware('auth')->name('redirect.by.role');

// ADMIN
Route::middleware(['auth', 'role:admin'])->prefix('backend')->group(function () {
    // Dashboard

    Route::get('/dashboard', [App\Http\Controllers\BK_DashboardController::class, 'index'])->name('admin.dashboard');

    // Gestione utenti (coerente con le view in backend/users/)
    Route::get('/users', [App\Http\Controllers\BK_UserController::class, 'index'])->name('users.index');
    Route::get('/users/create', [App\Http\Controllers\BK_UserController::class, 'create'])->name('users.create');
    Route::post('/users', [App\Http\Controllers\BK_UserController::class, 'store'])->name('users.store');
    Route::get('/users/{user}/edit', [App\Http\Controllers\BK_UserController::class, 'edit'])->name('users.edit');
    Route::put('/users', [App\Http\Controllers\BK_UserController::class, 'update'])->name('users.update');
    Route::delete('/users/{user}', [App\Http\Controllers\BK_UserController::class, 'destroy'])->name('users.destroy');

    // Rotte Promozioni (esplicite, come quelle di users)
    Route::get('/promotions', [App\Http\Controllers\BK_PromotionController::class, 'index'])->name('promotions.index');
    Route::get('/promotions/create', [App\Http\Controllers\BK_PromotionController::class, 'create'])->name('promotions.create');
    Route::post('/promotions', [BK_PromotionController::class, 'store'])->name('promotions.store');
    Route::get('/promotions/{promotion}', [BK_PromotionController::class, 'show'])->name('promotions.show');
    Route::get('/promotions/{promotion}/edit', [BK_PromotionController::class, 'edit'])->name('promotions.edit');
    Route::put('/promotions/{promotion}', [BK_PromotionController::class, 'update'])->name('promotions.update');
    Route::delete('/promotions/{promotion}', [BK_PromotionController::class, 'destroy'])->name('promotions.destroy');
    Route::delete('/promotions/promoactive', [BK_PromotionController::class, 'destroy'])->name('promotions.promoactive');


    // Rotte Categorie (esplicite, come quelle di users)
    Route::get('/categories', [App\Http\Controllers\BK_CategoryController::class, 'index'])->name('categories.index');
    Route::get('/categories/create', [App\Http\Controllers\BK_CategoryController::class, 'create'])->name('categories.create');
    Route::post('/categories', [App\Http\Controllers\BK_CategoryController::class, 'store'])->name('categories.store');
    Route::get('/categories/{category}', [App\Http\Controllers\BK_CategoryController::class, 'show'])->name('categories.show');
    Route::get('/categories/{category}/edit', [App\Http\Controllers\BK_CategoryController::class, 'edit'])->name('categories.edit');
    Route::put('/categories/{category}', [App\Http\Controllers\BK_CategoryController::class, 'update'])->name('categories.update');
    Route::delete('/categories/{category}', [App\Http\Controllers\BK_CategoryController::class, 'destroy'])->name('categories.destroy');

    // Rotta upload immagini per CKEditor
    Route::post('promotions/upload-image', [BK_PromotionController::class, 'uploadImage'])
        ->name('promotions.upload');

    Route::get('/aziende', [BK_AziendaController::class, 'index'])->name('aziende.index');
    Route::get('/aziende/create', [BK_AziendaController::class, 'create'])->name('aziende.create');
    Route::post('/aziende', [BK_AziendaController::class, 'store'])->name('aziende.store');
    Route::get('/aziende/{azienda}', [BK_AziendaController::class, 'show'])->name('aziende.show');
    Route::get('/aziende/{azienda}/edit', [BK_AziendaController::class, 'edit'])->name('aziende.edit');
    Route::put('/aziende/{azienda}', [BK_AziendaController::class, 'update'])->name('aziende.update');
    Route::delete('/aziende/{azienda}', [BK_AziendaController::class, 'destroy'])->name('aziende.destroy');
});

Route::middleware(['auth', 'role:user'])->prefix('frontend')->group(function () {
    Route::get('/dashboard', [FT_HomeController::class, 'index'])->name('user.dashboard');
    Route::get('/promozioni', [PromotionController::class, 'index'])->name('promozioni.index');
    Route::get('/promozioni/{promotion}', [PromotionController::class, 'promodetail'])->name('promozioni.promodetail');
    Route::get('/chisiamo', [FT_HomeController::class, 'chisiamo'])->name('chisiamo');
    Route::get('/contatti', [FT_HomeController::class, 'contatti'])->name('contatti');
    Route::get('/profilo', [FT_HomeController::class, 'profilo'])->name('profilo');
});

require __DIR__ . '/auth.php';
