<?php

namespace App\Http\Controllers;

use App\Models\Router;
use Illuminate\Http\Request;

class RouterController extends Controller
{
    public function index()
    {
        $routers = Router::orderBy('name', 'asc')->get();
        return view('admin.network.routers', compact('routers'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'ip_address' => 'required|ip',
            'model' => 'required|string',
            'location' => 'required|string',
        ]);

        $validated['status'] = 'Online';
        $validated['cpu_usage'] = rand(10, 35);
        $validated['active_sessions'] = rand(20, 150);
        $validated['last_seen'] = now();

        Router::create($validated);

        return redirect()->route('admin.network.routers')->with('success', 'MikroTik Router registered!');
    }

    public function reboot(Router $router)
    {
        $router->update(['last_seen' => now()]);
        return redirect()->route('admin.network.routers')->with('success', "Reboot signal sent to router {$router->name} ({$router->ip_address}).");
    }
}
