<?php

use App\Http\Controllers\PublicController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ExportController;
use App\Http\Controllers\Admin\AnswerOptionController;
use App\Http\Controllers\Admin\QuestionController;
use App\Http\Controllers\Admin\OPDController;
use App\Http\Controllers\Admin\SurveiAdminController;
use App\Http\Controllers\Admin\InformasiPublikAdminController;
use App\Http\Controllers\Admin\PermohonanInformasiAdminController;
use App\Http\Controllers\Admin\BankSoalIkkAdminController;
use App\Http\Controllers\SurveiController;
use App\Http\Controllers\PpidDashboardController;
use App\Http\Controllers\InformasiPublikController;
use App\Http\Controllers\PermohonanInformasiController;
use App\Http\Controllers\StatistikPpidController;
use App\Http\Controllers\SikkBankSoalController;
use App\Http\Controllers\PelaporanIkkController;
use App\Http\Controllers\LembarJawabIkkController;
use Illuminate\Support\Facades\Route;

Route::get('/', [SurveiController::class, 'form'])->name('survei.form');
Route::get('/survey/{opd}', [PublicController::class, 'survey'])->name('public.survey');
Route::post('/survey/{opd}/submit', [PublicController::class, 'submit'])->name('public.submit');
Route::get('/thankyou', [PublicController::class, 'thankyou'])->name('public.thankyou');

// Survei Routes
Route::prefix('survei')->group(function () {
    Route::get('pertanyaan', [SurveiController::class, 'form'])->name('survei.form');
    Route::post('submit', [SurveiController::class, 'submit'])->name('survei.submit');
    Route::get('selesai', [SurveiController::class, 'selesai'])->name('survei.selesai');
    Route::get('hasil', [SurveiController::class, 'hasil'])->name('survei.hasil');
    Route::get('data-responden', [SurveiController::class, 'dataResponden'])->name('survei.data-responden');
});

// PPID Routes
Route::prefix('ppid')->name('ppid.')->group(function () {
    Route::get('/', [PpidDashboardController::class, 'index'])->name('dashboard');
    Route::get('informasi-publik', [InformasiPublikController::class, 'index'])->name('informasi.index');
    Route::get('permohonan', [PermohonanInformasiController::class, 'create'])->name('permohonan.create');
    Route::post('permohonan/submit', [PermohonanInformasiController::class, 'store'])->name('permohonan.store');
    Route::get('statistik', [StatistikPpidController::class, 'index'])->name('statistik');
    Route::get('survei', function() { return redirect()->route('survei.form'); })->name('survei');
});

// SIKK Bank Soal Routes
Route::prefix('sikk')->name('sikk.')->group(function () {
    Route::get('bank-soal', [SikkBankSoalController::class, 'index'])->name('bank-soal.index');
    Route::get('bank-soal/create', [SikkBankSoalController::class, 'create'])->name('bank-soal.create');
    Route::post('bank-soal/store', [SikkBankSoalController::class, 'store'])->name('bank-soal.store');
    Route::get('bank-soal/{id}', [SikkBankSoalController::class, 'show'])->name('bank-soal.show');
    Route::get('bank-soal/{id}/edit', [SikkBankSoalController::class, 'edit'])->name('bank-soal.edit');
    Route::put('bank-soal/{id}', [SikkBankSoalController::class, 'update'])->name('bank-soal.update');
});

// Pelaporan IKK Routes
Route::prefix('ikk/pelaporan')->name('ikk.pelaporan.')->group(function () {
    Route::get('/', [PelaporanIkkController::class, 'index'])->name('index');
    Route::get('create', [PelaporanIkkController::class, 'create'])->name('create');
    Route::post('store', [PelaporanIkkController::class, 'store'])->name('store');
    Route::get('{id}/edit', [PelaporanIkkController::class, 'edit'])->name('edit');
    Route::put('{id}', [PelaporanIkkController::class, 'update'])->name('update');
    Route::get('{id}/jawaban', [PelaporanIkkController::class, 'jawaban'])->name('jawaban');
    Route::post('{id}/jawaban/submit', [PelaporanIkkController::class, 'submitJawaban'])->name('jawaban.submit');
});

