<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LicenseController;
use App\Http\Controllers\DeviceLogController;
use App\Http\Controllers\AuthController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// Public routes
Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Setup route (only if no users exist)
Route::get('/setup', [AuthController::class, 'showLoginForm'])->name('setup');
Route::post('/setup', [AuthController::class, 'createAdmin'])->name('setup.create');

// Protected routes
Route::middleware(['auth'])->group(function () {
    Route::get('/', function () {
        return redirect()->route('licenses.index');
    });

    // License Routes
    Route::resource('licenses', LicenseController::class);
    Route::post('/licenses/{license}/renew', [LicenseController::class, 'renew'])->name('licenses.renew');
    Route::post('/licenses/{license}/update-status', [LicenseController::class, 'updateStatus'])->name('licenses.updateStatus');

    // Device Routes
    Route::get('/devices', [DeviceLogController::class, 'index'])->name('devices.index');
    Route::get('/devices/{deviceLog}', [DeviceLogController::class, 'show'])->name('devices.show');
    Route::post('/devices/{deviceLog}/block', [DeviceLogController::class, 'block'])->name('devices.block');
    Route::post('/devices/{deviceLog}/unblock', [DeviceLogController::class, 'unblock'])->name('devices.unblock');
    Route::delete('/devices/{deviceLog}', [DeviceLogController::class, 'destroy'])->name('devices.destroy');
    Route::post('/devices/cleanup', [DeviceLogController::class, 'cleanup'])->name('devices.cleanup');

    // Export Route
    Route::get('/export', [LicenseController::class, 'export'])->name('licenses.export');

    // Settings Route
    Route::get('/settings', [AuthController::class, 'showSettings'])->name('settings');
    Route::put('/settings', [AuthController::class, 'updateCredentials'])->name('settings.update');
    // Add Admin Route
    Route::get('/admin/add', [AuthController::class, 'showAddAdminForm'])->name('admin.add.form');
    Route::post('/admin/add', [AuthController::class, 'addAdmin'])->name('admin.add');
});
