<?php

use App\Http\Controllers\ArticleController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\CheckoutController; // <-- Tambahan Baru

/*
|--------------------------------------------------------------------------
| RUTE PUBLIK (Toko Online)
|--------------------------------------------------------------------------
*/
// Rute Beranda
Route::get('/', [HomeController::class, 'index']);

// RUTE BLOG PUBLIK (Baru Ditambahkan)
Route::get('/blog', [HomeController::class, 'blogIndex'])->name('blog.index');
Route::get('/blog/{slug}', [HomeController::class, 'blogShow'])->name('blog.show');

// Rute Keranjang
Route::post('/cart/add/{id}', [CartController::class, 'add'])->name('cart.add');
Route::get('/cart', [CartController::class, 'index'])->name('cart.index');

// Rute Checkout (Diperbarui menggunakan CheckoutController)
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::post('/checkout/process', [CheckoutController::class, 'process'])->name('checkout.process');
Route::get('/checkout/success/{order_number}', [OrderController::class, 'success'])->name('checkout.success');

// Rute Cek Status Pesanan Publik (Baru Ditambahkan)
Route::get('/cek-pesanan', [OrderController::class, 'track'])->name('order.track');

// Rute API Internal untuk Biteship (AJAX) - Baru Ditambahkan
Route::get('/api/biteship/search-area', [CheckoutController::class, 'searchArea'])->name('biteship.search_area');
Route::post('/api/biteship/check-rates', [CheckoutController::class, 'checkRates'])->name('biteship.check_rates');


/*
|--------------------------------------------------------------------------
| RUTE AUTENTIKASI (Login)
|--------------------------------------------------------------------------
*/
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');


/*
|--------------------------------------------------------------------------
| RUTE ADMIN PANNEL
|--------------------------------------------------------------------------
*/
// Grup rute admin dilindungi Middleware 'auth' agar tidak bisa ditembus tanpa login
Route::middleware('auth')->prefix('admin')->group(function () {
    
    // Dashboard Utama
    Route::get('/dashboard', [AdminController::class, 'index'])->name('admin.dashboard');
    
    // Update Status Pesanan & Update Stok Cepat
    Route::post('/order/{id}/status', [AdminController::class, 'updateStatus'])->name('admin.order.status');
    Route::post('/product/{id}/stock', [AdminController::class, 'updateStock'])->name('admin.product.stock');

    // CRUD Kelola Produk Penuh (Tambah, Edit, Simpan, Hapus)
    Route::get('/product/create', [AdminController::class, 'createProduct'])->name('admin.product.create');
    Route::post('/product/store', [AdminController::class, 'storeProduct'])->name('admin.product.store');
    Route::get('/product/{id}/edit', [AdminController::class, 'editProduct'])->name('admin.product.edit');
    Route::put('/product/{id}', [AdminController::class, 'updateProduct'])->name('admin.product.update');
    Route::delete('/product/{id}', [AdminController::class, 'destroyProduct'])->name('admin.product.destroy');
    
    // CRUD Artikel Blog
    Route::get('/article', [ArticleController::class, 'index'])->name('admin.article.index');
    Route::get('/article/create', [ArticleController::class, 'create'])->name('admin.article.create');
    Route::post('/article/store', [ArticleController::class, 'store'])->name('admin.article.store');
    Route::get('/article/{id}/edit', [ArticleController::class, 'edit'])->name('admin.article.edit');
    Route::put('/article/{id}', [ArticleController::class, 'update'])->name('admin.article.update');
    Route::delete('/article/{id}', [ArticleController::class, 'destroy'])->name('admin.article.destroy');
    
});