<?php

declare(strict_types=1);

use App\Http\Controllers\AssetController;
use App\Http\Controllers\AssetDocumentController;
use App\Http\Controllers\AssetQrController;
use App\Http\Controllers\AssetWarrantyController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Assets
|--------------------------------------------------------------------------
|
| The core object of the product. Viewing, creating and editing are open to
| every signed-in user, matching every other reference-data module; only
| deletion is master-only.
|
| `export`, `bulk-destroy` and the QR bulk-label routes are static paths and
| must be registered before the `{asset}` resource routes below — Laravel
| matches routes in registration order, and a wildcard route registered
| first would swallow them as if they were an asset ID.
|
| `assets.scan` (the QR landing route) lives in web.php instead of here: it
| carries a deliberately shorter middleware stack (auth + active only, no
| password.change), which only makes sense as its own top-level group.
|
*/

Route::get('assets/export', [AssetController::class, 'export'])->name('assets.export');
Route::get('assets/tag-preview', [AssetController::class, 'tagPreview'])->name('assets.tag-preview');

Route::middleware('master')->post('assets/bulk-destroy', [AssetController::class, 'bulkDestroy'])
    ->name('assets.bulk-destroy');

Route::get('assets/qr/bulk', [AssetQrController::class, 'bulk'])->name('assets.qr.bulk');
Route::post('assets/qr/bulk/preview', [AssetQrController::class, 'bulkPreview'])->name('assets.qr.bulk.preview');
Route::post('assets/qr/bulk/pdf', [AssetQrController::class, 'bulkPdf'])->name('assets.qr.bulk.pdf');

Route::resource('assets', AssetController::class)->except(['destroy']);

Route::prefix('assets/{asset}')->name('assets.')->group(function (): void {
    Route::patch('status', [AssetController::class, 'updateStatus'])->name('status');
    Route::get('history', [AssetController::class, 'history'])->name('history');
    Route::get('qr', [AssetQrController::class, 'show'])->name('qr');
    Route::get('qr/png', [AssetQrController::class, 'png'])->name('qr.png');
    Route::get('qr/print', [AssetQrController::class, 'print'])->name('qr.print');

    Route::get('warranties/create', [AssetWarrantyController::class, 'create'])->name('warranties.create');
    Route::post('warranties', [AssetWarrantyController::class, 'store'])->name('warranties.store');

    Route::prefix('warranties/{warranty}')->name('warranties.')->group(function (): void {
        Route::get('edit', [AssetWarrantyController::class, 'edit'])->name('edit');
        Route::put('/', [AssetWarrantyController::class, 'update'])->name('update');
        Route::get('renew', [AssetWarrantyController::class, 'renewForm'])->name('renew.create');
        Route::post('renew', [AssetWarrantyController::class, 'renew'])->name('renew.store');
        Route::get('document', [AssetWarrantyController::class, 'document'])->name('document');
    });

    Route::post('documents', [AssetDocumentController::class, 'store'])->name('documents.store');

    Route::prefix('documents/{document}')->name('documents.')->group(function (): void {
        Route::get('/', [AssetDocumentController::class, 'show'])->name('show');
        Route::get('download', [AssetDocumentController::class, 'download'])->name('download');
    });
});

Route::middleware('master')->group(function (): void {
    Route::delete('assets/{asset}', [AssetController::class, 'destroy'])->name('assets.destroy');

    // withTrashed() lets implicit model binding resolve a soft-deleted record,
    // which is the only way to bind the thing being brought back.
    Route::patch('assets/{asset}/restore', [AssetController::class, 'restore'])
        ->withTrashed()
        ->name('assets.restore');

    Route::delete('assets/{asset}/warranties/{warranty}', [AssetWarrantyController::class, 'destroy'])
        ->name('assets.warranties.destroy');

    Route::patch('assets/{asset}/warranties/{warranty}/restore', [AssetWarrantyController::class, 'restore'])
        ->withTrashed()
        ->name('assets.warranties.restore');

    Route::delete('assets/{asset}/documents/{document}', [AssetDocumentController::class, 'destroy'])
        ->name('assets.documents.destroy');
});
