<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ObatMasukController;
use App\Http\Controllers\ObatKeluarController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\KadaluarsaController;
use App\Http\Controllers\KategoriController;
use App\Http\Controllers\SatuanController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ResepController;
use App\Http\Controllers\UserManagementController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::middleware(['auth'])->group(function () {
    // Dashboard - All roles can access
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Resep - All roles can view
    Route::get('/resep', [ResepController::class, 'index'])->name('resep.index');
    Route::get('/resep/{resep}', [ResepController::class, 'show'])->name('resep.show');
    Route::get('/resep/{resep}/print', [ResepController::class, 'print'])->name('resep.print');

    // Resep - Dokter only (create, edit, delete)
    Route::middleware(['role:dokter'])->group(function () {
        Route::get('/resep-create', [ResepController::class, 'create'])->name('resep.create');
        Route::post('/resep', [ResepController::class, 'store'])->name('resep.store');
        Route::get('/resep/{resep}/edit', [ResepController::class, 'edit'])->name('resep.edit');
        Route::put('/resep/{resep}', [ResepController::class, 'update'])->name('resep.update');
        Route::delete('/resep/{resep}', [ResepController::class, 'destroy'])->name('resep.destroy');
    });

    // Kadaluarsa - All roles can access
    Route::get('/kadaluarsa', [KadaluarsaController::class, 'index'])->name('kadaluarsa.index');

    // Apoteker only routes
    Route::middleware(['role:apoteker'])->group(function () {
        // Resep - Update status by apoteker
        Route::patch('/resep/{resep}/update-status', [ResepController::class, 'updateStatus'])->name('resep.update-status');
    });

    // Obat Masuk, Obat Keluar, Kategori, Satuan - Full CRUD for Superadmin & Apoteker
    Route::middleware(['role:apoteker,superadmin'])->group(function () {
        Route::resource('obat-masuk', ObatMasukController::class);
        Route::resource('obat-keluar', ObatKeluarController::class);
        Route::resource('kategori', KategoriController::class)->except(['show']);
        Route::resource('satuan', SatuanController::class)->except(['show']);
    });

    // Laporan - Apoteker & Superadmin
    Route::middleware(['role:apoteker,superadmin'])->group(function () {
        Route::get('/laporan', [LaporanController::class, 'index'])->name('laporan.index');
        Route::get('/laporan/export-pdf', [LaporanController::class, 'exportPdf'])->name('laporan.export-pdf');
        Route::get('/laporan/export-excel', [LaporanController::class, 'exportExcel'])->name('laporan.export-excel');
    });

    // Superadmin only routes
    Route::middleware(['role:superadmin'])->group(function () {
        Route::resource('user-management', UserManagementController::class)
            ->except(['show'])
            ->parameters(['user-management' => 'user']);
    });

    // Profile (from Breeze) - All roles can access
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
