<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

// === CONTROLLER UNTUK USER (AUDITOR) ===
use App\Http\Controllers\User\WebUserLoginController;
use App\Http\Controllers\User\DashboardController; 
use App\Http\Controllers\User\AuditController;
use App\Http\Controllers\User\NotifikasiController;
use App\Http\Controllers\User\LaporanController;
use App\Http\Controllers\User\ProfileController;


// === CONTROLLER UNTUK ADMIN ===
use App\Http\Controllers\Admin\AdminLoginController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\JadwalController;
use App\Http\Controllers\Admin\AuditController as AdminAuditController;
use App\Http\Controllers\Admin\TemaController as AdminTemaController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Admin\LaporanController as AdminLaporanController;
use App\Http\Controllers\Admin\AuditExportController;
use App\Http\Controllers\Admin\NotifikasiController as AdminNotifikasiController;

/*
|--------------------------------------------------------------------------
| ROUTES UNTUK USER (AUDITOR)
|--------------------------------------------------------------------------
*/

Route::redirect('/', '/login');

// === AUTH USER ===
Route::middleware('guest')->group(function () {
    Route::get('/login', [WebUserLoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [WebUserLoginController::class, 'login'])->name('login.post');

    Route::get('/register', [WebUserLoginController::class, 'showRegisterForm'])->name('register');
    Route::post('/register', [WebUserLoginController::class, 'register'])->name('register.post');
});

// === LOGOUT USER ===
Route::post('/logout', [WebUserLoginController::class, 'logout'])->middleware('auth')->name('logout');

// === FITUR UNTUK USER YANG TELAH LOGIN ===
Route::middleware('auth')->group(function () {

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


    // PROFILE
    Route::prefix('profile')->name('profile.')->group(function () {
        Route::get('/', [ProfileController::class, 'edit'])->name('edit');
        Route::patch('/', [ProfileController::class, 'update'])->name('update');
        Route::delete('/', [ProfileController::class, 'destroy'])->name('destroy');
        Route::patch('/foto', [ProfileController::class, 'updatePhoto'])->name('update.photo');
        Route::patch('/password', [ProfileController::class, 'updatePassword'])->name('update.password');
    });

    // AUDIT
    Route::get('/tambah-audit', [AuditController::class, 'create'])->name('audit.create');
    Route::post('/tambah-audit', [AuditController::class, 'store'])->name('audit.store');
    Route::get('/audit/{id}', [AuditController::class, 'show'])->name('audit.show');
    Route::patch('/audit/{id}/update-sesudah', [AuditController::class, 'updateSesudah'])->name('audit.updateSesudah');

    // DAFTAR VERIFIKASI (sudah diverifikasi, menunggu close admin)
    Route::get('/verifikasi-audit', [AuditController::class, 'verifikasiAudit'])->name('audit.verifikasi');

    // RIWAYAT
    Route::get('/riwayat-audit', [AuditController::class, 'riwayat'])->name('audit.riwayat');

    // HASIL AUDIT & VERIFIKASI
    Route::get('/hasil-audit', [AuditController::class, 'hasilAudit'])->name('audit.hasil');
    Route::get('/hasil-audit/{id}', [AuditController::class, 'hasilAuditShow'])->name('audit.hasil.show');
    Route::get('/hasil-verifikasi', [AuditController::class, 'hasilVerifikasi'])->name('audit.hasil.verifikasi');

    // FORM & SUBMIT VERIFIKASI
    Route::get('/audit/{id}/verifikasi', [AuditController::class, 'formVerifikasi'])->name('audit.verifikasi.form');
    Route::put('/audit/{id}/verifikasi', [AuditController::class, 'submitVerifikasi'])->name('audit.verifikasi.submit');

    // EXPORT PDF UNTUK USER
    Route::get('/export/pdf/{id}', [AuditController::class, 'exportPDF'])->name('audit.export.pdf');



    // NOTIFIKASI
    Route::prefix('notifikasi')->name('notifikasi.')->group(function () {
        Route::get('/', [NotifikasiController::class, 'index'])->name('index');
        Route::get('/{id}', [NotifikasiController::class, 'show'])->name('show');
        Route::get('/baca/{id}', [NotifikasiController::class, 'baca'])->name('baca');
        Route::post('/baca-semua', [NotifikasiController::class, 'bacaSemua'])->name('baca.semua');
        Route::delete('/hapus/{id}', [NotifikasiController::class, 'hapus'])->name('hapus');
        Route::delete('/hapus-semua', [NotifikasiController::class, 'hapusSemua'])->name('hapus.semua');
    });

    // VERIFIKASI ULANG EMAIL
    Route::post('/email/verification-notification', function (Request $request) {
        $request->user()->sendEmailVerificationNotification();
        return back()->with('status', 'Link verifikasi dikirim!');
    })->name('verification.send');
});

/*
|--------------------------------------------------------------------------
| ROUTES UNTUK ADMIN
|--------------------------------------------------------------------------
*/

Route::prefix('admin')->name('admin.')->group(function () {

    // === AUTH ADMIN ===
    Route::middleware('guest:admin')->group(function () {
        Route::get('/login', [AdminLoginController::class, 'showLoginForm'])->name('login');
        Route::post('/login', [AdminLoginController::class, 'login'])->name('login.post');
    });

    // === FITUR ADMIN SETELAH LOGIN ===
    Route::middleware('auth:admin')->group(function () {

        // LOGOUT
        Route::post('/logout', [AdminLoginController::class, 'logout'])->name('logout');

        // DASHBOARD
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
        
        // MANAJEMEN JADWAL
        Route::get('/jadwal', [JadwalController::class, 'index'])->name('jadwal.index');
        Route::post('/jadwal/audit', [JadwalController::class, 'storeAudit'])->name('jadwal.audit.store');
        Route::post('/jadwal/verifikasi', [JadwalController::class, 'storeVerifikasi'])->name('jadwal.verifikasi.store');
         Route::post('/jadwal/{id}/toggle', [JadwalController::class, 'toggleStatus'])->name('jadwal.toggle');
          Route::put('/jadwal/{id}', [JadwalController::class, 'update'])->name('jadwal.update');
          Route::delete('/jadwal/{id}', [JadwalController::class, 'destroy'])->name('jadwal.destroy');

        // MANAJEMEN AUDIT
        Route::prefix('audit')->group(function () {
            Route::get('/', [AdminAuditController::class, 'index'])->name('audit');

            // RUTE KHUSUS (didefinisikan lebih dulu supaya tidak dianggap sebagai {id})
            Route::get('/verifikasi', [AdminAuditController::class, 'verifikasiClose'])->name('audit.verifikasi');
            Route::get('/verifikasi/{id}', [AdminAuditController::class, 'verifikasiDetail'])->name('audit.verifikasi.detail');
            Route::post('/verifikasi/{id}', [AdminAuditController::class, 'tentukanVerifikasi'])->name('audit.tentukanVerifikasi');
            Route::patch('/reset-verifikasi/{id}', [AdminAuditController::class, 'resetVerifikasi'])->name('audit.resetVerifikasi');
            Route::post('/set-all-verifikasi', [AdminAuditController::class, 'setAllVerifikasi'])->name('audit.setAllVerifikasi');

            Route::get('/audit-riwayat', [AdminAuditController::class, 'riwayatSemua'])->name('audit.riwayat');
            Route::get('/riwayat/{id}', [AdminAuditController::class, 'riwayatDetail'])->name('audit.riwayat.detail');
            Route::post('/set-all-audit', [AdminAuditController::class, 'setAllAudit'])->name('audit.setAllAudit');

            // RUTE BIASA DENGAN PARAMETER DITARUH PALING BAWAH
            Route::put('/{id}/update', [AdminAuditController::class, 'update'])->name('audit.update');
            Route::get('/{id}', [AdminAuditController::class, 'show'])->name('audit.detail');
            Route::get('/{id}/edit', [AdminAuditController::class, 'edit'])->name('audit.edit');
            Route::delete('/{id}', [AdminAuditController::class, 'destroy'])->name('audit.destroy');
            Route::post('/{id}/selesaikan', [AdminAuditController::class, 'selesaikanAudit'])->name('audit.selesai');

            // EXPORT PDF
            Route::get('/export/pdf/{id}', [AuditExportController::class, 'exportPDF'])->name('audit.export.pdf');
        });

        // MANAJEMEN TEMA
        Route::prefix('tema')->name('tema.')->group(function () {
            Route::get('/', [AdminTemaController::class, 'index'])->name('index');
            Route::get('/create', [AdminTemaController::class, 'create'])->name('create');
            Route::post('/', [AdminTemaController::class, 'store'])->name('store');
            Route::get('/{id}/edit', [AdminTemaController::class, 'edit'])->name('edit');
            Route::put('/{id}', [AdminTemaController::class, 'update'])->name('update');
            Route::delete('/{id}', [AdminTemaController::class, 'destroy'])->name('destroy');
            Route::patch('/toggle/{id}', [AdminTemaController::class, 'toggle'])->name('toggle');
        });

        // MANAJEMEN NOTIFIKASI
        Route::prefix('notifikasi')->name('notifikasi.')->group(function () {
            Route::get('/', [AdminNotifikasiController::class, 'index'])->name('index');
            Route::get('/lihat/{id}', [AdminNotifikasiController::class, 'lihat'])->name('lihat');
            Route::get('/{id}', [AdminNotifikasiController::class, 'show'])->name('show');
            Route::post('/kirim', [AdminNotifikasiController::class, 'kirim'])->name('kirim');
        });

        // MANAJEMEN USER
        Route::prefix('user')->name('user.')->group(function () {
            Route::get('/', [AdminUserController::class, 'index'])->name('index');
            Route::get('/create', [AdminUserController::class, 'create'])->name('create');
            Route::post('/store', [AdminUserController::class, 'store'])->name('store');
            Route::get('/{id}/edit', [AdminUserController::class, 'edit'])->name('edit');
            Route::put('/{id}', [AdminUserController::class, 'update'])->name('update');
            Route::delete('/{id}', [AdminUserController::class, 'destroy'])->name('destroy');
        });

        
            // LAPORAN AUDIT
            Route::get('/laporan', [AdminLaporanController::class, 'index'])->name('laporan');
            Route::get('/laporan/export/pdf', [AdminLaporanController::class, 'exportPdf'])->name('laporan.export.pdf');
            Route::get('/laporan/export/excel', [AdminLaporanController::class, 'exportExcel'])->name('laporan.export.excel');
        });
});
