<?php


use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\DashboardController;
use App\Http\Controllers\Backend\JournalController;
use App\Http\Controllers\Backend\AboutJournalController;
use App\Http\Controllers\Backend\EditorialCategoryController;
use App\Http\Controllers\Backend\EditorialBoardController;
use App\Http\Controllers\Backend\PolicyController;
use App\Http\Controllers\Backend\ForAuthorController;
use App\Http\Controllers\Backend\ReviewerController;
use App\Http\Controllers\Backend\ContactController;
use App\Http\Controllers\Backend\JournalInfoController;
use App\Http\Controllers\Backend\PermissionController;
use App\Http\Controllers\Frontend\FrontendController;
use App\Http\Controllers\Backend\RoleController;
use App\Http\Controllers\Backend\UserController;

// Public Frontend Routes
Route::controller(FrontendController::class)->group(function () {
    Route::get('/', 'index')->name('home');
    Route::get('/about', 'about')->name('about');
    Route::get('/editorial-board', 'editorialBoard')->name('editorial_board');
    Route::get('/for-authors/{id?}', 'forAuthors')->name('for_authors.show');
    Route::get('/policies/{id?}', 'policies')->name('policies.show');
    Route::get('/for-reviewers/{id?}', 'forReviewers')->name('for_reviewers.show');
    Route::get('/journal-info/{id?}', 'journalInfo')->name('journal_info.show');
    Route::get('/contact', 'contact')->name('contact');
});

Route::get('/admin/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('admin.dashboard');


Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {

    Route::controller(JournalController::class)->group(function () {
        Route::get('/journals', 'index')->middleware('permission:view Journal')->name('journals.index');
        Route::get('/journals/fetch', 'fetch')->middleware('permission:view Journal')->name('journals.fetch'); // Yajra AJAX Route
        Route::post('/journals', 'store')->middleware('permission:create Journal')->name('journals.store');
        Route::get('/journals/{id}/edit', 'edit')->middleware('permission:edit Journal')->name('journals.edit');
        Route::post('/journals/{id}', 'update')->middleware('permission:edit Journal')->name('journals.update');
        Route::delete('/journals/{id}', 'destroy')->middleware('permission:delete Journal')->name('journals.destroy');
    });
    Route::controller(AboutJournalController::class)->group(function () {
        Route::get('/about-journal', 'index')->middleware('permission:view About Journal')->name('about.index');
        Route::get('/about-journal/fetch', 'fetch')->middleware('permission:view About Journal')->name('about.fetch');
        Route::post('/about-journal', 'store')->middleware('permission:create About Journal')->name('about.store');
        Route::get('/about-journal/{id}/edit', 'edit')->middleware('permission:edit About Journal')->name('about.edit');
        Route::post('/about-journal/{id}', 'update')->middleware('permission:edit About Journal')->name('about.update');
        Route::delete('/about-journal/{id}', 'destroy')->middleware('permission:delete About Journal')->name('about.destroy');
    });


    Route::controller(EditorialCategoryController::class)
        ->prefix('editorial-categories')
        ->name('editorial_categories.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Editor Category')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Editor Category')->name('fetch');
            Route::post('/', 'store')->middleware('permission:create Editor Category')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Editor Category')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Editor Category')->name('destroy');
        });

    // Editorial Board
    Route::controller(EditorialBoardController::class)
        ->prefix('editorial-board')
        ->name('editorial_board.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Editorial Board')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Editorial Board')->name('fetch');
            Route::post('/', 'store')->middleware('permission:create Editorial Board')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Editorial Board')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Editorial Board')->name('destroy');
        });
    Route::controller(PolicyController::class)
        ->prefix('policies')
        ->name('policies.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Policy')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Policy')->name('fetch');
            Route::post('/', 'store')->middleware('permission:create Policy')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Policy')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Policy')->name('destroy');
        });


    // For Authors Routes
    Route::controller(ForAuthorController::class)
        ->prefix('for-authors')
        ->name('for_authors.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view For Author')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view For Author')->name('fetch');
            Route::post('/', 'store')->middleware('permission:create For Author')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit For Author')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete For Author')->name('destroy');
        });
    // Reviewer (For Reviewers) CRUD Routes
    Route::controller(ReviewerController::class)
        ->prefix('reviewers')
        ->name('reviewers.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Reviewer')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Reviewer')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create Reviewer')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Reviewer')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Reviewer')->name('destroy');
        });

    Route::controller(ContactController::class)
        ->prefix('contacts')
        ->name('contacts.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Contact')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Contact')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create Contact')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Contact')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Contact')->name('destroy');
        });
    Route::controller(JournalInfoController::class)
        ->prefix('journal-infos')
        ->name('journal-infos.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Journal Info')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Journal Info')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create Journal Info')->name('store');
            Route::get('/{id}/edit', 'edit')->middleware('permission:edit Journal Info')->name('edit');
            Route::delete('/{id}', 'destroy')->middleware('permission:delete Journal Info')->name('destroy');
        });
    Route::controller(PermissionController::class)
        ->prefix('permissions')
        ->name('permissions.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Permission')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Permission')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create Permission')->name('store');
            Route::get('/edit/{id}', 'edit')->middleware('permission:edit Permission')->name('edit');
            Route::delete('/destroy/{id}', 'destroy')->middleware('permission:delete Permission')->name('destroy');

            // Excel Import & Sample Routes
            Route::post('/import', 'import')->middleware('permission:create Permission')->name('import');
            Route::get('/sample-download', 'downloadSample')->middleware('permission:view Permission')->name('sample');
        });
    Route::controller(RoleController::class)
        ->prefix('roles')
        ->name('roles.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view Role')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view Role')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create Role')->name('store');
            Route::get('/edit/{id}', 'edit')->middleware('permission:edit Role')->name('edit');
            Route::delete('/destroy/{id}', 'destroy')->middleware('permission:delete Role')->name('destroy');
        });
    Route::controller(UserController::class)
        ->prefix('users')
        ->name('users.')
        ->group(function () {
            Route::get('/', 'index')->middleware('permission:view User')->name('index');
            Route::get('/fetch', 'fetch')->middleware('permission:view User')->name('fetch');
            Route::post('/store', 'store')->middleware('permission:create User')->name('store');
            Route::get('/edit/{id}', 'edit')->middleware('permission:edit User')->name('edit');
            Route::delete('/destroy/{id}', 'destroy')->middleware('permission:delete User')->name('destroy');
            Route::post('/profile/update', 'updateProfile')->name('profile.update');
        });
});





Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

// routes/web.php ফাইলের একদম নিচে এটি যুক্ত করুন

Route::fallback(function () {
    return response()->view('errors.404', [], 404);
});

require __DIR__ . '/auth.php';
