<?php

/*
|--------------------------------------------------------------------------
| Web Routes — Smart Storage
|--------------------------------------------------------------------------
*/

use Modules\SmartStorage\Http\Controllers\InstallController;
use Modules\SmartStorage\Http\Controllers\SettingsController;
use Modules\SmartStorage\Http\Controllers\AccessLogController;
use Modules\SmartStorage\Http\Controllers\DeviceController;
use Modules\SmartStorage\Http\Controllers\OtpController;
use Modules\SmartStorage\Http\Controllers\FileServeController;

// Install / Update / Uninstall (used by the module-management page)
Route::middleware(['web', 'auth'])->prefix('smart-storage-module')->group(function () {
    Route::get('/install', [InstallController::class, 'index']);
    Route::post('/install', [InstallController::class, 'install']);
    Route::get('/uninstall', [InstallController::class, 'uninstall']);
    Route::get('/update', [InstallController::class, 'update']);
    Route::get('/repair', [InstallController::class, 'repair']);
});

// Admin UI
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone'],
    'prefix'     => 'smart-storage',
], function () {
    Route::get('/',                 [SettingsController::class, 'index'])->name('smart-storage.index');
    Route::post('/settings',        [SettingsController::class, 'update'])->name('smart-storage.settings.update');
    Route::post('/test-backend',    [SettingsController::class, 'testBackend'])->name('smart-storage.test');

    Route::get('/logs',             [AccessLogController::class, 'index'])->name('smart-storage.logs');
    Route::get('/logs/data',        [AccessLogController::class, 'data'])->name('smart-storage.logs.data');

    Route::get('/devices',          [DeviceController::class, 'index'])->name('smart-storage.devices');
    Route::post('/devices/{id}/approve', [DeviceController::class, 'approve'])->name('smart-storage.devices.approve');
    Route::post('/devices/{id}/revoke',  [DeviceController::class, 'revoke'])->name('smart-storage.devices.revoke');
    Route::delete('/devices/{id}',  [DeviceController::class, 'destroy'])->name('smart-storage.devices.destroy');

    // OTP challenge to open a storage-access session
    Route::get('/otp',              [OtpController::class, 'challenge'])->name('smart-storage.otp');
    Route::post('/otp/send',        [OtpController::class, 'send'])->name('smart-storage.otp.send');
    Route::post('/otp/verify',      [OtpController::class, 'verify'])->name('smart-storage.otp.verify');
});

/*
 | Transparent file-serving fallback.
 |
 | Files routed to a non-public backend (local-smart / cloud / encrypted) no
 | longer exist under public/uploads, so the legacy asset() URL 404s in Apache
 | and lands here. This route authorises (device/network/OTP), decrypts and
 | streams the file while logging the call. Existing files still physically
 | present under public/ are served by Apache and never reach this route.
 */
Route::middleware(['web'])
    ->get(trim(config('smartstorage.public_prefix', 'uploads'), '/') . '/{path}', [FileServeController::class, 'serve'])
    ->where('path', '.*')
    ->name('smart-storage.serve');
