<?php

use App\Http\Controllers\Publik\PendataanController;
use App\Http\Controllers\Pegawai\DashboardController as PegawaiDashboardController;
use App\Http\Controllers\Pegawai\AssessmentKompetensiController as PegawaiAssessmentKompetensiController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\PegawaiController;
use App\Http\Controllers\Admin\PegawaiAccountController;
use App\Http\Controllers\Admin\AssessmentKompetensiController as AdminAssessmentKompetensiController;
use App\Http\Controllers\Admin\KodeAksesKantorController;
use App\Http\Controllers\Admin\SuperAdmin\AdminUserController;
use App\Http\Controllers\Admin\SuperAdmin\ValidationHistoryController;
use App\Http\Controllers\Admin\SuperAdmin\AssessmentMasterController;
use App\Http\Controllers\Admin\SuperAdmin\UnitKerjaController;
use App\Http\Controllers\Admin\SuperAdmin\JabatanFungsionalController;
use Illuminate\Support\Facades\Route;

use App\Models\Pegawai;
use App\Models\JabatanFungsional;
use App\Models\UnitKerja;

// Landing page
Route::get('/', function () {
    $stats = [
        'pegawai' => Pegawai::count(),
        'jabatan' => JabatanFungsional::count(),
        'unit_kerja' => UnitKerja::count(),
    ];
    return view('public.landing', compact('stats'));
})->name('home');

// ===== PUBLIC ROUTES (Tanpa Login) =====
Route::get('/pegawai-files/{file}', [PendataanController::class, 'showFile'])
    ->middleware('signed')
    ->name('pegawai.files.show');

Route::prefix('pendataan')->group(function () {
    Route::get('/', [PendataanController::class, 'show'])->name('pegawai.form');
    Route::post('/akses', [PendataanController::class, 'verifyOfficeCode'])->name('pegawai.access.verify');
    Route::post('/ganti-kode', [PendataanController::class, 'clearOfficeCode'])->name('pegawai.access.clear');
    Route::post('/', [PendataanController::class, 'store'])->name('pegawai.store');
    Route::get('/{pegawai}/perbaiki', [PendataanController::class, 'editRejected'])
        ->middleware('signed')
        ->name('pegawai.correction.edit');
    Route::post('/{pegawai}/perbaiki', [PendataanController::class, 'updateRejected'])
        ->middleware('signed')
        ->name('pegawai.correction.update');
    Route::get('/{pegawai}/success', [PendataanController::class, 'success'])->name('pegawai.success');
});

// ===== AUTH ROUTES (Built-in Laravel Authentication) =====
Route::middleware('guest')->group(function () {
    Route::get('/login', [\App\Http\Controllers\Auth\AuthenticatedSessionController::class, 'create'])
        ->name('login');
    Route::post('/login', [\App\Http\Controllers\Auth\AuthenticatedSessionController::class, 'store']);
});

Route::middleware('auth')->group(function () {
    Route::post('/logout', [\App\Http\Controllers\Auth\AuthenticatedSessionController::class, 'destroy'])
        ->name('logout');
});

Route::middleware(['auth', 'pegawai'])->group(function () {
    Route::get('/pegawai/ubah-password', [PegawaiDashboardController::class, 'editPassword'])
        ->name('pegawai.password.edit');
    Route::post('/pegawai/ubah-password', [PegawaiDashboardController::class, 'updatePassword'])
        ->name('pegawai.password.update');
    Route::get('/pegawai/dashboard', [PegawaiDashboardController::class, 'index'])
        ->name('pegawai.dashboard');
    Route::get('/pegawai/assessment-kompetensi', [PegawaiAssessmentKompetensiController::class, 'form'])
        ->name('pegawai.assessment.form');
    Route::post('/pegawai/assessment-kompetensi', [PegawaiAssessmentKompetensiController::class, 'store'])
        ->name('pegawai.assessment.store');
    Route::get('/pegawai/assessment-kompetensi/hasil', [PegawaiAssessmentKompetensiController::class, 'hasil'])
        ->name('pegawai.assessment.hasil');
    Route::get('/pegawai/assessment-kompetensi/rekomendasi', [PegawaiAssessmentKompetensiController::class, 'rekomendasi'])
        ->name('pegawai.assessment.rekomendasi');
    Route::post('/pegawai/assessment-kompetensi/rekomendasi', [PegawaiAssessmentKompetensiController::class, 'storeRekomendasi'])
        ->name('pegawai.assessment.rekomendasi.store');
});

