<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LaporController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\PenyuluhController;
use App\Http\Controllers\PetaniController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\KontakController;

// ================= AUTH =================
Route::get('/login', [AuthController::class, 'loginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);

Route::get('/register', [AuthController::class, 'registerForm']);
Route::post('/register', [AuthController::class, 'register']);

Route::get('/forgot-password', [AuthController::class, 'forgotForm']);
Route::post('/reset-password', [AuthController::class, 'resetPassword']);

Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth');

Route::get('/lapor/{id}/edit', [LaporController::class, 'edit']);
Route::post('/lapor/update/{id}', [LaporController::class, 'update']);


// ================= PUBLIC =================
Route::get('/', fn() => view('landing'))->name('home');
Route::get('/pelaporan', fn() => view('pelaporan'));
Route::get('/kontak', fn() => view('contact'));
Route::post('/kontak', [KontakController::class, 'store']);


// ================= PROTEKSI (LOGIN) =================
Route::middleware('auth')->group(function () {

    // ================= LAPOR (RESTFUL) =================
    Route::resource('lapor', LaporController::class);

    // TAMBAHAN AKSI (KHUSUS PENYULUH)
    Route::post('/lapor/{id}/validasi', [LaporController::class, 'validasi'])->middleware('role:penyuluh');
    Route::post('/lapor/{id}/proses', [LaporController::class, 'proses'])->middleware('role:penyuluh');
    Route::post('/lapor/{id}/selesai', [LaporController::class, 'selesai'])->middleware('role:penyuluh');
    Route::post('/lapor/{id}/tolak', [LaporController::class, 'tolak'])->middleware('role:penyuluh');


    // ================= PETANI =================
    Route::prefix('petani')->middleware('role:petani')->group(function () {
        Route::get('/dashboard', [PetaniController::class, 'dashboard']);
    });


    // ================= PENYULUH =================
    Route::prefix('penyuluh')->middleware('role:penyuluh')->group(function () {
        Route::get('/dashboard', [PenyuluhController::class, 'index']);
        Route::get('/ranking', [PenyuluhController::class, 'ranking']);
        Route::get('/laporan', [PenyuluhController::class, 'laporan']);
        Route::get('/analisis/{id}', [PenyuluhController::class, 'analisis']);
        Route::get('/help', [PenyuluhController::class, 'help']);
    });


    // ================= ADMIN =================
    Route::prefix('admin')->middleware('role:admin')->group(function () {
        Route::get('/dashboard', [AdminController::class, 'dashboard']);
        Route::get('/laporan', [AdminController::class, 'laporan']);
        Route::get('/laporan/{id}', [AdminController::class, 'detail']);
        Route::get('/kontak', [KontakController::class, 'index']);
        Route::get('/help', [PenyuluhController::class, 'help']);
    });


    // ================= PROFIL =================
    Route::get('/profil', fn() => view('profil'));
    Route::post('/profil/update', [AuthController::class, 'updateProfile']);
});

