<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Izin\IzinAngkutController;
use App\Http\Controllers\Izin\IzinEmisiController;
use App\Http\Controllers\JadwalSidangController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\WebAuthController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\PenugasanController;

use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\PermissionController;
use App\Http\Controllers\PengumumanController;
use App\Http\Controllers\Persetujuan\AddendumController;
use App\Http\Controllers\Persetujuan\AmdalController;
use App\Http\Controllers\Persetujuan\DelhController;
use App\Http\Controllers\Persetujuan\DplhController;
use App\Http\Controllers\Persetujuan\KerangkaController;
use App\Http\Controllers\Persetujuan\RklController;
use App\Http\Controllers\Persetujuan\SpplController;
use App\Http\Controllers\Persetujuan\UklController;
use App\Http\Controllers\PersetujuanTeknisController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\RincianTeknisController;
use App\Http\Controllers\SuratArahanController;
use Illuminate\Support\Facades\Route;

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

Route::get('/', function () {
    return view('/frontend/home');
});

Route::get('/', [HomeController::class, 'index'])->name('home');


Route::get('/news/detail', function () {
    return view('/frontend/news_detail');
});

Route::get('/news', function () {
    return view('/frontend/news');
});

Route::get('/surat/pertek/penerimaan', function () {
    return view('/pertek/subkel/surat_penerimaan');
});

Route::get('/admin/pengguna', function () {
    return view('/admin/pengguna');
});

Route::get('/news', [NewsController::class, 'index'])->name('news.index');
Route::get('/news/detail', [NewsController::class, 'detail'])->name('news.detail');

Route::get('/surat', [SuratArahanController::class, 'show']);
Route::post('/surat/save', [SuratArahanController::class, 'save'])->name('surat.save');
// Route::get('/surat/exportPDF', [SuratArahanController::class, 'exportPDF'])->name('surat.exportPDF');
Route::match(['get', 'post'], '/surat/exportPDF', [SuratArahanController::class, 'exportPDF'])->name('surat.exportPDF');

// Protected Web Routes (Session-based authentication)
Route::middleware(['web.auth'])->group(function () {
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
    Route::get('/dashboard-pertek', [DashboardController::class, 'pertek'])->middleware('permission:dashboard.view.pertek')->name('dashboard.pertek');
    Route::get('/dashboard-rintek', [DashboardController::class, 'rintek'])->middleware('permission:dashboard.view.rintek')->name('dashboard.rintek');
    Route::get('/dashboard-amdal', [DashboardController::class, 'amdal'])->middleware('permission:dashboard.view.amdal')->name('dashboard.amdal');
    Route::get('/dashboard-bengkel', [DashboardController::class, 'bengkel'])->middleware('permission:dashboard.view.uji_emisi')->name('dashboard.bengkel');
});


// Web Authentication (Session-based with cookies)
Route::get('/auth/login', [LoginController::class, 'index'])->name('login.index');
Route::post('/auth/session-login', [WebAuthController::class, 'sessionLogin'])->name('login.session');
Route::post('/auth/logout', [WebAuthController::class, 'sessionLogout'])->name('logout.session');

// Daftar / Registrasi
Route::get('/auth/daftar', [LoginController::class, 'register'])->name('auth.daftar');
Route::get('/daftar', function () {
    return redirect()->route('auth.daftar');
})->name('daftar');

// Signup form submission
Route::post('/auth/daftar', [\App\Http\Controllers\SignupController::class, 'store'])->name('auth.daftar.store');

