<?php

use Ignite\Core\Http\Controllers\Admin\AdminController;
use Ignite\Core\Http\Controllers\Admin\BlockController;
use Ignite\Core\Http\Controllers\Admin\GroupsController;
use Ignite\Core\Http\Controllers\Admin\ImportController;
use Ignite\Core\Http\Controllers\Admin\InternalController;
use Ignite\Core\Http\Controllers\Admin\PageController;
use Ignite\Core\Http\Controllers\Admin\ParticipantActivityController;
use Ignite\Core\Http\Controllers\Admin\ParticipantController;
use Ignite\Core\Http\Controllers\Admin\ParticipantHistoryController;
use Ignite\Core\Http\Controllers\Admin\ParticipantNotesController;
use Ignite\Core\Http\Controllers\Admin\ParticipantTransactionsController;
use Ignite\Core\Http\Controllers\Admin\PermissionsController;
use Ignite\Core\Http\Controllers\Admin\ReportController;
use Ignite\Core\Http\Controllers\Admin\RunController;
use Ignite\Core\Http\Controllers\Admin\SessionController;
use Ignite\Core\Http\Controllers\Admin\TransactionController;
use Ignite\Core\Http\Controllers\Admin\TransactionResourceController;
use Ignite\Core\Http\Controllers\Admin\TrashedController;
use Ignite\Core\Http\Controllers\Admin\UsersController;

Route::group([
    'prefix' => '/admin',
    'namespace' => 'Admin',
    'middleware' => ['guest']
], function () {
    Route::get('/login', [SessionController::class, 'create'])->name('admin.session.create');
    Route::post('/login', [SessionController::class, 'store'])->name('admin.session.store');
});

