<?php

namespace App\Development;

/**
 * Development HTTP mock route table.
 *
 * register() returns a map of:
 *
 *     '<url pattern>' => ['<handler class>', '<method>']
 *
 * The URL pattern is a standard Http::fake() glob (Laravel auto-prefixes '*', so
 * a scheme-less host like '127.0.0.1:5000/asn*' still matches an https URL). The
 * handler class is resolved relative to the App\Development namespace, and its
 * method is invoked with the merged request params and the raw client Request.
 *
 * Each entry here replaces an inline development Http::fake() that previously
 * lived in a controller/repository/model, or provides a mock for an outbound
 * call that had none (so preventStrayRequests() does not break dev flows).
 */
class Manifest
{
    public function register(): array
    {
        $route_server = config('creatia.endpoint.route_server');
        $mercury      = env('tunnel_microservice_url');
        $compute      = env('compute_microservice_url');
        $bgp          = env('bgp_microservice');
        $bgp_v2       = env('bgp_microservice_v2');
        $billing      = env('ENDPOINT_BILLING_MICROSERVICE_URL');
        $overage      = env('ENDPOINT_OVERAGE_MICROSERVICE_URL');
        $bgp_forward  = config('creatia.endpoint.bgp_forward');
        $radb         = config('creatia.radb.endpoint');
        $reporting    = config('creatia.reporting.microservice_url');
        $whois        = explode(',', env('ENDPOINT_WHOIS'))[0];
        $oculi        = config('creatia.oculi.url');

        $routes = [
            // Automation route server — learned prefixes
            "{$route_server}/api/routes/v4" => ['Automation\Routes', 'v4'],
            "{$route_server}/api/routes/v6" => ['Automation\Routes', 'v6'],

            // Mercury tunnel microservice
            "{$mercury}/api/v1/bgp/cxid/*"            => ['Mercury\Routes', 'socket_bgp'],
            "{$mercury}/api/v1/peaks/*"               => ['Mercury\Routes', 'socket_peak'],
            "{$mercury}/api/v1/billing/*"             => ['Mercury\Routes', 'socket_billing'],
            "{$mercury}/api/v1/tunnels/by-cxid/*"     => ['Mercury\Routes', 'list_tunnels'],
            "{$mercury}/api/v1/tunnels/by-endpoint/*" => ['Mercury\Routes', 'by_endpoint'],
            "{$mercury}/api/v1/tunnels"               => ['Mercury\Routes', 'create_tunnel'],

            // Compute microservice (status before the broader peers glob)
            "{$compute}/api/status/peers" => ['Compute\Peers', 'sessions'],
            "{$compute}/api/peers*"       => ['Compute\Peers', 'peers'],

            // BGP v2
            "{$bgp_v2}/custview/cxid/*"                    => ['Bgp\Transit', 'scan'],
            "{$bgp_v2}/custview/live/*"                    => ['Bgp\Transit', 'details'],
            "{$bgp_v2}/custview/job/*"                     => ['Bgp\Transit', 'job_status'],
            "{$bgp_v2}/custview/*/bgp"                     => ['Bgp\Transit', 'provision'],
            "{$bgp_v2}/custview/*/session-config"          => ['Bgp\Transit', 'provision'],
            "{$bgp_v2}/custview/*/bgp/delete"              => ['Bgp\Transit', 'provision'],
            "{$bgp_v2}/custview/*/evc"                     => ['Bgp\Transit', 'provision'],
            "{$bgp_v2}/custview/*/evc/*/delete"            => ['Bgp\Transit', 'provision'],
            "{$bgp_v2}/custview/*/portconfig"              => ['Bgp\Transit', 'port_config'],
            "{$bgp_v2}/custview/*/portconfig/lag/member"   => ['Bgp\Transit', 'lag_member'],
            "{$bgp_v2}/custview/*/portconfig/lag/create"   => ['Bgp\Transit', 'lag_create'],
            "{$bgp_v2}/custview/*/lplimit"                 => ['Bgp\Transit', 'lp_limit'],
            "{$bgp_v2}/custview/*/target-id"               => ['Bgp\Transit', 'target_create'],
            "{$bgp_v2}/custview/*/target-id/*"             => ['Bgp\Transit', 'target_lookup'],
            "{$bgp_v2}/peeringdb/asn/*"                    => ['Bgp\Scan', 'peeringdb'],

            // BGP interface scan / live details / add-session
            "{$bgp}/scan/cxid/*"     => ['Bgp\Scan', 'scan'],
            "{$bgp}/live/details/*"  => ['Bgp\Scan', 'details'],
            "{$bgp}/peeringdb/asn/*" => ['Bgp\Scan', 'peeringdb'],
            "{$bgp}/provision/*"     => ['Bgp\Scan', 'provision'],

            // Billing microservice directory
            "{$billing}*" => ['Billing\Routes', 'list'],

            // Overage microservice (v1 and versioned paths)
            "{$overage}/graph*"    => ['Billing\Routes', 'graph'],
            "{$overage}/v*/graph*" => ['Billing\Routes', 'graph'],
            "{$overage}/invoice*"    => ['Billing\Routes', 'invoice'],
            "{$overage}/v*/invoice*" => ['Billing\Routes', 'invoice'],

            // BGP forward / Arista
            "{$bgp_forward}/validate*" => ['Bgp\Forward', 'validate'],
            "{$bgp_forward}/ping*"     => ['Bgp\Forward', 'ping'],
            "{$bgp_forward}/reroute*"  => ['Bgp\Forward', 'reroute'],
            "{$bgp_forward}/unroute*"  => ['Bgp\Forward', 'unroute'],

            // RADb registry
            "{$radb}/radb/as-set/*" => ['Radb\Registry', 'asSet'],
            "{$radb}/radb/search*"  => ['Radb\Registry', 'search'],

            // Reporting microservice (automation before the narrower destination-table)
            "{$reporting}/creatia-report/automation-destination-table*" => ['Reporting\Routes', 'automation_table'],
            "{$reporting}/creatia-report/destination-table*"            => ['Reporting\Routes', 'destination_table'],
            "{$reporting}/creatia-report/leaderboard*"                  => ['Reporting\Routes', 'leaderboard'],
            "{$reporting}/creatia-report/destination-report*"           => ['Reporting\Routes', 'report'],
            "{$reporting}/creatia-notification/start*"                  => ['Reporting\Routes', 'notification'],

            // WHOIS forwarders
            "{$whois}/asn*"    => ['Whois\Lookup', 'asn'],
            "{$whois}/as_set*" => ['Whois\Lookup', 'asSet'],

            // PeeringDB (public API)
            '*peeringdb.com/api/net*' => ['Peering\Db', 'net'],

            // Goliath ruleset cost validation
            '*:5000/validate/cost' => ['Goliath\Validator', 'cost'],

            // Webhooks (Slack / Discord / report hooks). Destinations are dynamic,
            // so match by provider host or a "/hook/" path segment and 200 them.
            'hooks.slack.com/*'           => ['Webhook\Accept', 'ok'],
            '*discord.com/api/webhooks/*' => ['Webhook\Accept', 'ok'],
            '*/hook/*'                    => ['Webhook\Accept', 'ok'],
        ];

        // Oculi is optional per environment, and an empty URL would collapse to a '*' catch-all.
        if (! empty($oculi)) {
            $routes["{$oculi}/mitigation/events"]            = ['Oculi\Routes', 'events'];
            $routes["{$oculi}/mitigation/organisations"]     = ['Oculi\Routes', 'organisations'];
            $routes["{$oculi}/mitigation/attacks/table*"]    = ['Oculi\Routes', 'attacks_table'];
            $routes["{$oculi}/mitigation/attacks/summary*"]  = ['Oculi\Routes', 'attacks_summary'];
            $routes["{$oculi}/mitigation/attacks/ongoing*"]  = ['Oculi\Routes', 'attacks_ongoing'];
            $routes["{$oculi}/mitigation/attacks/*/report*"] = ['Oculi\Routes', 'attack_report'];
            $routes["{$oculi}/mitigation/attacks/*/events*"] = ['Oculi\Routes', 'attack_events'];
            $routes["{$oculi}/mitigation/leaderboard*"]      = ['Oculi\Routes', 'leaderboard'];
        }

        // ClickHouse only talks HTTP when a discrete endpoint is configured
        // (development uses the native client), so only register it if present —
        // an empty host would collapse to a '*' catch-all and defeat strict mode.
        $clickhouse = config('creatia.clickhouse.endpoint');

        if (! empty($clickhouse)) {
            $routes["{$clickhouse}*"] = ['Clickhouse\Query', 'run'];
        }

        return $routes;
    }
}