// User Management & Settings (restricted to settings.manage)
Route::prefix('admin')->middleware(['web.auth', 'permission:settings.manage'])->group(function () {
    // User Management
    Route::get('/users', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('admin.users.index');
    Route::get('/users/create', [\App\Http\Controllers\Admin\UserController::class, 'create'])->name('admin.users.create');
    Route::post('/users', [\App\Http\Controllers\Admin\UserController::class, 'store'])->name('admin.users.store');
    Route::get('/users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'show'])->name('admin.users.show');
    Route::get('/users/{user}/edit', [\App\Http\Controllers\Admin\UserController::class, 'edit'])->name('admin.users.edit');
    Route::put('/users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'update'])->name('admin.users.update');
    Route::delete('/users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'destroy'])->name('admin.users.destroy');

    // Roles & Permissions Management
    Route::get('/roles', [RoleController::class, 'index'])->name('admin.roles.index');
    Route::get('/roles/create', [RoleController::class, 'create'])->name('admin.roles.create');
    Route::post('/roles', [RoleController::class, 'store'])->name('admin.roles.store');
    Route::get('/roles/{role}/edit', [RoleController::class, 'edit'])->name('admin.roles.edit');
    Route::put('/roles/{role}', [RoleController::class, 'update'])->name('admin.roles.update');
    Route::delete('/roles/{role}', [RoleController::class, 'destroy'])->name('admin.roles.destroy');

    Route::get('/permissions', [PermissionController::class, 'index'])->name('admin.permissions.index');
    Route::get('/permissions/create', [PermissionController::class, 'create'])->name('admin.permissions.create');
    Route::post('/permissions', [PermissionController::class, 'store'])->name('admin.permissions.store');
    Route::get('/permissions/{permission}/edit', [PermissionController::class, 'edit'])->name('admin.permissions.edit');
    Route::put('/permissions/{permission}', [PermissionController::class, 'update'])->name('admin.permissions.update');
    Route::delete('/permissions/{permission}', [PermissionController::class, 'destroy'])->name('admin.permissions.destroy');
});

// Pertek
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/pertek/arahan', [PersetujuanTeknisController::class, 'index_arahan'])->middleware('permission:persetujuan_teknis.access')->name('pertek.index_arahan');
    Route::get('/pertek/slo', [PersetujuanTeknisController::class, 'index_slo'])->middleware('permission:persetujuan_teknis.access')->name('pertek.index_slo');
    Route::get('/pertek/detail-slo', [PersetujuanTeknisController::class, 'detail_slo'])->middleware('permission:persetujuan_teknis.access')->name('pertek.detail_slo');
    Route::get('/pertek/create-arahan', [PersetujuanTeknisController::class, 'create_arahan'])->middleware('permission:persetujuan_teknis.access')->name('pertek.create_arahan');
    Route::get('/pertek/verifikator/arahan', [PersetujuanTeknisController::class, 'verifikator_arahan'])->middleware('permission:persetujuan_teknis.access')->name('pertek.verifikator_arahan');
    Route::get('/pertek/user/arahan', [PersetujuanTeknisController::class, 'user_arahan'])->middleware('permission:persetujuan_teknis.access')->name('pertek.user_arahan');
    Route::get('/pertek/emisi', [PersetujuanTeknisController::class, 'index_emisi'])->middleware('permission:persetujuan_teknis.access')->name('pertek.index_emisi');
    Route::get('/pertek/emisi/detail', [PersetujuanTeknisController::class, 'detail_emisi'])->middleware('permission:persetujuan_teknis.access')->name('pertek.detail_emisi');
    Route::get('/pertek/airlimbah', [PersetujuanTeknisController::class, 'index_airlimbah'])->middleware('permission:persetujuan_teknis.access')->name('pertek.index_airlimbah');
    Route::get('/pertek/limbahb3', [PersetujuanTeknisController::class, 'index_limbahb3'])->middleware('permission:persetujuan_teknis.access')->name('pertek.index_limbahb3');
});

//rintek
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/rintek/arahan', [RincianTeknisController::class, 'index_arahan'])->middleware('permission:rincian_teknis.access')->name('rintek.index_arahan');
    Route::get('/rintek/create-arahan', [RincianTeknisController::class, 'create_arahan'])->middleware('permission:rincian_teknis.access')->name('rintek.create_arahan');
    Route::get('/rintek/verifikator/arahan', [RincianTeknisController::class, 'verifikator_arahan'])->middleware('permission:rincian_teknis.access')->name('rintek.verifikator_arahan');
});

//izin angkut
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/izinangkut', [IzinAngkutController::class, 'index_angkut'])->middleware('permission:izin_angkut_olah.access')->name('izinangkut.index_permohonan');
});

//izin emisi
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/izinemisi', [IzinEmisiController::class, 'index_emisi'])->middleware('permission:izin_tempat_uji_emisi.access')->name('izinemisi.index_permohonan');
});

Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/pengumuman', [PengumumanController::class, 'index'])->middleware('permission:pengumuman.access')->name('pengumuman.index');
    Route::get('/pengumuman/create', [PengumumanController::class, 'create'])->middleware('permission:pengumuman.access')->name('pengumuman.create');
    Route::post('/pengumuman', [PengumumanController::class, 'store'])->middleware('permission:pengumuman.access')->name('pengumuman.store');
    Route::post('/pengumuman/calculate-period', [PengumumanController::class, 'calculatePeriod'])->middleware('permission:pengumuman.access')->name('pengumuman.calculate-period');
    Route::get('/pengumuman/get-kecamatan', [PengumumanController::class, 'getKecamatan'])->middleware('permission:pengumuman.access')->name('pengumuman.get-kecamatan');
    Route::get('/pengumuman/get-kelurahan', [PengumumanController::class, 'getKelurahan'])->middleware('permission:pengumuman.access')->name('pengumuman.get-kelurahan');
    Route::get('/pengumuman/{id}', [PengumumanController::class, 'detail'])->middleware('permission:pengumuman.access')->name('pengumuman.detail');
});