// Admin Pages Routes
Route::group([
    'prefix' => '/admin',
    'namespace' => 'Admin',
    'middleware' => ['web', 'auth'],
], function () {
    Route::any('/logout', [SessionController::class, 'destroy'])->name('admin.session.destroy');

    // Dashboard
    Route::get('/', [AdminController::class, 'index'])->name('admin.dashboard.index')->middleware('can:core.dashboard.browse');
    Route::get('/index', [AdminController::class, 'index'])->middleware('can:core.dashboard.browse');

    // Impersonation Routes
    Route::get('/impersonate/{user}', [AdminController::class, 'mask'])->name('admin.impersonate')->middleware('can:core.user.impersonate');

    // Security Admin Routes
    Route::group(['prefix' => '/security'], function () {
        // Users
        Route::any('/users', [UsersController::class, 'index'])->name('admin.security.users.index')->middleware('can:core.security.users.browse');
        Route::get('/users/{user}/edit', [UsersController::class, 'edit'])->name('admin.security.users.edit')->middleware('can:core.security.users.update');
        Route::patch('/users/{user}', [UsersController::class, 'update'])->name('admin.security.users.update')->middleware('can:core.security.users.update');
        Route::get('/users/{user}/activate', [UsersController::class, 'activate'])->name('admin.security.users.activate')->middleware('can:core.security.users.activate');
        Route::get('/users/{user}/deactivate', [UsersController::class, 'deactivate'])->name('admin.security.users.deactivate')->middleware('can:core.security.users.deactivate');

        // Groups
        Route::any('/groups', [GroupsController::class, 'index'])->name('admin.security.groups.index')->middleware('can:core.security.groups.browse');
        Route::get('/groups/create', [GroupsController::class, 'create'])->name('admin.security.groups.create')->middleware('can:core.security.groups.create');
        Route::post('/groups/create', [GroupsController::class, 'store'])->name('admin.security.groups.store')->middleware('can:core.security.groups.create');
        Route::get('/groups/{group}/edit', [GroupsController::class, 'edit'])->name('admin.security.groups.edit')->middleware('can:core.security.groups.update');
        Route::patch('/groups/{group}', [GroupsController::class, 'update'])->name('admin.security.groups.update')->middleware('can:core.security.groups.update');
        Route::delete('/groups/delete', [GroupsController::class, 'destroyMany'])->name('admin.security.groups.destroyMany')->middleware('can:core.security.groups.delete-many');
        Route::delete('/groups/{group}', [GroupsController::class, 'destroy'])->name('admin.security.groups.destroy')->middleware('can:core.security.groups.delete');

        // Permissions
        Route::any('/permissions', [PermissionsController::class, 'index'])->name('admin.security.permissions.index')->middleware('can:core.security.permissions.browse');
        Route::get('/permissions/create', [PermissionsController::class, 'create'])->name('admin.security.permissions.create')->middleware('can:core.security.permissions.create');
        Route::post('/permissions/create', [PermissionsController::class, 'store'])->name('admin.security.permissions.store')->middleware('can:core.security.permissions.create');
        Route::get('/permissions/{permission}/edit', [PermissionsController::class, 'edit'])->name('admin.security.permissions.edit')->middleware('can:core.security.permissions.update');
        Route::patch('/permissions/{permission}', [PermissionsController::class, 'update'])->name('admin.security.permissions.update')->middleware('can:core.security.permissions.update');
        Route::delete('/permissions/delete', [PermissionsController::class, 'destroyMany'])->name('admin.security.permissions.destroyMany')->middleware('can:core.security.permissions.delete-many');
        Route::delete('/permissions/{permission}', [PermissionsController::class, 'destroy'])->name('admin.security.permissions.destroy')->middleware('can:core.security.permissions.delete');
    });

    // Admin Participant Pages Routes
    Route::group(['prefix' => '/participant'], function () {
        // Internal Users
        Route::any('/internal/{status?}', [InternalController::class, 'index'])->name('admin.participant.internal')->middleware('can:core.user.internal.browse');
        Route::any('/internal/{status?}/export', [InternalController::class, 'index'])->name('admin.participant.internal.export')->middleware('can:core.user.internal.browse');

        // Trashed Users
        Route::any('/trashed', [TrashedController::class, 'index'])->name('admin.participant.trashed')->middleware('can:core.user.trashed.browse');
        Route::any('/trashed/export', [TrashedController::class, 'index'])->name('admin.participant.trashed.export')->middleware('can:core.user.trashed.browse');
        Route::put('/trashed/restore', [TrashedController::class, 'store'])->name('admin.participant.restore')->middleware('can:core.user.trashed.restore');
        Route::delete('/trashed/shred', [TrashedController::class, 'destroy'])->name('admin.participant.shred')->middleware('can:core.user.trashed.shred');

        // Participant Users
        Route::any('/participants', [ParticipantController::class, 'index'])->name('admin.participant.index')->middleware('can:core.user.participant.browse');
        Route::any('/participants/export', [ParticipantController::class, 'index'])->name('admin.participant.index.export')->middleware('can:core.user.participant.browse');
        Route::get('/participants/show/{userId}', [ParticipantController::class, 'show'])->name('admin.participant.show')->middleware('can:core.user.participant.browse');
        Route::get('/participants/create', [ParticipantController::class, 'create'])->name('admin.participant.create')->middleware('can:core.user.participant.create');
        Route::post('/participants/store', [ParticipantController::class, 'store'])->name('admin.participant.store')->middleware('can:core.user.participant.create');
        Route::get('/participants/edit/{userId}', [ParticipantController::class, 'edit'])->name('admin.participant.edit')->middleware('can:core.user.participant.update');
        Route::post('/participants/update/{id}', [ParticipantController::class, 'update'])->name('admin.participant.update')->middleware('can:core.user.participant.update');
        Route::get('/participants/delete/{id}', [ParticipantController::class, 'delete'])->name('admin.participant.delete')->middleware('can:core.user.participant.delete');
        Route::delete('/participants/destroy/{id}', [ParticipantController::class, 'destroy'])->name('admin.participant.destroy')->middleware('can:core.user.participant.delete');

        // Tables/Lists
        Route::any('/participants/{participant}/transactions', [ParticipantTransactionsController::class, 'index'])->name('admin.participant.transactions')->middleware('can:core.user.transaction.browse');
        Route::post('/participants/{participant}/transactions/store', [ParticipantTransactionsController::class, 'store'])->name('admin.participant.transactions.store')->middleware('can:core.user.transaction.create');
        Route::any('/participants/{participant}/history', [ParticipantHistoryController::class, 'index'])->name('admin.participant.history')->middleware('can:core.user.history.browse');
        Route::any('/participants/{participant}/activity', [ParticipantActivityController::class, 'index'])->name('admin.participant.activity')->middleware('can:core.user.activity.browse');
        Route::any('/participants/{participant}/notes', [ParticipantNotesController::class, 'index'])->name('admin.participant.notes')->middleware('can:core.user.notes.browse');
        Route::post('/participants/{participant}/notes/create', [ParticipantNotesController::class, 'store'])->name('admin.participant.note.create')->middleware('can:core.user.notes.create');
    });

    // Transaction Admin Routes
    Route::group(['prefix' => '/transactions'], function () {
        Route::any('/', [TransactionController::class, 'index'])->name('admin.transactions.index')->middleware('can:core.transactions.browse');
        Route::any('/export', [TransactionController::class, 'index'])->name('admin.transactions.index.export')->middleware('can:core.transactions.browse');
        Route::get('/create', [TransactionController::class, 'create'])->name('admin.transactions.create')->middleware('can:core.transactions.create');
        Route::post('/create', [TransactionController::class, 'store'])->name('admin.transactions.store')->middleware('can:core.transactions.create');

        Route::any('/waiting', [TransactionResourceController::class, 'index'])->name('admin.transactions.resource.index')->middleware('can:core.transactions.waiting.browse');
        Route::any('/waiting/export', [TransactionResourceController::class, 'index'])->name('admin.transactions.resource.index.export')->middleware('can:core.transactions.waiting.browse');
        Route::get('/waiting/{id}/edit', [TransactionResourceController::class, 'edit'])->name('admin.transactions.resource.edit')->middleware('can:core.transactions.waiting.assign');
        Route::put('/waiting/{id}/update', [TransactionResourceController::class, 'update'])->name('admin.transactions.resource.update')->middleware('can:core.transactions.waiting.assign');
        Route::delete('/waiting/delete', [TransactionResourceController::class, 'destroyMany'])->name('admin.transactions.resource.destroyMany')->middleware('can:core.transactions.waiting.delete');
    });

    // Import Admin Routes
    Route::group(['prefix' => '/import'], function () {
        Route::any('/', [ImportController::class, 'index'])->name('admin.import.index')->middleware('can:core.import.browse');
        Route::post('/store', [ImportController::class, 'store'])->name('admin.import.store')->middleware('can:core.import.create');
        Route::get('/{id}', [ImportController::class, 'show'])->name('admin.import.show')->middleware('can:core.import.view');
        Route::delete('/{id}', [ImportController::class, 'destroy'])->name('admin.import.destroy')->middleware('can:core.import.delete');
        // Routes that interact with the import file.
        Route::post('/run/{id}', [RunController::class, 'store'])->name('admin.import.run')->middleware('can:core.import.run');
        Route::get('/preview/{id}/{offset?}', [RunController::class, 'preview'])->name('admin.import.preview')->middleware('can:core.import.preview');
        Route::get('/download/{id}', [RunController::class, 'download'])->name('admin.import.download')->middleware('can:core.import.download');
        Route::get('/generate/{id}', [RunController::class, 'generate'])->name('admin.import.generate')->middleware('can:core.import.generate');
    });

    // Report Admin Routes
    Route::group(['prefix' => '/reports'], function () {
        Route::any('/', [ReportController::class, 'index'])->name('admin.reports.index')->middleware('can:core.report.browse');
        Route::any('/export', [ReportController::class, 'index'])->name('admin.reports.index.export')->middleware('can:core.report.browse');
        Route::any('/show/{id}', [ReportController::class, 'show'])->name('admin.reports.show')->middleware('can:core.report.browse');
        Route::any('/show/{id}/export', [ReportController::class, 'show'])->name('admin.reports.export')->middleware('can:core.report.export');
        Route::get('/send/{id}', [ReportController::class, 'send'])->name('admin.reports.send')->middleware('can:core.report.send');
        Route::get('/clear/{id}', [ReportController::class, 'clear'])->name('admin.reports.clear')->middleware('can:core.report.browse');
    });

    // Blocks Admin Routes
    Route::group(['prefix' => '/cms/blocks'], function () {
        Route::any('/', [BlockController::class, 'index'])->name('admin.blocks.index')->middleware('can:core.cms.blocks.browse');
        Route::any('/export', [BlockController::class, 'index'])->name('admin.blocks.index.export')->middleware('can:core.cms.blocks.browse');
        Route::get('create', [BlockController::class, 'create'])->name('admin.blocks.create')->middleware('can:core.cms.blocks.create');
        Route::post('store', [BlockController::class, 'store'])->name('admin.blocks.store')->middleware('can:core.cms.blocks.create');
        Route::get('edit/{id}', [BlockController::class, 'edit'])->name('admin.blocks.edit')->middleware('can:core.cms.blocks.update');
        Route::put('update/{id}', [BlockController::class, 'update'])->name('admin.blocks.update')->middleware('can:core.cms.blocks.update');
        Route::delete('delete/{id}', [BlockController::class, 'destroy'])->name('admin.blocks.destroy')->middleware('can:core.cms.blocks.delete');
        Route::delete('delete-many', [BlockController::class, 'destroyMany'])->name('admin.blocks.destroyMany')->middleware('can:core.cms.blocks.delete.many');
    });

    // Pages Admin Routes
    Route::group(['prefix' => '/cms/pages'], function () {
        Route::get('/', [PageController::class, 'index'])->name('admin.pages.index')->middleware('can:core.cms.pages.browse');
        Route::get('create', [PageController::class, 'create'])->name('admin.pages.create')->middleware('can:core.cms.pages.create');
        Route::post('store', [PageController::class, 'store'])->name('admin.pages.store')->middleware('can:core.cms.pages.create');
        Route::get('edit/{id}', [PageController::class, 'edit'])->name('admin.pages.edit')->middleware('can:core.cms.pages.update');
        Route::put('update/{id}', [PageController::class, 'update'])->name('admin.pages.update')->middleware('can:core.cms.pages.update');
    });

    // Media Admin Routes
    Route::group(['prefix' => '/cms/media'], function () {
        Route::get('/', 'MediaController@index')->name('admin.media.index')->middleware('can:core.cms.media.browse');
    });
});

