<?php

// Laravel
use Illuminate\Support\Facades\Route;

// Creatia
use App\Http\Controllers\Goliath\AdminReportController;
use App\Http\Controllers\Goliath\ReportController;
use App\Http\Controllers\Mitigation\AdminAttackController;
use App\Http\Controllers\Mitigation\AttackController;

/*
 * Attack visibility served by Oculi. These run in parallel with the legacy GoliathReport routes until
 * parity is proven, at which point the legacy tree is retired - nothing here shares state with it.
 */
class MitigationAttack {

    public static function routes() {
        static::guest_attack();
        static::user_attack();
        static::user_carpet();
        static::admin_attack();
        static::admin_carpet();
    }

    private static function guest_attack() {
        Route::group(['middleware' => ['api'], 'prefix' => 'guest'], function ($r) {

            // Tokenised report downloads minted by mitigation:queue-reports.
            Route::get('/mitigation/report/download/{_token}', [AttackController::class, 'fetch_from_link']);

        });
    }

    private static function user_attack() {

        Route::group(['middleware' => ['auth:api', "bounce:mitigation.attack.read"], 'prefix' => 'mitigation/attack'], function ($r) {

            /*
             * List Endpoints
             */

            Route::get('/', [AttackController::class, 'fetch_table']);
            Route::get('/summary', [AttackController::class, 'fetch_summary']);
            Route::get('/ongoing', [AttackController::class, 'fetch_ongoing']);

            /*
             * Report Fetching Endpoints
             */

            Route::get('/{_attack_id}/events', [AttackController::class, 'fetch_events'])->where([ '_attack_id' => '[0-9a-f-]+' ]);
            Route::get('/{_attack_id}/report', [AttackController::class, 'fetch_report'])->where([ '_attack_id' => '[0-9a-f-]+' ]);

            /*
             * Action endpoints
             */

            /**
             * Notification settings are one store however you reach them - these are the mitigation
             * tree's doors onto the same legacy attribute handlers the goliath/report tree uses.
             */
            Route::get('/settings', [ReportController::class, 'fetch_attributes']);

        });

        Route::group(['middleware' => ['auth:api', "bounce:mitigation.attack.write"], 'prefix' => 'mitigation/attack'], function ($r) {

            Route::post('/settings', [ReportController::class, 'set_attributes']);

        });

    }

    /**
     * Carpet-bomb attacks are a view over the same attacks, scoped the same way, so they sit behind
     * the same read scope rather than a scope of their own.
     */
    private static function user_carpet() {

        Route::group(['middleware' => ['auth:api', "bounce:mitigation.attack.read"], 'prefix' => 'mitigation/carpet'], function ($r) {

            /*
             * List Endpoints
             */

            Route::get('/', [AttackController::class, 'fetch_carpet_table']);
            Route::get('/summary', [AttackController::class, 'fetch_carpet_summary']);
            Route::get('/ongoing', [AttackController::class, 'fetch_carpets_ongoing']);

            /*
             * Report Fetching Endpoints
             */

            Route::get('/{_carpet_id}/attacks', [AttackController::class, 'fetch_carpet_attacks'])->where([ '_carpet_id' => '[0-9a-f-]+' ]);
            Route::get('/{_carpet_id}/report', [AttackController::class, 'fetch_carpet_report'])->where([ '_carpet_id' => '[0-9a-f-]+' ]);

        });

    }

    private static function admin_carpet() {

        Route::group(['middleware' => ['auth:api', "bounce:admin.mitigation.attack.read"], 'prefix' => 'admin/mitigation/carpet'], function ($r) {

            /*
             * List Endpoints
             */

            Route::get('/', [AdminAttackController::class, 'fetch_carpet_table']);
            Route::get('/summary', [AdminAttackController::class, 'fetch_carpet_summary']);
            Route::get('/ongoing', [AdminAttackController::class, 'fetch_carpets_ongoing']);

            /*
             * Report Fetching Endpoints
             */

            Route::get('/{_carpet_id}/attacks', [AdminAttackController::class, 'fetch_carpet_attacks'])->where([ '_carpet_id' => '[0-9a-f-]+' ]);
            Route::get('/{_carpet_id}/report', [AdminAttackController::class, 'fetch_carpet_report'])->where([ '_carpet_id' => '[0-9a-f-]+' ]);

        });

    }

    private static function admin_attack() {

        Route::group(['middleware' => ['auth:api', "bounce:admin.mitigation.attack.read"], 'prefix' => 'admin/mitigation/attack'], function ($r) {

            /*
             * List Endpoints
             */

            Route::get('/', [AdminAttackController::class, 'fetch_table']);
            Route::get('/summary', [AdminAttackController::class, 'fetch_summary']);
            Route::get('/ongoing', [AdminAttackController::class, 'fetch_ongoing']);
            Route::get('/leaderboard', [AdminAttackController::class, 'fetch_leaderboard']);
            Route::get('/notification', [AdminAttackController::class, 'fetch_notifications']);

            /*
             * Report Fetching Endpoints
             */

            Route::get('/{_attack_id}/events', [AdminAttackController::class, 'fetch_events'])->where([ '_attack_id' => '[0-9a-f-]+' ]);
            Route::get('/{_attack_id}/report', [AdminAttackController::class, 'fetch_report'])->where([ '_attack_id' => '[0-9a-f-]+' ]);

            /*
             * Action endpoints
             */

            /**
             * The admin handlers take the organisation from the `organisation_id` input, which is all
             * the legacy attribute methods ever honoured - the old route parameter was decorative.
             */
            Route::get('/settings', [AdminReportController::class, 'fetch_attributes']);

        });

        Route::group(['middleware' => ['auth:api', "bounce:admin.mitigation.attack.write"], 'prefix' => 'admin/mitigation/attack'], function ($r) {

            Route::post('/settings', [AdminReportController::class, 'set_attributes']);

        });

    }
}
