<?php

use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\BackupController;
use App\Http\Controllers\Admin\CertificateTemplateController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ParticipantController;
use App\Http\Controllers\Admin\ParticipantImportController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Public\CertificateDownloadController;
use App\Http\Controllers\Public\CertificateSearchController;
use Illuminate\Support\Facades\Route;

Route::redirect('/', '/e-sijil');

Route::get('/e-sijil', [CertificateSearchController::class, 'index'])
    ->name('certificate.index');

Route::post('/e-sijil/search', [CertificateSearchController::class, 'search'])
    ->middleware('throttle:certificate-search')
    ->name('certificate.search');

Route::post('/e-sijil/download', [CertificateDownloadController::class, 'download'])
    ->middleware('throttle:certificate-download')
    ->name('certificate.download');

Route::get('/e-sijil/reset', [CertificateSearchController::class, 'reset'])
    ->name('certificate.reset');

Route::prefix('admin')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('admin.login');
    Route::post('/login', [LoginController::class, 'login'])->name('admin.login.submit');

    Route::middleware(['auth', 'active'])->group(function () {
        Route::post('/logout', [LoginController::class, 'logout'])->name('admin.logout');

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

        Route::resource('/participants', ParticipantController::class)->names('admin.participants');
        Route::post('/participants/bulk-delete', [ParticipantController::class, 'bulkDelete'])
            ->name('admin.participants.bulk-delete');
        Route::post('/participants/{participant}/restore', [ParticipantController::class, 'restore'])
            ->name('admin.participants.restore');
        Route::get('/participants/{participant}/download', [ParticipantController::class, 'download'])
            ->name('admin.participants.download');
        Route::post('/participants/{participant}/reset-download', [ParticipantController::class, 'resetDownload'])
            ->name('admin.participants.reset-download');

        Route::get('/participants-import', [ParticipantImportController::class, 'index'])
            ->name('admin.participants.import.index');
        Route::post('/participants-import', [ParticipantImportController::class, 'store'])
            ->name('admin.participants.import.store');
        Route::get('/participants-import/template', [ParticipantImportController::class, 'template'])
            ->name('admin.participants.import.template');
        Route::get('/participants-import/{batch}', [ParticipantImportController::class, 'show'])
            ->name('admin.participants.import.show');
        Route::get('/participants-import/{batch}/errors', [ParticipantImportController::class, 'downloadErrors'])
            ->name('admin.participants.import.errors');

        Route::get('/reports', [ReportController::class, 'index'])
            ->name('admin.reports.index');
        Route::get('/reports/export/excel', [ReportController::class, 'exportExcel'])
            ->name('admin.reports.export.excel');
        Route::get('/reports/export/pdf', [ReportController::class, 'exportPdf'])
            ->name('admin.reports.export.pdf');

        Route::resource('/certificate-templates', CertificateTemplateController::class)->names('admin.certificate-templates');
        Route::get('/certificate-templates/{certificate_template}/preview', [CertificateTemplateController::class, 'preview'])
            ->name('admin.certificate-templates.preview');
        Route::post('/certificate-templates/{certificate_template}/activate', [CertificateTemplateController::class, 'activate'])
            ->name('admin.certificate-templates.activate');

        Route::middleware('super_admin')->group(function () {
            Route::resource('/users', UserController::class)->names('admin.users');
            Route::get('/settings', [SettingController::class, 'index'])->name('admin.settings.index');
            Route::put('/settings', [SettingController::class, 'update'])->name('admin.settings.update');
            Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('admin.activity-logs.index');
            Route::get('/backup', [BackupController::class, 'download'])->name('admin.backup.download');
            Route::post('/maintenance/on', [BackupController::class, 'maintenanceOn'])->name('admin.maintenance.on');
            Route::post('/maintenance/off', [BackupController::class, 'maintenanceOff'])->name('admin.maintenance.off');
        });
    });
});