// Blocks Routes: Api
Route::group(
    [
        'middleware' => ['web', 'auth'],
        'namespace'  => 'Api',
        'prefix'     => '/admin/cms/blocks/api'
    ],
    function () {
        Route::get('/', 'BlockController@index')->name('api.blocks.index')->middleware('can:core.cms.blocks.browse');
    }
);

// Data Api Routes
Route::group(
    [
        'middleware' => ['web', 'auth'],
        'namespace'  => 'Api',
        'prefix'     => '/admin/data/api'
    ],
    function () {
        Route::get('participants', 'DataController@participants')
            ->name('admin.data.participants')
            ->middleware('can:core.dashboard.browse');

        Route::get('participant/{user}', 'DataController@participant')
            ->name('admin.data.participant')
            ->middleware('can:core.dashboard.browse');
    }
);

// Pages Api Routes
Route::group(
    [
        'middleware' => ['web', 'auth'],
        'namespace'  => 'Api',
        'prefix'     => '/admin/cms/pages/api'
    ],
    function () {
        Route::get('', 'PageController@index')->name('admin.pages.browse')->middleware('can:core.cms.pages.browse');
        Route::post('reorder', 'PageController@reorder')->name('admin.pages.reorder')->middleware('can:core.cms.pages.reorder');
        Route::post('toggle/{id}', 'PageController@toggle')->name('admin.pages.toggle')->middleware('can:core.cms.pages.status-toggle');
    }
);

