<?php

namespace App\Modules\Network;

use App\Models\User;

use App\Modules\Network\Controllers\NetworkController;
use App\Modules\Network\Controllers\AdminNetworkController;

use App\Modules\Network\Controllers\AdminGoliathController;
use App\Modules\Network\Controllers\GoliathController;

use Illuminate\Support\Facades\Broadcast;

use Illuminate\Support\Facades\Route;

class GoliathRoutes {
    public static function register() {
        /* URI SLUGS */
        $admin_slug         = 'admin';
        $goliath_slug       = 'goliath';
    
        /* PERMISSIONS */
        $goliath_l1_view = 'goliath.l1.view';
        $goliath_l1_edit = 'goliath.l1.edit';
        $goliath_l1_log = 'goliath.l1.log';
        $goliath_l1_capture = 'goliath.l1.capture';
        $goliath_l1_chart = 'goliath.l1.chart';
        $goliath_l1_manage = 'goliath.l1.manage';
    
        $goliath_l2_view = 'goliath.l2.view';
        $goliath_l2_edit = 'goliath.l2.edit';
        $goliath_l2_log = 'goliath.l2.log';
        $goliath_l2_manage = 'goliath.l2.manage';
    
        // LEVEL 1 VIEWABLE - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_view"], 'prefix' => "$goliath_slug"], function ($router) {
    
            Route::get('/prefixes/{_network_id}', [GoliathController::class, 'get_prefix_list']);               // View subnets in a supernet
            Route::get('/prefixes', [GoliathController::class, 'get_prefix_list']);                             // View all prefixes
            Route::get('/prefix/options', [NetworkController::class, 'get_prefix_options']);                    // View prefix options
        });
    
        // LEVEL 1 VIEWABLE - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_view,$goliath_l1_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
    
            Route::get('/device/options', [AdminGoliathController::class, 'fetch_appliances_options']);                                 // Get list of appliance options
            Route::get('/device', [AdminGoliathController::class, 'fetch_appliance_list']);                                             // Get a list of appliances
    
            Route::get('/device/region/{_region_id}/{_unallocated?}', [AdminGoliathController::class, 'fetch_appliances_list']);        // Get list of appliances per region
            Route::get('/device/subregion/{_subregion_id}/{_unallocated?}', [AdminGoliathController::class, 'fetch_appliances_list']);  // Get list of appliances per subregion
            Route::get('/device/metro/{_metro_id}', [AdminGoliathController::class, 'fetch_appliances_list']);                          // Get list of appliances per datacentre
    
            // View prefixes
            Route::get('/prefixes/{_network_id}', [AdminGoliathController::class, 'get_prefix_list']);                                  // Fetch list of subnets for a supernet
            Route::get('/prefixes', [AdminGoliathController::class, 'get_prefix_list']);                                                // Fetch list of prefixes
            Route::get('/prefix/options', [AdminNetworkController::class, 'get_prefix_options']);                                       // View prefixes as options
        });
    
        // LEVEL 1 EDIT - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_edit"], 'prefix' => "$goliath_slug"], function ($router) {
            
            Route::get('/{_network_id}/subnet', [GoliathController::class, 'get_inside_subnet']);   // Check whether the subnet can be added
            Route::post('/{_network_id}/subnet', [GoliathController::class, 'post_subnet']);        // Add subnet
    
            Route::get('/prefix/{_prefix_id}/flag', [GoliathController::class, 'get_flags']);       // Get flags for editing 
            Route::put('/prefix/{_prefix_id}/flag', [GoliathController::class, 'put_flags']);       // Edit flags
    
            Route::get('/prefix/default/{_asn_id}', [GoliathController::class, 'get_defaults']);    // Get set defaults for an ASN
            Route::put('/prefix/default/{_asn_id}', [GoliathController::class, 'put_defaults']);    // Set defaults for an ASN
        });
    
        // LEVEL 1 EDIT - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_edit,$goliath_l1_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
    
            Route::get('/{_network_id}/subnet', [AdminGoliathController::class, 'get_inside_subnet']);  // Check whether the subnet can be added
            Route::post('/{_network_id}/subnet', [AdminGoliathController::class, 'post_subnet']);       // Add subnet
    
            Route::get('/prefix/{_prefix_id}/flag', [AdminGoliathController::class, 'get_flags']);      // Get flags
            Route::put('/prefix/{_prefix_id}/flag', [AdminGoliathController::class, 'put_flags']);      // Edit flags
    
            Route::get('/prefix/default/{_asn_id}', [AdminGoliathController::class, 'get_defaults']);   // Get defaults for an ASN
            Route::put('/prefix/default/{_asn_id}', [AdminGoliathController::class, 'put_defaults']);   // Set defaults for an ASN
    
            Route::get('/rollback/options', [AdminGoliathController::class, 'fetch_rollback_options']); // Get a list of rollback points
            Route::post('/rollback', [AdminGoliathController::class, 'rollback_appliances']);           // Execute an appliance rollback
        });
    
        // LEVEL 1 LOG - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_log"], 'prefix' => "$goliath_slug"], function ($router) {
            Route::get('/activity/mitigation', [GoliathController::class, 'get_recent_changes']); // Audit trail for flag changes
            Route::get('/activity/attack/{_prefix_id?}', [GoliathController::class, 'get_attack_logs']); // List of attacks against a customer
            Route::get('/summary/attack', [GoliathController::class, 'get_attack_summary']); // Summary of attack information
        });
    
        // LEVEL 1 LOG - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_log,$goliath_l1_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
            Route::get('/activity/mitigation', [AdminGoliathController::class, 'get_recent_changes']); // Audit trail for recent flag changes
            Route::get('/activity/attack/{_prefix_id?}', [AdminGoliathController::class, 'get_attack_logs']); // List of attacks against a customer
            Route::get('/summary/attack', [AdminGoliathController::class, 'get_attack_summary']); // Summary of attack information
        });
    
        // LEVEL 1 CAPTURE - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_capture"], 'prefix' => "$goliath_slug"], function ($router) {
    
            Route::get('/capture/options', [GoliathController::class, 'get_appliance_options']);            // Get list of appliance options
            Route::get('/capture', [GoliathController::class, 'get_captures']);                             // Get list of captures
            Route::get('/capture/result/{_capture_id}', [GoliathController::class, 'download_capture']);    // Download a specific capture
            Route::post('/capture/{_appliance_id}', [GoliathController::class, 'post_captures']);           // Post capture for a given appliance
        });
    
        // LEVEL 1 CAPTURE - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_capture,$goliath_l1_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
            
            Route::get('/capture/options', [AdminGoliathController::class, 'get_appliance_options']);           // Get list of appliance options
            Route::get('/capture', [AdminGoliathController::class, 'get_captures']);                            // Get list of captures
            Route::get('/capture/result/{_capture_id}', [AdminGoliathController::class, 'download_capture']);   // Download a specific capture
            Route::post('/capture/{_appliance_id}', [AdminGoliathController::class, 'post_captures']);          // Post capture for a given appliance
        });

        // LEVEL 1 CHARTS - Regular
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_chart"], 'prefix' => "$goliath_slug"], function ($router) {
            Route::get('/usage', [GoliathController::class, 'get_usage_chart']); // Usage for ALL user ASNs
        });

        Broadcast::channel('goliathTraffic.{organisation_id}', function ($user, $organisation_id) use($goliath_l1_chart) {
            return ($user->organisation_id == $organisation_id) && User::find($user->id)->canDo($goliath_l1_chart);
        });

        // LEVEL 1 CHARTS - Management
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l1_chart,$goliath_l1_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
            Route::get('/usage', [AdminGoliathController::class, 'get_usage_chart']); // Usage for ALL user ASNs
        });

        Broadcast::channel('goliathAdminTraffic', function ($user) use($goliath_l1_chart, $goliath_l1_manage) {
            return User::find($user->id)->canDo([$goliath_l1_chart, $goliath_l1_manage]);
        });
    
        // LEVEL 2 VIEWABLE - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_view"], 'prefix' => "$goliath_slug"], function ($router) {
            
            Route::get('/chains', [GoliathController::class, 'get_chains']);                                // Get a list of all chains
            Route::get('/chains/{_prefix_id}', [GoliathController::class, 'get_chains']);                   // Get list of chains attached to a prefix
            Route::get('/chains/{_chain_id}/prefixes', [GoliathController::class, 'get_chain_prefixes']);   // Get a list of prefixes attached to a chain
            Route::get('/chain/{_chain_id}', [GoliathController::class, 'get_chain']);                      // Get an individual chain
    
            Route::get('/prefixes/chain/{_chain_id}', [GoliathController::class, 'get_chain_prefix_list']); // Get a list of prefixes attached to a chain
        });
    
        // LEVEL 2 VIEWABLE - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_view,$goliath_l2_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
    
            Route::get('/chains', [AdminGoliathController::class, 'get_chains']);                                   // Get a list of all chains
            Route::get('/chains/{_prefix_id}', [AdminGoliathController::class, 'get_chains']);                      // Get a list of chains attached to a prefix
            Route::get('/chains/{_chain_id}/prefixes', [AdminGoliathController::class, 'get_chain_prefixes']);      // Get a list of prefixes attached to a chain
            Route::get('/chain/{_chain_id}', [AdminGoliathController::class, 'get_chain']);                         // Get an individual chain
    
            Route::get('/prefixes/chain/{_chain_id}', [AdminGoliathController::class, 'get_chain_prefix_list']);    // Get a list of prefixes attached to a chain
        });
    
        // LEVEL 2 VIEWABLE - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_edit"], 'prefix' => "$goliath_slug"], function ($router) {
    
            Route::get('/options', [GoliathController::class, 'get_chain_options']);                        // Get a list of options to use in editing
    
            Route::post('/chain', [GoliathController::class, 'put_chain']);                                 // Create a new chain
    
            Route::put('/chain/{_chain_id}', [GoliathController::class, 'put_chain']);                      // Edit an existing chain
    
            Route::delete('/chain/{_chain_id}', [GoliathController::class, 'delete_chain']);                // Delete an existing chain
    
            Route::put('/prefix/{_prefix_id}', [GoliathController::class, 'put_prefix_chain']);             // Associate a chain with a prefix
    
            Route::delete('/prefix/{_prefix_id}', [GoliathController::class, 'delete_prefix_chain']);       // Disassociate a chain with a prefix
    
            Route::get('/prefix/{_prefix_id}/occupied', [GoliathController::class, 'get_would_replace']);   // Check whether a new chain would replace an existing chain on a prefix
        });
    
        // LEVEL 2 VIEWABLE - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_edit,$goliath_l2_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
    
            Route::get('/options', [AdminGoliathController::class, 'get_chain_options']);                       // Get a list of options to use in editing
            
            Route::post('/chain', [AdminGoliathController::class, 'put_chain']);                                // Create a new chain
    
            Route::put('/chain/{_chain_id}', [AdminGoliathController::class, 'put_chain']);                     // Edit an existing chain
    
            Route::delete('/chain/{_chain_id}', [AdminGoliathController::class, 'delete_chain']);               // Delete an existing chain
    
            Route::put('/prefix/{_prefix_id}', [AdminGoliathController::class, 'put_prefix_chain']);            // Associate a chain with a prefix
    
            Route::delete('/prefix/{_prefix_id}', [AdminGoliathController::class, 'delete_prefix_chain']);      // Disassociate a chain with a prefix
    
            Route::get('/prefix/{_prefix_id}/occupied', [AdminGoliathController::class, 'get_would_replace']);  // Check whether a new chain would replace an existing chain on a prefix
        });
    
        // LEVEL 2 VIEWABLE - REGULAR
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_log"], 'prefix' => "$goliath_slug"], function ($router) {
    
        });
    
        // LEVEL 2 VIEWABLE - MANAGEMENT
        Route::group(['middleware' => ['auth:api',"bounce:$goliath_l2_log,$goliath_l2_manage"], 'prefix' => "$admin_slug/$goliath_slug"], function ($router) {
    
        });
    }
}