<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DataPelangganController;
use App\Http\Controllers\DataPetugasController;
use App\Http\Controllers\PencatatanController;
use App\Http\Controllers\TagihanController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\NotificationController;
use App\Http\Middleware\AdminMiddleware;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\AdminProfileController;

// --- LOGIN & GUEST ROUTES ---
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
});

// --- AUTH ROUTES (Harus Login Baru Bisa Akses) ---
Route::middleware('auth')->group(function () {
    
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // Dashboard (Admin & Petugas)
    Route::get('/', [DashboardController::class, 'index'])->name('index');

    // Profile (Admin & Petugas)
    Route::get('/profile/edit', [AdminProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [AdminProfileController::class, 'update'])->name('profile.update');

    // Pencatatan (Admin & Petugas)
    Route::prefix('admin/pencatatan')->group(function () {
        Route::get('/', [PencatatanController::class, 'index'])->name('pencatatan.index');
        Route::get('/{id}', [PencatatanController::class, 'show'])->name('pencatatan.show');
        Route::put('/{id}', [PencatatanController::class, 'update'])->name('pencatatan.update');
    });

    // Tagihan (Admin & Petugas)
    Route::prefix('admin/tagihan')->group(function () {
        Route::get('/', [TagihanController::class, 'index'])->name('tagihan.index');
        Route::get('/{id}', [TagihanController::class, 'show'])->name('tagihan.show');
    });

    // Berita CRUD (Admin & Petugas)
    Route::prefix('admin/news')->group(function () {
        Route::get('/', [NewsController::class, 'index'])->name('news.index');
        Route::get('/create', [NewsController::class, 'create'])->name('news.create');
        Route::post('/store', [NewsController::class, 'store'])->name('news.store');
        Route::get('/{id}', [NewsController::class, 'show'])->name('news.show');
        Route::get('/{id}/edit', [NewsController::class, 'edit'])->name('news.edit');
        Route::put('/update/{id}', [NewsController::class, 'update'])->name('news.update');    
        Route::delete('/delete/{id}', [NewsController::class, 'destroy'])->name('news.destroy');
    });

    Route::post('/pencatatan/toggle-akses', [PencatatanController::class, 'toggleStatus'])->name('settings.toggle');
    Route::prefix('petugas')->group(function () {
    Route::get('/pemasangan', [DataPetugasController::class, 'pemasangan'])->name('DataPetugas.pemasangan');
    Route::post('/pasang/{user}', [DataPetugasController::class, 'pasangMeteran'])->name('DataPetugas.pasang');
});
    // ========================================================
    // --- KHUSUS ADMIN SAJA ---
    // ========================================================
    Route::middleware([AdminMiddleware::class])->group(function () {
        
        // Data Pelanggan
        Route::prefix('admin/pelanggan')->group(function () {
            Route::get('/', [DataPelangganController::class, 'index'])->name('DataPelanggan');
            Route::get('/{id}', [DataPelangganController::class, 'show'])->name('DataPelanggan.show');
            Route::put('/{id}', [DataPelangganController::class, 'update'])->name('DataPelanggan.update');
        });

        // Verifikasi Pelanggan
        Route::prefix('admin/verifikasi')->group(function () {
        Route::get('/', [DataPelangganController::class, 'verifikasi'])->name('verifikasi');
        Route::get('/{id}', [DataPelangganController::class, 'showVerifikasi'])->name('verifikasi.show');
        Route::post('/{id}/approve', [DataPelangganController::class, 'approve'])->name('verifikasi.approve');
        Route::post('/{id}/tolak', [DataPelangganController::class, 'tolak'])->name('verifikasi.tolak');
        });

        // Data Petugas
        Route::prefix('admin/petugas')->group(function () { 
            Route::get('/', [DataPetugasController::class, 'index'])->name('DataPetugas');
            Route::get('/create', [DataPetugasController::class, 'create'])->name('DataPetugas.create');
            Route::post('/store', [DataPetugasController::class, 'store'])->name('DataPetugas.store');
            Route::get('/{id}', [DataPetugasController::class, 'show'])->name('DataPetugas.show');
            Route::put('/{id}', [DataPetugasController::class, 'update'])->name('DataPetugas.update');
        });
        Route::prefix('admin/meteran')->group(function () {
        Route::get('/', [DataPetugasController::class, 'stokMeteran'])->name('DataPetugas.stok');
        Route::post('/store', [DataPetugasController::class, 'storeMeteran'])->name('DataPetugas.stok.store');
        Route::delete('/destroy/{meteran}', [DataPetugasController::class, 'destroyMeteran'])->name('DataPetugas.stok.destroy');
        });

        Route::get('/admin/monitoring-pemasangan', [DataPelangganController::class, 'monitoringPemasangan'])->name('monitoring.pemasangan');
        Route::post('/admin/setting-harga', [PencatatanController::class, 'updateHarga'])->name('admin.setting-harga.update');

        // Notifikasi WA Blast
        Route::get('/admin/notifikasi', [NotificationController::class, 'index'])->name('notifikasi.index');
        Route::post('/admin/notifikasi/send', [NotificationController::class, 'send'])->name('notifikasi.send');

        // Laporan
        Route::prefix('admin/laporan')->group(function () {
            Route::get('/', [LaporanController::class, 'index'])->name('laporan.index');
            Route::get('/export-pdf', [LaporanController::class, 'exportPDF'])->name('laporan.pdf');
            Route::get('/export-csv', [LaporanController::class, 'exportCSV'])->name('laporan.csv');
            Route::get('/export-excel', [LaporanController::class, 'exportExcel'])->name('laporan.excel');
        });

    });

});