// Media Api Routes
Route::group(
    [
        'middleware' => ['web', 'auth'],
        'namespace'  => 'Api',
        'prefix'     => '/admin/cms/media/api'
    ],
    function () {
        Route::get('/', 'MediaController@index')->name('admin.media.api.index')->middleware('can:core.cms.media.browse');
        Route::post('upload', 'MediaController@upload')->name('admin.media.api.upload')->middleware('can:core.cms.media.upload');
        Route::post('/download', 'MediaController@download')->name('admin.media.api.download')->middleware('can:core.cms.media.download');
        Route::post('/delete', 'MediaController@destroy')->name('admin.media.api.destroy')->middleware('can:core.cms.media.delete');
    }
);

// Core Controller Authenticated Routes
Route::group([
    'middleware' => ['web', 'auth']
], function () {
    // Logout Routes
    Route::get('logout', 'Auth\LoginController@logout')->name('logout');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout.post');

    // Transaction Routes
    Route::group(['prefix' => '/transactions'], function () {
        Route::get('/', 'TransactionController@index')->name('participant.transaction.index');
    });

    // Participant Routes
    Route::group(['prefix' => '/participant'], function () {
        // Password
        Route::get('/password', 'PasswordController@edit')->name('participant.password.edit');
        Route::post('/password', 'PasswordController@update')->name('participant.password.update');

        // Profile
        Route::get('/profile', 'ProfileController@show')->name('participant.profile.show');
        Route::get('/edit', 'ProfileController@edit')->name('participant.profile.edit');
        Route::post('/edit', 'ProfileController@update')->name('participant.profile.update');
    });
});

// Core Controller Non-Authenticated Routes
Route::group([
    'middleware' => ['web', 'guest']
], function () {
    // Login Routes
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');

    Route::get('login/{driver}', 'Auth\LoginController@redirectToProvider')->name('sso.redirect');
    Route::get('login/{driver}/callback', 'Auth\LoginController@handleProviderCallback')->name('sso.callback');

    // Unauthenticated Participant Pages Routes
    Route::get('enroll', 'EnrollmentController@create')->name('participant.enroll.create');
    Route::post('enroll', 'EnrollmentController@store')->name('participant.enroll.store');
    Route::get('enroll/accepted', 'EnrollmentAcceptedController@show')->name('participant.enroll.accepted');

    // Password Reset Routes...
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset');
});

Route::group([
    'middleware' => ['web']
], function () {
    // Page Routes
    Route::redirect('/home', '/', 301);
    Route::get('/', 'PageController@index')->name('homepage');
    Route::get('{code}', 'PageController@show')->where('code', '.+')->name('page')->fallback();
});
