<?php

use App\Http\Controllers\ActivityLogController;
use App\Http\Controllers\OltDeviceController;
use App\Http\Controllers\OnuController;
use App\Http\Controllers\PonPowerController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RogueOnuController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('dashboard');
});

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', function () {
        $devices    = \App\Models\OltDevice::withCount('ports')->get();
        $totalOnus  = \App\Models\OltOnu::count();
        $recentLogs = \App\Models\ActivityLog::with('user', 'device')->latest()->take(10)->get();
        return view('dashboard', compact('devices', 'totalOnus', 'recentLogs'));
    })->name('dashboard');

    // Profile (Breeze)
    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');

    // OLT Devices
    Route::resource('devices', OltDeviceController::class);
    Route::post('devices/{device}/test-connection', [OltDeviceController::class, 'testConnection'])
        ->name('devices.test-connection');

    // ONUs
    Route::post('onus/preview', [OnuController::class, 'previewCommands'])->name('onus.preview');
    Route::post('onus/next-position', [OnuController::class, 'nextPosition'])->name('onus.next-position');
    Route::post('onus/{onu}/optical-power', [OnuController::class, 'opticalPower'])->name('onus.optical-power');
    Route::resource('onus', OnuController::class);

    // Rogue ONUs
    Route::get('rogue', [RogueOnuController::class, 'index'])->name('rogue.index');
    Route::post('rogue/{device}/query', [RogueOnuController::class, 'query'])->name('rogue.query');

    // PON Power Monitor
    Route::get('pon-power', [PonPowerController::class, 'index'])->name('pon-power.index');
    Route::post('pon-power/{device}/slots', [PonPowerController::class, 'getSlots'])->name('pon-power.slots');
    Route::post('pon-power/{device}/power', [PonPowerController::class, 'getPower'])->name('pon-power.power');

    // Activity Logs
    Route::get('logs', [ActivityLogController::class, 'index'])->name('logs.index');
    Route::get('logs/{activityLog}', [ActivityLogController::class, 'show'])->name('logs.show');

    // User Management (admin only)
    Route::middleware('admin')->group(function () {
        Route::resource('users', UserController::class)->except('show');
    });
});

require __DIR__ . '/auth.php';
