<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\DataKKController;
use App\Http\Controllers\MustahikController;
use App\Http\Controllers\DistribusiController;
use App\Http\Controllers\WilayahController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\RekapZakatController;
use App\Http\Controllers\MustahikHistoryController;
use App\Http\Controllers\PublikController;
use App\Http\Controllers\AmilDashboardController;
use App\Http\Controllers\RtController;
use App\Models\PengajuanMasjid;
use Illuminate\Support\Facades\Route;

// ==========================================
// 1. JALUR PUBLIK (SIAPA PUN BISA AKSES)
// ==========================================
Route::get('/', [PublikController::class, 'index'])->name('home');
Route::get('/masjid/{id}', [PublikController::class, 'detailMasjid'])->name('masjid.detail');
Route::get('/masjid/{id}/cek-zakat', [PublikController::class, 'cekZakat'])->name('masjid.cek_zakat');
Route::get('/kuitansi/{id}', [PublikController::class, 'cetakKuitansi'])->name('kuitansi');

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

// Rute Pengajuan Pendaftaran Masjid Baru (Tanpa Login)
Route::get('/pengajuan', function () {
    return view('form_pendaftaran');
});
Route::post('/pengajuan/store', [AuthController::class, 'storePengajuan']);

// Proxy Geocoding (server-side, bebas CORS)
Route::get('/api/geocode', [PublikController::class, 'geocode']);



// ==========================================
// 2. JALUR KHUSUS: HARUS LOGIN TERLEBIH DAHULU
// ==========================================
Route::middleware(['auth', 'no.back'])->group(function () {

    // KELOMPOK KAMAR: ROLE ADMIN MASJID & SUPERADMIN
    Route::middleware(['role:admin_masjid,superadmin'])->group(function () {
        Route::get('/admin/dashboard', [DataKKController::class, 'dashboard']);
        
        // Menu Kelola Data Warga / KK (DataKKController)
        Route::get('/admin/masyarakat', [DataKKController::class, 'index']);
        Route::get('/admin/masyarakat/create', [DataKKController::class, 'create']);
        Route::post('/admin/masyarakat/store', [DataKKController::class, 'store']);

        // Menu Kelola Wilayah RT/RW (WilayahController)
        Route::get('/admin/wilayah', [WilayahController::class, 'index']);
        Route::get('/admin/wilayah/create', [WilayahController::class, 'create']);
        Route::post('/admin/wilayah/store', [WilayahController::class, 'store']);
        Route::get('/admin/wilayah/edit/{id}', [WilayahController::class, 'edit']);
        Route::post('/admin/wilayah/update/{id}', [WilayahController::class, 'update']);
        Route::post('/admin/wilayah/destroy/{id}', [WilayahController::class, 'destroy']);

        // Menu Kelola User (UserController)
        Route::get('/admin/user', [UserController::class, 'index']);
        Route::get('/admin/user/create', [UserController::class, 'create']);
        Route::post('/admin/user/store', [UserController::class, 'store']);
        Route::get('/admin/user/edit/{id}', [UserController::class, 'edit']);
        Route::post('/admin/user/update/{id}', [UserController::class, 'update']);
        Route::post('/admin/user/toggle/{id}', [UserController::class, 'toggle']);
        Route::post('/admin/user/destroy/{id}', [UserController::class, 'destroy']);

        // Menu Laporan Mustahik (LaporanController)
        Route::get('/admin/laporan', [LaporanController::class, 'index']);
        Route::get('/admin/laporan/export-excel', [LaporanController::class, 'exportExcel']);
        Route::get('/admin/laporan/export-pdf', [LaporanController::class, 'exportPdf']);

        // Menu Rekap Zakat (RekapZakatController)
        Route::get('/admin/rekap-zakat', [RekapZakatController::class, 'index']);
    });

    // KELOMPOK KAMAR: ROLE KETUA RT
    Route::middleware(['role:rt,superadmin'])->group(function () {
        Route::get('/rt/dashboard', [RtController::class, 'dashboard']);
        Route::get('/rt/data-warga', [RtController::class, 'dataWarga']);
        Route::get('/rt/status-pengajuan', [RtController::class, 'statusPengajuan']);
        
        // Simpan Data dari Modal
        Route::post('/rt/kk/store', [RtController::class, 'storeKk']);
        Route::post('/rt/kk/update/{id}', [RtController::class, 'updateKk']);
        Route::post('/rt/mustahik/store', [RtController::class, 'storeMustahik']);
        Route::post('/rt/mustahik/update/{id}', [RtController::class, 'updateMustahik']);
        
        // Endpoint cadangan jika form benar-benar disubmit dari halaman lain
        Route::get('/rt/input-mustahik', [MustahikController::class, 'createSurvey']);
        Route::post('/rt/input-mustahik', [MustahikController::class, 'storeSurvey']);
    });

    // KELOMPOK KAMAR: ROLE AMIL (BENDAHARA ZAKAT)
    Route::middleware(['role:amil,superadmin'])->group(function () {
        Route::get('/amil/dashboard', [AmilDashboardController::class, 'index']);
        
        // Menu Transaksi Penerimaan & Distribusi Zakat
        Route::get('/amil/penerimaan', [DistribusiController::class, 'indexPenerimaan']);
        Route::post('/amil/penerimaan', [DistribusiController::class, 'storePenerimaan']);
        
        Route::get('/amil/distribusi', [DistribusiController::class, 'indexDistribusi']);
        Route::post('/amil/distribusi', [DistribusiController::class, 'storeDistribusi']);
        
        // Menu Verifikasi Data Mustahik dari RT (Sesuai Use Case Baru)
        Route::get('/amil/verifikasi', [MustahikController::class, 'indexVerifikasi']);
        Route::post('/amil/approve/{id}', [MustahikController::class, 'approve']);

        // Menu Evaluasi Mustahik Tahunan
        Route::get('/amil/update-tahunan', [MustahikHistoryController::class, 'index']);
        Route::post('/amil/update-tahunan', [MustahikHistoryController::class, 'storeBulk']);
    });

    // KELOMPOK KAMAR: ROLE SUPERADMIN (Panel Verifikasi Masjid)
    Route::middleware(['role:superadmin'])->group(function () {
        Route::get('/superadmin/dashboard', [App\Http\Controllers\SuperAdminController::class, 'dashboard']);
        Route::get('/superadmin/kelola-pengajuan', [App\Http\Controllers\SuperAdminController::class, 'kelolaPengajuan']);
        Route::get('/superadmin/kelola-masjid', [App\Http\Controllers\SuperAdminController::class, 'kelolaMasjid']);
        Route::post('/superadmin/masjid/store', [App\Http\Controllers\SuperAdminController::class, 'storeMasjid']);
        Route::get('/superadmin/masjid/data/{id}', [App\Http\Controllers\SuperAdminController::class, 'editMasjid'])->name('superadmin.masjid.data');
        Route::post('/superadmin/masjid/update/{id}', [App\Http\Controllers\SuperAdminController::class, 'updateMasjid'])->name('superadmin.masjid.update');

        Route::post('/superadmin/masjid/approve/{id}', [AuthController::class, 'approveMasjid']);
        Route::post('/superadmin/masjid/reject/{id}', [AuthController::class, 'rejectMasjid']);
    });

});