<?php

use App\Http\Controllers\AuthPelangganController;
use App\Http\Controllers\KatalogController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BackendDashboardController;
use App\Http\Controllers\TransaksiController;
use App\Http\Controllers\CrmInteraktifController;
use App\Http\Controllers\ProfilController;
use App\Http\Controllers\KeranjangController;
use App\Http\Controllers\KeluhanController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\KuponController;
use App\Http\Controllers\BarangController;
use App\Http\Controllers\LaporanController;

// Redirect halaman utama ke login sementara waktu
Route::get('/', function () {
    return redirect('/login');
});

// ==========================================
// 🔐 MODUL AUTENTIKASI PELANGGAN & MEMBER
// ==========================================
Route::get('/login', [AuthPelangganController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthPelangganController::class, 'login']);
Route::get('/register', [AuthPelangganController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthPelangganController::class, 'register']);
Route::post('/logout-pelanggan', [AuthPelangganController::class, 'logout'])->name('pelanggan.logout');

// ==========================================
// 🛒 MODUL KATALOG, PROFIL & KERANJANG BELANJA
// ==========================================
Route::get('/katalog', [KatalogController::class, 'index'])->name('katalog');
Route::get('/katalog/detail/{id}', [KatalogController::class, 'detail'])->name('katalog.detail');

Route::get('/profil', [ProfilController::class, 'index'])->name('profil.index');
Route::post('/profil/update', [ProfilController::class, 'update'])->name('profil.update');

Route::get('/keranjang', [KeranjangController::class, 'index'])->name('keranjang.index');
Route::post('/keranjang/tambah', [KeranjangController::class, 'tambah'])->name('keranjang.tambah');
Route::post('/keranjang/kurangi/{id}', [KeranjangController::class, 'kurangi'])->name('keranjang.kurangi');
Route::post('/keranjang/checkout-final', [KeranjangController::class, 'prosesCheckout'])->name('keranjang.checkout_final');
Route::delete('/keranjang/hapus/{id}', [KeranjangController::class, 'hapus'])->name('keranjang.hapus');

// ==========================================
// 📦 MODUL TRANSAKSI NOTA & LACAK PAKET (CUSTOMER)
// ==========================================
Route::post('/checkout', [TransaksiController::class, 'checkoutSederhana'])->name('checkout.proses');
Route::get('/nota/{id}', [TransaksiController::class, 'detailNota'])->name('nota.detail');
Route::post('/nota/{id}/upload', [TransaksiController::class, 'uploadStruk'])->name('nota.upload');
Route::get('/lacak-paket', [TransaksiController::class, 'lacakPaket'])->name('lacak.paket');
Route::post('/transaksi/selesai/{id}', [TransaksiController::class, 'konfirmasiSelesai'])->name('transaksi.selesai');
Route::post('/transaksi/batalkan/{id}', [KeranjangController::class, 'batalkanPesanan'])->name('transaksi.batalkan');

// ==========================================
// 💬 MODUL LIVE CHAT & FEEDBACK KELUHAN (CUSTOMER)
// ==========================================
Route::get('/keluhan', [KeluhanController::class, 'index'])->name('keluhan.index');
Route::post('/keluhan/kirim', [KeluhanController::class, 'kirim'])->name('keluhan.kirim');
Route::post('/kirim-chat-crm', [ChatController::class, 'kirim'])->name('chat.kirim');

// ==========================================
// 🧑‍💻 CORE BACKEND PANEL STAFF (ADMIN & OWNER)
// ==========================================
Route::get('/admin/dashboard', [BackendDashboardController::class, 'indexAdmin'])->name('admin.dashboard');
Route::get('/owner/dashboard', [BackendDashboardController::class, 'indexOwner'])->name('owner.dashboard');

// A. Manajemen Transaksi & Verifikasi Struk oleh Admin
Route::get('/admin/transaksi', [BackendDashboardController::class, 'indexTransaksi'])->name('admin.transaksi.index');
Route::get('/admin/verifikasi/{id}', [BackendDashboardController::class, 'periksaPembayaran'])->name('admin.transaksi.periksa');
// Menggunakan metode POST sesuai data rute asal
Route::post('/admin/transaksi/proses/{id}', [BackendDashboardController::class, 'prosesVerifikasi'])->name('admin.transaksi.proses');

// B. Manajemen Respon Keluhan Pelanggan oleh Admin
Route::get('/admin/keluhan', [BackendDashboardController::class, 'indexKeluhan'])->name('admin.keluhan.index');
Route::post('/admin/keluhan/solusi/{id}', [BackendDashboardController::class, 'prosesSolusi'])->name('admin.keluhan.solusi');

// C. Manajemen Live Chat Customer Service Admin
Route::get('/admin/live-chat', [BackendDashboardController::class, 'indexChat'])->name('admin.chat.index');
Route::post('/admin/live-chat/kirim/{pelanggan_id}', [BackendDashboardController::class, 'kirimChat'])->name('admin.chat.kirim');

// D. Modul Pengelolaan Data Master Sembako Gudang Admin
Route::get('/admin/barang', [BarangController::class, 'index'])->name('admin.barang.index');
Route::post('/admin/barang/store', [BarangController::class, 'store'])->name('admin.barang.store');
Route::post('/admin/barang/update/{id}', [BarangController::class, 'update'])->name('admin.barang.update');
Route::delete('/admin/barang/delete/{id}', [BarangController::class, 'destroy'])->name('admin.barang.delete');

// E. Modul Pengelolaan Kupon Diskon Promo Admin Store
Route::get('/admin/kupon', [KuponController::class, 'index'])->name('admin.kupon.index');
Route::post('/admin/kupon/store', [KuponController::class, 'store'])->name('admin.kupon.store');
Route::delete('/admin/kupon/delete/{id}', [KuponController::class, 'destroy'])->name('admin.kupon.delete');

// ==========================================
// 📊 MODUL LAPORAN KEUANGAN & ANALISIS CRM
// ==========================================
Route::get('/admin/laporan', [LaporanController::class, 'index'])->name('admin.laporan.index');
Route::get('/admin/laporan/cetak', [LaporanController::class, 'cetak'])->name('admin.laporan.cetak');
Route::get('/admin/laporan/pelanggan', [LaporanController::class, 'cetakPelanggan'])->name('admin.laporan.pelanggan');

// ==========================================
// 🚪 LOGOUT SISTEM GLOBAL
// ==========================================
Route::get('/logout', function () {
    session()->forget(['admin_logged', 'owner_logged', 'pelanggan_id', 'pelanggan_nama']);
    return redirect('/login')->with('success', 'Anda telah berhasil logout dari sistem.');
})->name('logout');
