<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\Controllers\AuthController;
use App\Http\Controllers\Controllers\LaporanController;
use App\Http\Controllers\Controllers\DashboardController;
use App\Http\Controllers\Controllers\NewsAdminController;
use App\Http\Controllers\Controllers\PengajuanController;
use App\Http\Controllers\Controllers\PerangkatKontroller;
use App\Http\Controllers\Controllers\AttendanceController;
use App\Http\Controllers\Controllers\LandingPageController;

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

// Auth Routes

Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/forgot-password', [AuthController::class, 'showForgetPasswordForm'])->name('forget.password');
Route::post('/forgot-password', [AuthController::class, 'forgotPassword'])
    ->name('password.email');
Route::get('/reset-password/{token}', [AuthController::class, 'showResetPasswordForm'])->name('reset.password');
Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update');


// News routes
Route::get('/news', [NewsController::class, 'index'])->name('news.index');
Route::get('/news/{slug}', [NewsController::class, 'show'])->name('news.show');
Route::get('/api/news/categories', [NewsController::class, 'getCategories'])->name('news.categories');

// Admin routes
/*
|--------------------------------------------------------------------------
| Protected routes (harus login)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'role:admin'])->group(function () {


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

    // Setting Absensi
    Route::get('/admin/attendance/setting', [AttendanceController::class, 'show'])
        ->name('admin.attendance.setting.show');
    Route::post('/admin/attendance/setting', [AttendanceController::class, 'store'])
        ->name('admin.attendance.setting.store');
    Route::put('/admin/attendance/{attendance}', [AttendanceController::class, 'updateAttendance'])
        ->name('admin.attendance.update');



    // Daftar pengajuan
    Route::get('/admin/pengajuan', [PengajuanController::class, 'showPengajuan'])->name('admin.pengajuan');

    // Detail pengajuan
    Route::get('/admin/pengajuan/{id}', [PengajuanController::class, 'detailPengajuan'])->name('admin.pengajuan.detail');

    // Update status pengajuan
    Route::post('/admin/pengajuan/{id}/status', [PengajuanController::class, 'updateStatus'])
        ->name('admin.pengajuan.updateStatus');

    // Daftar perangkat
    Route::get('/admin/perangkat', [PerangkatKontroller::class, 'index'])->name('admin.perangkat');
    Route::post('/admin/perangkat', [PerangkatKontroller::class, 'store'])
        ->name('admin.perangkat.store');
    // Tambah perangkat
    Route::get('/admin/perangkat/add', [PerangkatKontroller::class, 'create'])->name('admin.perangkat.add');

    // Edit perangkat
    Route::get('/admin/perangkat/{id}/edit', [PerangkatKontroller::class, 'edit'])->name('admin.perangkat.edit');
    Route::put('/admin/perangkat/{id}', [PerangkatKontroller::class, 'update'])->name('admin.perangkat.update');

    // Laporan
    Route::get('/admin/laporan', [LaporanController::class, 'showLaporan'])->name('admin.laporan');
    Route::get('/admin/laporan/export', [LaporanController::class, 'export'])->name('admin.laporan.export');
    // detail per user
    Route::get('/admin/laporan/{user}/detail', [LaporanController::class, 'showDetail'])
        ->name('admin.laporan.detail');

    // (opsional) export detail PDF per user
    Route::get('/admin/laporan/{user}/detail/export', [LaporanController::class, 'exportDetail'])
        ->name('admin.laporan.detail.export');



    // Admin News routes
    Route::get('/admin/berita', [NewsAdminController::class, 'index'])->name('admin.berita.index');
    Route::get('/admin/berita/create', [NewsAdminController::class, 'create'])->name('admin.berita.create');
    Route::post('/admin/berita', [NewsAdminController::class, 'store'])->name('admin.berita.store');
    Route::get('/admin/berita/{id}/edit', [NewsAdminController::class, 'edit'])->name('admin.berita.edit');
    Route::put('/admin/berita/{id}', [NewsAdminController::class, 'update'])->name('admin.berita.update');
    Route::delete('/admin/berita/{id}', [NewsAdminController::class, 'destroy'])->name('admin.berita.destroy');

    Route::get('/logout', [AuthController::class, 'logout'])->name('logout');
});

Route::get('/attendance/sessions/{sessionId}', [AttendanceController::class, 'showPage'])
    ->name('attendance.session.show');
Route::get('/admin/attendance/sessions/{sessionId}/qrcode', [AttendanceController::class, 'currentToken'])
    ->name('admin.attendance.session.qrcode');
Route::get('/admin/attendance/sessions/{sessionId}/members', [AttendanceController::class, 'members'])
    ->name('admin.attendance.session.members');
Route::post('/admin/attendance/sessions/{sessionId}/rotate', [AttendanceController::class, 'forceRotate'])
    ->name('admin.attendance.session.rotate');
