<?php

use App\Http\Controllers\{AuthController, PosController, ProductController, ReportController};
use App\Http\Controllers\{CategoryController, UserController, SettingController};
use Illuminate\Support\Facades\Route;

// Auth
Route::middleware('guest')->group(function () {
    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')->middleware('auth');
Route::post('/profile/update-password', [UserController::class, 'updatePassword'])->name('profile.update-password')->middleware('auth');

// Dashboard
Route::get('/', [ReportController::class, 'dashboard'])->name('dashboard')->middleware('auth');

// POS - Kasir
Route::prefix('pos')->name('pos.')->middleware('auth')->group(function () {
    Route::get('/open-session', [PosController::class, 'openSession'])->name('open-session');
    Route::post('/open-session', [PosController::class, 'storeSession'])->name('store-session');
    Route::get('/', [PosController::class, 'index'])->name('index');
    Route::post('/transaction', [PosController::class, 'store'])->name('store');
    Route::get('/receipt/{transaction}', [PosController::class, 'receipt'])->name('receipt');
    Route::get('/search-product', [PosController::class, 'searchProduct'])->name('search-product');
    Route::post('/close-session', [PosController::class, 'closeSession'])->name('close-session');
    Route::get('/session-summary', [PosController::class, 'sessionSummary'])->name('session-summary');
    Route::post('/update-printer-settings', [PosController::class, 'updatePrinterSettings'])->name('update-printer-settings');
});

// Produk
Route::resource('products', ProductController::class)->middleware('auth');
Route::post('/products/{product}/adjust-stock', [ProductController::class, 'adjustStock'])
    ->name('products.adjust-stock')->middleware('auth');

// Kategori
Route::resource('categories', CategoryController::class)->middleware('auth');

// Laporan (admin & manajer)
Route::prefix('reports')->name('reports.')->middleware(['auth', 'role:admin,manajer'])->group(function () {
    Route::get('/sales', [ReportController::class, 'sales'])->name('sales');
    Route::get('/voids', [ReportController::class, 'voids'])->name('voids');
    Route::get('/clerk', [ReportController::class, 'clerk'])->name('clerk');
    Route::get('/stock', [ReportController::class, 'stock'])->name('stock');
    Route::get('/purchases', [ReportController::class, 'purchases'])->name('purchases');
    Route::post('/purchases/{purchase}/pay', [ReportController::class, 'payPurchase'])->name('purchases.pay');
    Route::get('/expenses', [ReportController::class, 'expenses'])->name('expenses');
    Route::get('/transaction-detail/{transaction}', [ReportController::class, 'transactionDetail'])->name('transaction-detail');
});

// User management (admin only)
Route::resource('users', UserController::class)->middleware(['auth', 'role:admin']);

// Settings
Route::get('/settings', [SettingController::class, 'index'])->name('settings.index')->middleware(['auth', 'role:admin']);
Route::post('/settings', [SettingController::class, 'update'])->name('settings.update')->middleware(['auth', 'role:admin']);

// Riwayat transaksi kasir hari ini
Route::get('/pos/history', [PosController::class, 'history'])->name('pos.history')->middleware('auth');
Route::post('/pos/void/{transaction}', [PosController::class, 'void'])->name('pos.void')->middleware('auth');
Route::post('/pos/verify-authority', [PosController::class, 'verifyAuthority'])->name('pos.verify-authority')->middleware('auth');
Route::post('/pos/restock', [PosController::class, 'restock'])->name('pos.restock')->middleware('auth');
Route::post('/pos/expense', [PosController::class, 'expense'])->name('pos.expense')->middleware('auth');

// Export laporan ke Excel (aktifkan setelah composer install maatwebsite/excel)
// Route::get('/reports/export/sales',  [ReportController::class, 'exportSales'])->name('reports.export.sales')->middleware(['auth','role:admin,manajer']);
// Route::get('/reports/export/clerk',  [ReportController::class, 'exportClerk'])->name('reports.export.clerk')->middleware(['auth','role:admin,manajer']);
// Route::get('/reports/export/stock',  [ReportController::class, 'exportStock'])->name('reports.export.stock')->middleware(['auth','role:admin,manajer']);
