<?php

use App\Http\Controllers\AdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\LocationController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

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

// ====================================================
// 1. HALAMAN PUBLIK
// ====================================================
Route::get('/', [ProductController::class, 'index'])->name('front.index');
Route::get('/product/{slug}', [ProductController::class, 'show'])->name('product.detail');
Route::get('/api/check-new-products', [ProductController::class, 'checkNewProducts'])->name('api.new_products');

// Login & Register
Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
Route::post('/register', [AuthController::class, 'register'])->name('register.post');

// ====================================================
// 2. KERANJANG & BELI
// ====================================================
Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
Route::get('/cart/add/{id}', [CartController::class, 'addToCart'])->name('cart.add');
Route::get('/buy-now/{id}', [CartController::class, 'buyNow'])->name('front.buyNow');
Route::delete('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');
Route::post('/cart/checkout', [CartController::class, 'checkoutSelected'])->name('cart.checkout.selected');

// ====================================================
// 3. CHECKOUT, PROFIL & PENGIRIMAN (BUTUH LOGIN)
// ====================================================
Route::middleware(['auth'])->group(function () {

    // Halaman Pesanan Saya
    Route::get('/pesanan-saya', [OrderController::class, 'index'])->name('front.orders');

    // Halaman Detail Pesanan
    Route::get('/pesanan-saya/{id}/detail', [OrderController::class, 'show'])->name('front.order_detail');

    // [BARU] Customer klik Pesanan Selesai (Fitur Poin 5)
    Route::post('/pesanan-saya/{id}/complete', [OrderController::class, 'completeOrder'])->name('front.order_complete');

    // [BARU] Customer konfirmasi penerimaan barang COD
    Route::post('/pesanan-saya/{id}/cod-diterima', [OrderController::class, 'confirmCodReceived'])->name('front.cod.confirm');

    // [BARU] Form & Simpan Ulasan Produk
    Route::get('/review/{order_id}', [App\Http\Controllers\ReviewController::class, 'create'])->name('front.review.create');
    Route::post('/review/{order_id}', [App\Http\Controllers\ReviewController::class, 'store'])->name('front.review.store');
    Route::delete('/review/{id}', [App\Http\Controllers\ReviewController::class, 'destroy'])->name('front.review.destroy');

    // Halaman Profil
    Route::get('/profile', [ProfileController::class, 'index'])->name('profile.index');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');

    // Hapus Alamat di Buku Alamat
    Route::delete('/profile/address/{id}', [ProfileController::class, 'destroyAddress'])->name('profile.address.destroy');

    // Hapus Foto Profil
    Route::delete('/profile/remove-photo', [ProfileController::class, 'removePhoto'])->name('profile.photo.remove');

    // Halaman Checkout & Proses Order
    Route::get('/checkout', [CheckoutController::class, 'index'])->name('front.checkout');
    Route::post('/checkout/process', [CheckoutController::class, 'process'])->name('front.order.store');

    // Halaman Pembayaran & Sukses
    Route::get('/payment/{id}', [CheckoutController::class, 'showPayment'])->name('front.payment');
    Route::get('/payment-success/{id}', [CheckoutController::class, 'success'])->name('front.payment.success');

    // --- API AJAX LOKASI (DIUBAH MENGGUNAKAN LocationController) ---
    Route::post('/api/get-cities', [LocationController::class, 'getCities'])->name('api.cities');
    Route::post('/api/get-districts', [LocationController::class, 'getDistricts'])->name('api.districts');
    Route::post('/api/get-villages', [LocationController::class, 'getVillages'])->name('api.villages');

    // API AJAX Cek Ongkir tetap pakai CheckoutController
    Route::post('/api/check-ongkir', [CheckoutController::class, 'checkOngkir'])->name('api.checkOngkir');

});

// ====================================================
// 4. ADMIN PANEL
// ====================================================
Route::middleware(['auth', 'is_admin'])->group(function () {
    Route::get('/admin', [AdminController::class, 'index'])->name('admin.index');
    Route::get('/admin/create', [AdminController::class, 'create'])->name('admin.create');
    Route::post('/admin/store', [AdminController::class, 'store'])->name('admin.store');
    Route::get('/admin/edit/{id}', [AdminController::class, 'edit'])->name('admin.edit');
    Route::put('/admin/update/{id}', [AdminController::class, 'update'])->name('admin.update');
    Route::delete('/admin/delete/{id}', [AdminController::class, 'destroy'])->name('admin.destroy');

    Route::get('/admin/orders', [AdminController::class, 'orders'])->name('admin.orders');
    Route::get('/admin/api/check-new-orders', [AdminController::class, 'checkNewOrders'])->name('admin.api.new_orders');
    // [BARU] Admin input nomor resi (Fitur Poin 1)
    Route::post('/admin/orders/{id}/tracking', [AdminController::class, 'updateTracking'])->name('admin.orders.tracking');

    Route::get('/admin/orders/print/{id}', [AdminController::class, 'print'])->name('admin.print');
    Route::get('/admin/export-income', [AdminController::class, 'exportIncome'])->name('admin.export.income');
    Route::get('/admin/users', [AdminController::class, 'users'])->name('admin.users');
    Route::post('/admin/product/{id}/stock', [AdminController::class, 'addStock'])->name('admin.stock.add');
});

// ====================================================
// 5. MIDTRANS WEBHOOK (BEBAS LOGIN)
// ====================================================
Route::post('/midtrans/callback', [OrderController::class, 'callback'])->name('midtrans.callback');