// ===== ADMIN ROUTES (Login Required + Admin Middleware) =====
Route::middleware(['auth', 'admin'])->group(function () {
    // Dashboard
    Route::get('/admin/dashboard', [AdminDashboardController::class, 'index'])->name('admin.dashboard');
    Route::get('/admin/dashboard/pensiun', [AdminDashboardController::class, 'detailPensiun'])->name('admin.dashboard.pensiun');
    Route::get('/admin/dashboard/pensiun/sudah', [AdminDashboardController::class, 'sudahPensiun'])->name('admin.dashboard.pensiun.sudah');
    Route::get('/admin/dashboard/belum-assessment', [AdminDashboardController::class, 'belumAssessment'])->name('admin.dashboard.belum-assessment');
    Route::get('/admin/dashboard/heatmap-kompetensi', [AdminDashboardController::class, 'heatmapKompetensiDetail'])->name('admin.dashboard.heatmap-kompetensi');

    // Pegawai Management
    Route::get('admin/pendataan-masuk', [PegawaiController::class, 'submissions'])
        ->name('admin.pegawai.submissions');

    Route::resource('admin/pegawai', PegawaiController::class, [
        'names' => [
            'index' => 'admin.pegawai.index',
            'show' => 'admin.pegawai.show',
            'create' => 'admin.pegawai.create',
            'store' => 'admin.pegawai.store',
            'edit' => 'admin.pegawai.edit',
            'update' => 'admin.pegawai.update',
        ],
    ])->only(['index', 'show', 'create', 'store', 'edit', 'update']);

    // Pegawai Validation
    Route::post('admin/pegawai/{pegawai}/validation', [PegawaiController::class, 'updateValidation'])
        ->name('admin.pegawai.validation');

    Route::get('admin/akun-pegawai', [PegawaiAccountController::class, 'index'])
        ->name('admin.pegawai-accounts.index');
    Route::post('admin/akun-pegawai/{pegawai}', [PegawaiAccountController::class, 'store'])
        ->name('admin.pegawai-accounts.store');
    Route::post('admin/akun-pegawai/{pegawai}/reset-password', [PegawaiAccountController::class, 'resetPassword'])
        ->name('admin.pegawai-accounts.reset-password');

    Route::get('admin/assessment-kompetensi', [AdminAssessmentKompetensiController::class, 'index'])
        ->name('admin.assessment-kompetensi.index');
    Route::get('admin/assessment-kompetensi/export', [AdminAssessmentKompetensiController::class, 'exportExcel'])
        ->name('admin.assessment-kompetensi.export');
    Route::get('admin/assessment-kompetensi/rekap', [AdminAssessmentKompetensiController::class, 'recap'])
        ->name('admin.assessment-kompetensi.recap');
    Route::get('admin/assessment-kompetensi/rekap/export', [AdminAssessmentKompetensiController::class, 'exportRekapExcel'])
        ->name('admin.assessment-kompetensi.recap.export');
    Route::get('admin/assessment-kompetensi/{pegawai}', [AdminAssessmentKompetensiController::class, 'show'])
        ->name('admin.assessment-kompetensi.show');
    Route::post('admin/assessment-kompetensi/{pegawai}/pengembangan', [AdminAssessmentKompetensiController::class, 'updatePengembangan'])
        ->name('admin.assessment-kompetensi.pengembangan');
    Route::post('admin/assessment-kompetensi/{pegawai}/verify', [AdminAssessmentKompetensiController::class, 'verify'])
        ->name('admin.assessment-kompetensi.verify');

    Route::get('admin/kode-akses-kantor', [KodeAksesKantorController::class, 'index'])
        ->name('admin.kode-akses-kantor.index');
    Route::post('admin/kode-akses-kantor', [KodeAksesKantorController::class, 'store'])
        ->name('admin.kode-akses-kantor.store');
    Route::patch('admin/kode-akses-kantor/{kodeAksesKantor}/toggle', [KodeAksesKantorController::class, 'toggle'])
        ->name('admin.kode-akses-kantor.toggle');
    Route::delete('admin/kode-akses-kantor/{kodeAksesKantor}', [KodeAksesKantorController::class, 'destroy'])
        ->name('admin.kode-akses-kantor.destroy');

    Route::middleware('super_admin')->group(function () {
        // Admin Account Management
        Route::resource('admin/admin-users', AdminUserController::class, [
            'parameters' => [
                'admin-users' => 'adminUser',
            ],
            'names' => [
                'index' => 'admin.admin-users.index',
                'create' => 'admin.admin-users.create',
                'store' => 'admin.admin-users.store',
                'edit' => 'admin.admin-users.edit',
                'update' => 'admin.admin-users.update',
                'destroy' => 'admin.admin-users.destroy',
            ],
        ])->except(['show']);

        Route::get('admin/validation-histories', [ValidationHistoryController::class, 'index'])
            ->name('admin.validation-histories.index');

        Route::get('admin/master-assessment-kompetensi', [AssessmentMasterController::class, 'index'])
            ->name('admin.assessment-master.index');
        Route::post('admin/master-assessment-kompetensi/kompetensi', [AssessmentMasterController::class, 'storeKompetensi'])
            ->name('admin.assessment-master.kompetensi.store');
        Route::post('admin/master-assessment-kompetensi/kompetensi/{kompetensi}/deskripsi-jenjang', [AssessmentMasterController::class, 'updateDeskripsiJenjang'])
            ->name('admin.assessment-master.kompetensi.update-deskripsi-jenjang');
        Route::get('admin/master-assessment-kompetensi/kompetensi/{kompetensi}/indikator', [AssessmentMasterController::class, 'manageIndikator'])
            ->name('admin.assessment-master.indikator.manage');
        Route::post('admin/master-assessment-kompetensi/kompetensi/{kompetensi}/indikator', [AssessmentMasterController::class, 'storeManageIndikator'])
            ->name('admin.assessment-master.indikator.store-manage');
        Route::post('admin/master-assessment-kompetensi/standar', [AssessmentMasterController::class, 'storeStandard'])
            ->name('admin.assessment-master.standar.store');
        Route::post('admin/master-assessment-kompetensi/rekomendasi', [AssessmentMasterController::class, 'storeRekomendasi'])
            ->name('admin.assessment-master.rekomendasi.store');
        Route::put('admin/master-assessment-kompetensi/rekomendasi/{rekomendasi}', [AssessmentMasterController::class, 'updateRekomendasi'])
            ->name('admin.assessment-master.rekomendasi.update');
        Route::patch('admin/master-assessment-kompetensi/rekomendasi/{rekomendasi}/toggle', [AssessmentMasterController::class, 'toggleRekomendasi'])
            ->name('admin.assessment-master.rekomendasi.toggle');
        Route::delete('admin/master-assessment-kompetensi/rekomendasi/{rekomendasi}', [AssessmentMasterController::class, 'destroyRekomendasi'])
            ->name('admin.assessment-master.rekomendasi.destroy');


        // Unit Kerja Management
        Route::resource('admin/unit-kerja', UnitKerjaController::class, [
            'names' => [
                'index' => 'admin.unit-kerja.index',
                'create' => 'admin.unit-kerja.create',
                'store' => 'admin.unit-kerja.store',
                'edit' => 'admin.unit-kerja.edit',
                'update' => 'admin.unit-kerja.update',
                'destroy' => 'admin.unit-kerja.destroy',
            ],
        ])->except(['show']);

        // Jabatan Fungsional Management
        Route::get('admin/jabatan/detail/{namaJabatan}', [JabatanFungsionalController::class, 'detail'])
            ->name('admin.jabatan.detail');

        Route::resource('admin/jabatan', JabatanFungsionalController::class, [
            'names' => [
                'index' => 'admin.jabatan.index',
                'create' => 'admin.jabatan.create',
                'store' => 'admin.jabatan.store',
                'edit' => 'admin.jabatan.edit',
                'update' => 'admin.jabatan.update',
                'destroy' => 'admin.jabatan.destroy',
            ],
        ])->except(['show']);
    });
});
