<?php

namespace App\Development\Bgp;

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

// bgp_microservice mock - port scans, live session details, PeeringDB prefill and session provisioning.
class Scan
{
    // Every port / circuit allocated to a customer (cxid = the org's gsl identity).
    public function scan(array $params, Request $request)
    {
        $scans = json_decode(file_get_contents(app_path('Development/Bgp/Scan.json')), true);

        return Http::response($scans[Str::afterLast($request->url(), '/')] ?? [], 200);
    }

    // Live per-interface detail and BGP sessions for a scanned port (by temp_id).
    public function details(array $params, Request $request)
    {
        $details = json_decode(file_get_contents(app_path('Development/Bgp/Details.json')), true);

        return Http::response($details[Str::afterLast($request->url(), '/')] ?? [], 200);
    }

    // PeeringDB-style prefill for the add-session form; curated ASNs cover the edge cases.
    public function peeringdb(array $params, Request $request)
    {
        $asn = (int) Str::afterLast($request->url(), '/');
        $networks = json_decode(file_get_contents(app_path('Development/Bgp/Peeringdb.json')), true);

        return Http::response($networks[$asn] ?? [
            'network_name'        => "Example network for AS$asn",
            'irr_as_set'          => "AS-EXAMPLE-$asn",
            'info_prefixes4'      => 50,
            'info_prefixes6'      => 100,
            'asn'                 => $asn,
            'ipv4_subnet'         => '10.16.0.2/31',
            'ipv6_subnet'         => '2401:3cc0::4:502/127',
            'ipv4_is_private'     => true,
            'suggested_ipv4_hash' => "example-v4-hash-$asn",
            'suggested_ipv6_hash' => "example-v6-hash-$asn",
        ], 200);
    }

    // Accepts a new BGP session request.
    public function provision(array $params, Request $request)
    {
        return Http::response([ 'message' => 'Live mode: BGP session request accepted for SID' ], 200);
    }
}