// Lembar Jawab IKK Routes
Route::prefix('ikk/lembar-jawab')->name('ikk.lembar-jawab.')->group(function () {
    Route::get('/', [LembarJawabIkkController::class, 'index'])->name('index');
    Route::get('grafik', [LembarJawabIkkController::class, 'grafik'])->name('grafik');
    Route::get('create', [LembarJawabIkkController::class, 'create'])->name('create');
    Route::post('store', [LembarJawabIkkController::class, 'store'])->name('store');
    Route::get('{id}', [LembarJawabIkkController::class, 'show'])->name('show');
    Route::get('{id}/edit', [LembarJawabIkkController::class, 'edit'])->name('edit');
    Route::put('{id}', [LembarJawabIkkController::class, 'update'])->name('update');
});


Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/opd/{opd}/survei-results', [DashboardController::class, 'opdResults'])->name('opd.survei.results');
    Route::get('/opd/{opd}/export/excel', [ExportController::class, 'exportExcel'])->name('opd.export.excel');
    Route::get('/opd/{opd}/export/pdf', [ExportController::class, 'exportPdf'])->name('opd.export.pdf');

    // OPD Management (Kelola OPD)
    Route::get('/opd/{id}/results', [OPDController::class, 'results'])->name('opd.results');
    Route::resource('opds', OPDController::class);
    
    // Question Configuration (Konfigurasi Soal)
    Route::resource('questions', QuestionController::class);
    
    // Answer Options Management (Legacy - kept for backward compatibility)
    Route::resource('answer-options', AnswerOptionController::class);

    // Survei Admin Management
    Route::prefix('survei')->name('survei.')->group(function () {
        Route::get('/', [SurveiAdminController::class, 'index'])->name('index');
        Route::get('/{id}', [SurveiAdminController::class, 'show'])->name('show');
        Route::delete('/{id}', [SurveiAdminController::class, 'destroy'])->name('destroy');
        Route::get('/laporan/statistik', [SurveiAdminController::class, 'laporan'])->name('laporan');
    });

    // PPID Admin Management
    Route::prefix('ppid')->name('ppid.')->group(function () {
        // Informasi Publik Management
        Route::prefix('informasi')->name('informasi.')->group(function () {
            Route::get('/', [InformasiPublikAdminController::class, 'index'])->name('index');
            Route::get('/create', [InformasiPublikAdminController::class, 'create'])->name('create');
            Route::post('/', [InformasiPublikAdminController::class, 'store'])->name('store');
            Route::get('/{id}/edit', [InformasiPublikAdminController::class, 'edit'])->name('edit');
            Route::put('/{id}', [InformasiPublikAdminController::class, 'update'])->name('update');
            Route::delete('/{id}', [InformasiPublikAdminController::class, 'destroy'])->name('destroy');
        });

        // Permohonan Informasi Management
        Route::prefix('permohonan')->name('permohonan.')->group(function () {
            Route::get('/', [PermohonanInformasiAdminController::class, 'index'])->name('index');
            Route::get('/{id}', [PermohonanInformasiAdminController::class, 'show'])->name('show');
            Route::post('/{id}/update-status', [PermohonanInformasiAdminController::class, 'updateStatus'])->name('update-status');
            Route::delete('/{id}', [PermohonanInformasiAdminController::class, 'destroy'])->name('destroy');
        });
    });

    // IKK Admin Management
    Route::prefix('ikk')->name('ikk.')->group(function () {
        // Bank Soal Management
        Route::prefix('bank-soal')->name('bank-soal.')->group(function () {
            Route::get('/', [BankSoalIkkAdminController::class, 'index'])->name('index');
            Route::get('/create', [BankSoalIkkAdminController::class, 'create'])->name('create');
            Route::post('/', [BankSoalIkkAdminController::class, 'store'])->name('store');
            Route::get('/{id}/edit', [BankSoalIkkAdminController::class, 'edit'])->name('edit');
            Route::put('/{id}', [BankSoalIkkAdminController::class, 'update'])->name('update');
            Route::delete('/{id}', [BankSoalIkkAdminController::class, 'destroy'])->name('destroy');
        });
    });

    // User Management (Admin only)
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\UserManagementController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\UserManagementController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\UserManagementController::class, 'store'])->name('store');
        Route::get('/{id}/edit', [App\Http\Controllers\Admin\UserManagementController::class, 'edit'])->name('edit');
        Route::put('/{id}', [App\Http\Controllers\Admin\UserManagementController::class, 'update'])->name('update');
        Route::delete('/{id}', [App\Http\Controllers\Admin\UserManagementController::class, 'destroy'])->name('destroy');
    });
});

// IKK Routes (Outside admin group - accessible by OPD, Verifikator, and Admin)
Route::middleware('auth')->prefix('ikk')->name('ikk.')->group(function () {
    // Verifikasi (Admin & Verifikator only)
    Route::prefix('verifikasi')->name('verifikasi.')->group(function () {
        Route::get('/', [App\Http\Controllers\VerifikasiIkkController::class, 'index'])->name('index');
        Route::get('/{id}', [App\Http\Controllers\VerifikasiIkkController::class, 'show'])->name('show');
        Route::post('/{id}/verify', [App\Http\Controllers\VerifikasiIkkController::class, 'verify'])->name('verify');
    });
});