//rintek
// Route::prefix('admin')->group(function () {
//     Route::get('/jadwal', [JadwalSidangController::class, 'index'])->name('jadwal.index');
//     Route::get('/jadwal/create', [JadwalSidangController::class, 'create'])->name('jadwal.create');
// });

// Jadwal Sidang
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/jadwal', [JadwalSidangController::class, 'index'])->middleware('permission:penjadwalan.access')->name('jadwal.index');
    Route::match(['get', 'post'], '/jadwal/create', [JadwalSidangController::class, 'create'])->middleware('permission:penjadwalan.access')->name('jadwal.create');
});

// Penugasan
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/penugasan', [PenugasanController::class, 'index'])->name('penugasan.index');
});

// Profile Management
Route::prefix('admin/profile')->middleware(['web.auth'])->group(function () {
    Route::get('/', [ProfileController::class, 'index'])->name('profile.index');
    Route::get('/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/update', [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/change-password', [ProfileController::class, 'changePasswordForm'])->name('profile.change-password');
    Route::put('/change-password', [ProfileController::class, 'updatePassword'])->name('profile.update-password');
    Route::get('/change-email', [ProfileController::class, 'changeEmailForm'])->name('profile.change-email');
    Route::put('/change-email', [ProfileController::class, 'updateEmail'])->name('profile.update-email');
    Route::post('/update-photo', [ProfileController::class, 'updatePhoto'])->name('profile.update-photo');

});

// News Management
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    Route::get('/news', [NewsController::class, 'index_newsvideo'])->name('news.index_newsvideo');
    Route::get('/news/create', [NewsController::class, 'create_newsvideo'])->name('news.create_newsvideo');
});

// Persetujuan Lingkungan Routes
Route::prefix('admin')->middleware(['web.auth'])->group(function () {
    // Kerangka Acuan
    Route::get('/kerangka', [KerangkaController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.kerangka.index');
    Route::get('/kerangka/create', [KerangkaController::class, 'create'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.kerangka.create');

    // AMDAL
    Route::get('/amdal', [AmdalController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.amdal.index');
    Route::get('/amdal/detail', [AmdalController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.amdal.detail');

    // UKL-UPL
    Route::get('/ukl', [UklController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.ukl.index');
    Route::get('/ukl/detail', [UklController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.ukl.detail');

    // RKL-RPL
    Route::get('/rkl', [RklController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.rkl.index');
    Route::get('/rkl/detail', [RklController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.rkl.detail');

    // SPPL
    Route::get('/sppl', [SpplController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.sppl.index');
    Route::get('/sppl/detail', [SpplController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.sppl.detail');

    // Addendum
    Route::get('/addendum', [AddendumController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.addendum.index');
    Route::get('/addendum/detail', [AddendumController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.addendum.detail');

    // DELH
    Route::get('/delh', [DelhController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.delh.index');
    Route::get('/delh/detail', [DelhController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.delh.detail');

    // DPLH
    Route::get('/dplh', [DplhController::class, 'index'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.dplh.index');
    Route::get('/dplh/detail', [DplhController::class, 'detail'])->middleware('permission:persetujuan_lingkungan.access')->name('persetujuan.dplh.detail');
});

// API Routes for Frontend Layanan
Route::prefix('api/layanan')->group(function () {
    Route::get('/data', [App\Http\Controllers\LayananController::class, 'getLayananData'])->name('api.layanan.data');
    Route::get('/wilayah', [App\Http\Controllers\LayananController::class, 'getWilayahData'])->name('api.layanan.wilayah');
    Route::get('/detail/{id}', [App\Http\Controllers\LayananController::class, 'getDetailKegiatan'])->name('layanan.detail');
    Route::get('/filter', [App\Http\Controllers\LayananController::class, 'getFilteredKegiatan'])->name('api.layanan.filter');
});
