<?php

use App\Http\Controllers\EntryController;
use App\Http\Controllers\ModCP\LanguageController;
use App\Http\Controllers\ReviewController;
use App\Http\Controllers\WebhookController;
use Illuminate\Routing\RedirectController;
use Illuminate\Support\Facades\Route;

// HomeController.
Route::get('/', [ \App\Http\Controllers\HomeController::class, 'index' ] )->name('home');

// EntryController.
Route::name('entries.')->controller(EntryController::class)->group(function () {

    Route::get('/database', 'index' )->name('index');
    Route::get('/{section}', 'section_redirect' )->name('section_redirect')
        ->where(['section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts']);

    Route::get('/{section}/{entry:slug}', 'show' )->name('show')->where(
        [
            'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts',
            'entry' => '[^/]+',
        ]
    );

    Route::get('/my-drafts', 'drafts' )->middleware('xf.auth')->name('drafts');

});
Route::name('reviews.')->controller(ReviewController::class)->group(function () {
    Route::get('/reviews', 'index')->name('index');
    Route::post('/reviews/{entry:id}', 'store')->name('store')->middleware(['xf.auth','can:create,\App\Models\EntryReview']);
});

// SubmissionController.
Route::name('submit.')->prefix('/submit')->controller(\App\Http\Controllers\SubmissionController::class)->middleware(['submissions.maintenance','xf.auth', 'can:create,\App\Models\Entry'])->group(function () {
    Route::get('/', 'index')->name('index');
    Route::get('/{section}', 'create' )->name('create')
        ->where([ 'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts' ]);
    Route::post('/{section}', 'store' )->name('store')
        ->where([ 'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts' ]);
});
Route::name('submit.')->prefix('/edit')->controller(\App\Http\Controllers\SubmissionController::class)->middleware(['submissions.maintenance','xf.auth', 'can:update,entry'])->group(function () {
    Route::get('/{section}/{entry:id}', 'edit' )->name('edit')
        ->where([ 'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts', 'entry' => '[0-9\-]+' ]);
    Route::post('/{section}/{entry:id}', 'update' )->name('update')
        ->where([ 'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts', 'entry' => '[0-9\-]+' ]);
    Route::delete('/{section}/{entry:id}', 'destroy' )->name('destroy')
        ->where([ 'section' => 'translations|romhacks|homebrew|utilities|documents|lua-scripts', 'entry' => '[0-9\-]+' ]);
});

// NewsController
Route::name('news.')->controller(\App\Http\Controllers\NewsController::class)->group(function () {
    Route::get('/news/', 'index' )->name('index');
    Route::get('/news/{news:slug}', 'show' )->name('show')->where([            'news' => '[a-zA-Z0-9\-_]+']);

    Route::get('/submit/news', 'create' )->name('create')->middleware(['submissions.maintenance','xf.auth','can:create,\App\Models\News']);
    Route::post('/submit/news', 'store' )->name('store')->middleware(['submissions.maintenance','xf.auth','can:create,\App\Models\News']);

    Route::get('/edit/news/{news:id}', 'edit')->name('edit')->where(['news' => '[0-9\-]+'])->middleware(['submissions.maintenance','xf.auth','can:update,news']);
    Route::post('/edit/news/{news:id}', 'update')->name('update')->where(['news' => '[0-9\-]+'])->middleware(['submissions.maintenance','xf.auth','can:update,news']);
    Route::delete('/edit/news/{news:id}', 'destroy')->name('destroy')->where(['news' => '[0-9\-]+'])->middleware(['submissions.maintenance','xf.auth','can:update,news']);
});

