<?php

use App\Http\Controllers\AccountPasswordController;
use App\Http\Controllers\BookController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\PlaceholderController;
use App\Http\Controllers\PrinterController;
use App\Http\Controllers\PrinterSearchController;
use App\Http\Controllers\ReadingController;
use App\Http\Controllers\ServiceHistoryController;
use App\Http\Controllers\TechHomeController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

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

    return redirect()->route(auth()->user()->homeRoute());
});

Route::middleware('auth')->group(function () {
    // Admin-only modules
    Route::middleware('role:admin')->group(function () {
        Route::get('/dashboard',  DashboardController::class)->name('dashboard');
        Route::get('/services',   [ServiceHistoryController::class, 'index'])->name('services.index');

        Route::get('/clients',                 [ClientController::class, 'index'])->name('clients.index');
        Route::get('/clients/create',          [ClientController::class, 'create'])->name('clients.create');
        Route::post('/clients',                [ClientController::class, 'store'])->name('clients.store');
        Route::get('/clients/{client}',        [ClientController::class, 'show'])->whereNumber('client')->name('clients.show');
        Route::get('/clients/{client}/edit',   [ClientController::class, 'edit'])->whereNumber('client')->name('clients.edit');
        Route::put('/clients/{client}',        [ClientController::class, 'update'])->whereNumber('client')->name('clients.update');

        Route::get('/printers',                 [PrinterController::class, 'index'])->name('printers.index');
        Route::get('/printers/create',          [PrinterController::class, 'create'])->name('printers.create');
        Route::post('/printers',                [PrinterController::class, 'store'])->name('printers.store');
        Route::get('/printers/{printer}',       [PrinterController::class, 'show'])->whereNumber('printer')->name('printers.show');
        Route::get('/printers/{printer}/edit',  [PrinterController::class, 'edit'])->whereNumber('printer')->name('printers.edit');
        Route::put('/printers/{printer}',       [PrinterController::class, 'update'])->whereNumber('printer')->name('printers.update');
        Route::get('/printers/{printer}/qr',    [PrinterController::class, 'qr'])->whereNumber('printer')->name('printers.qr');

        Route::get('/printers/{printer}/readings/create', [ReadingController::class, 'create'])->whereNumber('printer')->name('readings.create');
        Route::post('/printers/{printer}/readings',       [ReadingController::class, 'store'])->whereNumber('printer')->name('readings.store');
        Route::get('/readings/{reading}/edit',            [ReadingController::class, 'edit'])->whereNumber('reading')->name('readings.edit');
        Route::put('/readings/{reading}',                 [ReadingController::class, 'update'])->whereNumber('reading')->name('readings.update');
        Route::delete('/readings/{reading}',              [ReadingController::class, 'destroy'])->whereNumber('reading')->name('readings.destroy');

        Route::get('/users',                    [UserController::class, 'index'])->name('users.index');
        Route::get('/users/create',             [UserController::class, 'create'])->name('users.create');
        Route::post('/users',                   [UserController::class, 'store'])->name('users.store');
        Route::patch('/users/{user}/deactivate', [UserController::class, 'deactivate'])->name('users.deactivate');
        Route::patch('/users/{user}/activate',  [UserController::class, 'activate'])->name('users.activate');
        Route::get('/users/{user}/password',  [UserController::class, 'editPassword'])->name('users.password.edit');
        Route::put('/users/{user}/password',  [UserController::class, 'updatePassword'])->name('users.password.update');
    });

    // Technician (and admin) routes
    Route::get('/tehnicar',        TechHomeController::class)->name('tech.home');
    Route::get('/printers/search', [PrinterSearchController::class, 'search'])->name('printers.search');
    Route::get('/p/{printer:qr_token}', [BookController::class, 'show'])->name('book.show');
    Route::get('/scan',            PlaceholderController::class)->defaults('module', 'Скенирај код')->name('scan');

    // Self-service password change (both roles)
    Route::get('/account/password',  [AccountPasswordController::class, 'edit'])->name('account.password.edit');
    Route::put('/account/password',  [AccountPasswordController::class, 'update'])->name('account.password.update');
});

require __DIR__.'/auth.php';
