<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\{Client, Isp, IspIncident};
use Illuminate\Http\Request;

class IspController extends Controller
{
    public function index(Client $client)
    {
        $client->load('isps.incidents');
        return view('admin.clients.isp', compact('client'));
    }

    public function store(Request $request, Client $client)
    {
        $data = $request->validate([
            'provider_name'   => 'required',
            'package'         => 'nullable',
            'bandwidth'       => 'nullable',
            'ip_address'      => 'nullable',
            'account_number'  => 'nullable',
            'support_contact' => 'nullable',
        ]);
        $client->isps()->create($data);
        return back()->with('success','ISP ditambahkan.');
    }

    public function incident(Request $request, Client $client, Isp $isp)
    {
        $data = $request->validate([
            'down_at'     => 'required|date',
            'up_at'       => 'nullable|date',
            'description' => 'nullable|string',
        ]);
        $data['client_id'] = $client->id;
        $data['status']    = empty($data['up_at']) ? 'ongoing' : 'resolved';
        $isp->incidents()->create($data);
        return back()->with('success','Insiden ISP dicatat.');
    }
}
