<?php

use App\Http\Controllers\Admin\KategoriController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\ForgotPasswordController;
use App\Http\Controllers\LandingController;
use App\Http\Controllers\PesanController;
use App\Http\Controllers\Petani\DashboardController;
use App\Http\Controllers\Petani\ProdukController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\TransaksiController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// --- PUBLIC ROUTES (Bisa diakses siapa saja) ---
Route::get('/', [LandingController::class, 'index'])->name('home');
Route::get('/shop', [LandingController::class, 'shop'])->name('shop');
Route::get('/produk/{id}', [LandingController::class, 'detail'])->name('produk.detail');

// --- CART ROUTES ---
Route::get('/cart', [CartController::class, 'index'])->name('cart');
Route::post('/cart/add', [CartController::class, 'addToCart'])->name('cart.add');
Route::delete('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');

// --- ROUTE GLOBAL (BISA DIAKSES PETANI & PEMBELI) ---
Route::post('/pesan/kirim', [PesanController::class, 'store'])
    ->middleware('auth:pembeli,petani')
    ->name('pesan.kirim');


// --- AUTH ROUTES (Guest Only) ---
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login-proses', [AuthController::class, 'loginProcess'])->name('login.proses');

    Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
    Route::post('/register-proses', [AuthController::class, 'registerProcess'])->name('register.proses');

    Route::get('/forgot-password', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
    Route::post('/forgot-password', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');

    Route::get('/reset-password/{token}', [ForgotPasswordController::class, 'showResetForm'])->name('password.reset');
    Route::post('/reset-password', [ForgotPasswordController::class, 'reset'])->name('password.update');
});

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


// --- PEMBELI AREA (Wajib Login) ---
Route::middleware(['auth:pembeli'])->group(function () {
    // Menampilkan halaman checkout dari cart atau beli langsung
    Route::get('/checkout', [TransaksiController::class, 'checkoutPage'])->name('checkout');

    // Proses Simpan Transaksi
    Route::post('/checkout/process', [TransaksiController::class, 'prosesCheckout'])->name('checkout.proses');

    // Riwayat Pesanan
    Route::get('/pesanan-saya', [TransaksiController::class, 'pesananSaya'])->name('pesanan.saya');

    // Route Konfirmasi Pesanan Selesai
    Route::post('/pesanan/{id}/selesai', [TransaksiController::class, 'konfirmasiSelesai'])->name('pesanan.selesai');

    Route::patch('/cart/update', [CartController::class, 'updateCart'])->name('cart.update');
    Route::delete('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');

    // Route Pesan untuk Pembeli
    Route::get('/pesan', [PesanController::class, 'index'])->name('pembeli.pesan.index');
    Route::get('/pesan/{id}', [PesanController::class, 'show'])->name('pembeli.pesan.show');

    Route::get('/profile', [ProfileController::class, 'editPembeli'])->name('pembeli.profile');
    Route::put('/profile', [ProfileController::class, 'updatePembeli'])->name('pembeli.profile.update');
});

Route::middleware(['auth:admin'])->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('/admin/monitoring', [AdminController::class, 'monitoring'])->name('admin.monitoring');

    // Petani Verification Logic
    Route::controller(AdminController::class)->prefix('admin/verifikasi')->group(function () {
        Route::get('/', 'verifikasiIndex')->name('admin.verifikasi.index');
        Route::get('/{id}', 'verifikasiShow')->name('admin.verifikasi.show');
        Route::post('/{id}/approve', 'verifikasiApprove');
        Route::post('/{id}/reject', 'verifikasiReject');
    });
    
    // CRUD Kategori
    Route::resource('admin/kategori', KategoriController::class)->names('admin.kategori');

    Route::get('/admin/transaksi/{id}', [AdminController::class, 'transaksiDetail'])
        ->name('admin.transaksi.detail');
});


// --- PETANI AREA ---
Route::middleware(['auth:petani'])->group(function () {

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

    // CRUD Produk Petani
    Route::resource('petani/produk', ProdukController::class)->names('petani.produk');
    Route::delete('/petani/produk/image/{id}', [App\Http\Controllers\Petani\ProdukController::class, 'deleteImage'])->name('petani.produk.image.delete');

    // Manajemen Pesanan Masuk (Petani)
    Route::get('/petani/pesanan', [TransaksiController::class, 'pesananMasuk'])->name('petani.pesanan.index');
    Route::patch('/petani/pesanan/{id}', [TransaksiController::class, 'updateStatus'])->name('petani.pesanan.update');
    Route::get('/petani/pesanan/{id}', [TransaksiController::class, 'pesananDetail'])->name('petani.pesanan.detail');

    // Route Pesan untuk Petani
    Route::get('/petani/pesan', [PesanController::class, 'index'])->name('petani.pesan.index');
    Route::get('/petani/pesan/{id}', [PesanController::class, 'show'])->name('petani.pesan.show');

    Route::get('/petani/profile', [ProfileController::class, 'editPetani'])->name('petani.profile');
    Route::put('/petani/profile', [ProfileController::class, 'updatePetani'])->name('petani.profile.update');
});