// QueueController
Route::name('queue.')->prefix('/queue')->controller(\App\Http\Controllers\QueueController::class)->group(function () {
    Route::get('/', 'index' )->name('index');

    Route::patch('/{entry:id}/comment', 'updateComment' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:updateComment,entry' ] )
        ->where([ 'entry' => '[0-9\-]+' ])
        ->name('comment');

    Route::patch('/{entry:id}/approve', 'approve' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:approve,entry' ] )
        ->where([ 'entry' => '[0-9\-]+' ])
        ->name('approve');

    Route::patch('/{entry:id}/reject', 'reject' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:reject,entry' ] )
        ->where([ 'entry' => '[0-9\-]+' ])
        ->name('reject');

    Route::patch('/news/{news:id}/comment', 'updateComment_news' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:updateComment,news' ] )
        ->where([ 'news' => '[0-9\-]+' ])
        ->name('news.comment');

    Route::patch('/news/{news:id}/approve', 'approve_news' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:approve,news' ] )
        ->where([ 'news' => '[0-9\-]+' ])
        ->name('news.approve');

    Route::patch('/news/{news:id}/reject', 'reject_news' )
        ->middleware(['submissions.maintenance','xf.auth', 'can:reject,news' ] )
        ->where([ 'news' => '[0-9\-]+' ])
        ->name('news.reject');
});

// ToolsController
Route::name('tools.')->controller(\App\Http\Controllers\ToolsController::class)->group(function () {
    Route::get('/patch', 'patcher' )->name('patcher');
    Route::get( '/patch/{entry_id}/{file:file_uuid}', 'directPatch' )->name('direct-patch')
        ->where(['entry_id' => '[0-9]+']);
    Route::get('/play/{entry_id}/{file:file_uuid}', 'play' )->name('play')->middleware('xf.auth')
        ->where(['entry_id' => '[0-9]+']);
    Route::get('/hash', 'hasher' )->name('hash');
});

// ModeratorCPController
Route::name('modcp.')->prefix('/modcp')->controller(\App\Http\Controllers\ModCPController::class)->middleware(['xf.auth','can:is-mod'])->group(function () {

    Route::get('/', 'index' )->name('index');
    Route::get('/locked-entries', 'locked' )->name('locked');
    Route::get('/draft-entries', 'draft' )->name('draft')->middleware('can:is-admin');
    Route::get('/hidden-entries', 'hidden' )->name('hidden')->middleware('can:is-admin');
    Route::get('/deleted-entries', 'deleted' )->name('deleted')->middleware('can:is-admin');
    Route::get('/logs', 'logs' )->name('logs')->middleware('can:is-admin');

    Route::patch('/restore/{entry}', 'restore' )->name('restore')->where(['entry' => '[0-9\-]+'])->withTrashed()->middleware('can:is-admin');
    Route::delete('/purge/{entry}', 'destroy' )->name('destroy')->where(['entry' => '[0-9\-]+'])->withTrashed()->middleware('can:is-admin');

    Route::resource('games', \App\Http\Controllers\ModCP\GameController::class)->only(['index', 'store','update','destroy']);
    Route::resource('languages', LanguageController::class)->only(['index', 'store','update','destroy']);
    Route::resource('authors', \App\Http\Controllers\ModCP\AuthorController::class)->only(['index', 'store','update','destroy']);
    Route::resource('platforms', \App\Http\Controllers\ModCP\PlatformController::class )->middleware('can:is-admin')->only(['index', 'store','update','destroy']);
    Route::resource('genres', \App\Http\Controllers\ModCP\GenreController::class )->middleware('can:is-admin')->only(['index', 'store','update','destroy']);
});

// RedirectController
Route::name('redirect.')->controller(\App\Http\Controllers\RedirectController::class)->group(function () {
    Route::get('/entry/report_redirect', 'entryReportRedirect' )->name('entry_report');
    Route::get('/news/report_redirect', 'newsReportRedirect' )->name('news_report');
});

// ShortLinkController
Route::domain('rhpz.org')->name('shorturl.')->controller(\App\Http\Controllers\ShortLinkController::class)->group(function () {
    Route::get('/{id}', 'legacy' )->where('id', '[0-9]+')->name('legacy');
    Route::get('/{letter}/{id}', 'redirect' )->where(['letter' => '[a-z]', 'id' => '[0-9]+'])->name('redirect');
});
