<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\Login;
//Untuk Kasir
use App\Http\Controllers\kasir\LaporanController;
use App\Http\Controllers\kasir\StokController as KasirStokController;
use App\Http\Controllers\kasir\TransaksiController;

//Untuk Admin
use App\Http\Controllers\admin\DashboardController;
use App\Http\Controllers\admin\ProdukController;
use App\Http\Controllers\admin\ResepController;
use App\Http\Controllers\admin\StokController as AdminStokController;
use App\Http\Controllers\admin\LaporanController as AdminLaporanController;

// 1. AUTHENTICATION
Route::get('/', function () {
    return view('auth.login');
});

Route::get('/login', function () {
    return view('auth.login');
})->name('login');

Route::get('/daftar', function () {
    return view('auth.daftar');
});

Route::post('/login', [Login::class, 'login']);
Route::post('/daftar', [Login::class, 'register']);
Route::post('/logout', [Login::class, 'logout'])->name('logout');


// 2. FITUR SISTEM (Harus Login Terlebih Dahulu)
Route::middleware('auth')->group(function () {
    Route::get('/admin/resep/export', [ResepController::class, 'export'])->name('admin.resep.export');

    // ==========================================
    // KHUSUS USER ADMIN
    // ==========================================
    Route::middleware('role:admin')->group(function () {

        Route::get('/admin/dashboard', function () {
            return view('admin.dashboard');
        })->name('admin.dashboard');

        Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
        Route::get('/admin/produk', [ProdukController::class, 'index']);
        Route::get('/admin/produk/create', [App\Http\Controllers\admin\ProdukController::class, 'create']);
        Route::post('/admin/produk', [App\Http\Controllers\admin\ProdukController::class, 'store']);
        Route::put('/admin/produk/{id}', [App\Http\Controllers\admin\ProdukController::class, 'update']);
        Route::delete('/admin/produk/{id}', [App\Http\Controllers\admin\ProdukController::class, 'destroy'])->name('admin.produk.destroy');
        Route::get('/admin/resep', [ResepController::class, 'index'])->name('admin.resep');
        Route::post('/admin/resep/store', [ResepController::class, 'store'])->name('admin.resep.store');
        Route::get('/admin/resep/{id}', [ResepController::class, 'show'])->name('admin.resep.show');
        Route::put('/admin/resep/update/{id}', [ResepController::class, 'update'])->name('admin.resep.update');
        Route::delete('/admin/resep/delete/{id}', [ResepController::class, 'destroy'])->name('admin.resep.destroy');
        Route::get('/admin/resep/{id}/edit', [ResepController::class, 'edit'])->name('admin.resep.edit');
        Route::get('/admin/kontrol-stok', [AdminStokController::class, 'index'])->name('admin.stok');
        Route::put('/admin/kontrol-stok/{id}', [AdminStokController::class, 'update'])->name('admin.stok.update');
        Route::delete('/admin/stok/destroy/{id}', [App\Http\Controllers\admin\StokController::class, 'destroy'])->name('admin.stok.destroy');
        Route::put('/admin/stok/restock/{id}', [App\Http\Controllers\admin\StokController::class, 'restock'])->name('admin.stok.restock');
        Route::post('/admin/stok/store', [App\Http\Controllers\admin\StokController::class, 'store'])->name('admin.stok.store');
        Route::get('/admin/stok/export', [App\Http\Controllers\admin\StokController::class, 'export'])->name('admin.stok.export');
        Route::get('/admin/laporan', [App\Http\Controllers\admin\LaporanController::class, 'index'])->name('admin.laporan');
        Route::get('/admin/laporan/export-excel', [App\Http\Controllers\admin\LaporanController::class, 'exportExcel'])->name('admin.laporan.export_excel');
        Route::get('/admin/laporan/export-pdf', [App\Http\Controllers\admin\LaporanController::class, 'exportPdf'])->name('admin.laporan.export_pdf');
        Route::get('/admin/laporan/detail/{id}', [App\Http\Controllers\admin\LaporanController::class, 'getDetail']);
    });



    // ==========================================
    // KHUSUS USER KASIR
    // ==========================================
    Route::middleware('role:kasir')->group(function () {

        Route::get('/kasir/dashboard', function () {
            $produks = \App\Models\Produk::all();
            return view('kasir.dashboard', compact('produks'));
        })->name('kasir.dashboard');

        // --- BAGIAN STOK ---
        Route::get('/kasir/stok', [App\Http\Controllers\kasir\StokController::class, 'index'])->name('kasir.stok');
        Route::post('/kasir/stok/simpan', [App\Http\Controllers\kasir\StokController::class, 'store'])->name('kasir.stok.simpan');
        Route::put('/kasir/stok/update/{id}', [App\Http\Controllers\kasir\StokController::class, 'update'])->name('kasir.stok.update');
        Route::delete('/kasir/stok/delete/{id}', [App\Http\Controllers\kasir\StokController::class, 'destroy'])->name('kasir.stok.delete');

        // --- BAGIAN LAPORAN ---
        Route::get('/kasir/laporan', [LaporanController::class, 'index'])->name('kasir.laporan');
        Route::get('/kasir/laporan/export-excel', [LaporanController::class, 'exportExcel'])->name('kasir.laporan.export_excel');
        Route::get('/kasir/laporan/export-pdf', [LaporanController::class, 'exportPdf'])->name('kasir.laporan.export_pdf');

        // --- BAGIAN TRANSAKSI ---
        Route::get('/kasir/transaksi', [TransaksiController::class, 'index'])->name('kasir.transaksi');
        Route::post('/kasir/transaksi/store', [TransaksiController::class, 'store'])->name('kasir.transaksi.store');
        Route::post('/kasir/simpan-transaksi', [TransaksiController::class, 'store'])->name('kasir.simpan_transaksi');
    });
});
