<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Owner\DashboardController as OwnerDashboardController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ArticleController;
use App\Http\Controllers\Admin\BahanBakuController;
use App\Http\Controllers\Admin\KategoriProdukController;
use App\Http\Controllers\Admin\ProdukController;
use App\Http\Controllers\Admin\LokasiDistribusiController;
use App\Http\Controllers\Admin\MitraController;
use App\Http\Controllers\Admin\SertifikatController;
use App\Http\Controllers\Admin\KunjunganController;
use App\Http\Controllers\Owner\ProduksiExportController;
use App\Http\Controllers\StaffProduksi\ProduksiController;
use App\Http\Controllers\StaffProduksi\DashboardController as StaffProduksiDashboardController;
use App\Http\Controllers\StaffGudang\GudangController;
use App\Http\Controllers\StaffGudang\StokProdukController;
use App\Http\Controllers\Admin\PesananController as AdminPesananController;
use App\Http\Controllers\Customer\PesananController as CustomerPesananController;
use App\Http\Controllers\Customer\DashboardController as CustomerDashboardController;
use App\Http\Controllers\Customer\KatalogController;
use App\Models\Article;
use App\Models\Produk;
use App\Models\KategoriProduk;

//landing page
Route::get('/', function () {
    $latestArticles = Article::where('status', 'aktif')->latest()->take(1)->get();
    $products = Produk::with('kategori')->where('status', 'aktif')->latest()->get();
    
    return view('welcome', compact('latestArticles', 'products'));
});

Route::get('/produk', function () {
    $categories = KategoriProduk::all();
    $query = Produk::with('kategori')->where('status', 'aktif');
    if (request('category')) {
        $query->whereHas('kategori', function($q) {
            $q->where('nama_kategori', request('category'));
        });
    }
    $products = $query->latest()->get();
    return view('produk.index', compact('products', 'categories'));
})->name('produk.index');

Route::get('/lokasi', [LokasiDistribusiController::class, 'indexPublic'])->name('lokasi.index');
Route::get('/artikel/{slug}', [ArticleController::class, 'show'])->name('artikel.show');
Route::get('/artikel', [App\Http\Controllers\Admin\ArticleController::class, 'indexFrontend'])->name('artikel.index');
Route::get('/produk/{id}', [App\Http\Controllers\Admin\ProdukController::class, 'show'])->name('produk.show');
Route::get('/produk/{id}', function ($id) {
    $product = \App\Models\Produk::with('kategori')->findOrFail($id);
    return view('produk.show', compact('product'));
})->name('produk.show');

Route::get('/kontak', function () { return view('kontak.index'); })->name('kontak');
Route::get('/tentangkami', [SertifikatController::class, 'tentangkami'])->name('tentangkami.index');

//Kunjungan Industri
Route::get('/kunjungan', [KunjunganController::class, 'index'])->name('kunjungan.index');
Route::get('/kunjungan/buat', [KunjunganController::class, 'create'])->name('kunjungan.create');
Route::post('/kunjungan/simpan', [KunjunganController::class, 'store'])->name('kunjungan.store');
Route::get('/kunjungan/lacak', [KunjunganController::class, 'lacak'])->name('kunjungan.lacak');


// Group Admin
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::resource('users', UserController::class);
    Route::resource('articles', ArticleController::class);
    Route::resource('bahan-baku', BahanBakuController::class);
    Route::resource('kategori-produk', KategoriProdukController::class);
    Route::resource('produk', ProdukController::class);
    Route::resource('lokasi', LokasiDistribusiController::class);
    Route::resource('sertifikat', SertifikatController::class);
    Route::get('/mitra', [MitraController::class, 'index'])->name('mitra.index');
    Route::get('/kunjungan', [KunjunganController::class, 'indexAdmin'])->name('kunjungan.index');
    Route::get('/kunjungan/{id}', [KunjunganController::class, 'show'])->name('kunjungan.show'); // Tambahkan rute detail
    Route::patch('/kunjungan/{id}/status', [KunjunganController::class, 'updateStatus'])->name('kunjungan.status');
});

// Group Owner
Route::middleware(['auth'])->prefix('owner')->name('owner.')->group(function () {
    Route::get('/dashboard', [OwnerDashboardController::class, 'index'])->name('dashboard');
    Route::get('/produksi', [ProduksiController::class, 'index'])->name('produksi.index');    
    Route::get('/rekap-produksi/export', [ProduksiExportController::class, 'export'])->name('produksi.export');
});

// Group Staff Produksi
Route::middleware(['auth'])->prefix('staffproduksi')->name('staffproduksi.')->group(function () {
    Route::get('/dashboard', [StaffProduksiDashboardController::class, 'index'])->name('dashboard');
    Route::get('/produksi', [ProduksiController::class, 'index'])->name('produksi.index');
    Route::get('/produksi/create', [ProduksiController::class, 'create'])->name('produksi.create');
    Route::post('/produksi/store', [ProduksiController::class, 'store'])->name('produksi.store'); // Ini yang bikin error tadi
    Route::get('/produksi/edit/{tanggal}', [ProduksiController::class, 'edit'])->name('produksi.edit');
    Route::put('/produksi/update/{tanggal}', [ProduksiController::class, 'update'])->name('produksi.update');
    Route::delete('/produksi/{tanggal}', [ProduksiController::class, 'destroy'])->name('produksi.destroy');
});

// Group Staff Gudang
Route::group(['middleware' => ['auth'], 'prefix' => 'staff-gudang', 'as' => 'staffgudang.'], function () {
    Route::get('/dashboard', [GudangController::class, 'dashboard'])->name('dashboard');
    Route::get('/stok-bahanbaku', [GudangController::class, 'indexBahanBaku'])->name('stokbahanbaku.index');
    Route::get('/stok-bahanbaku/{id}/edit', [GudangController::class, 'editStok'])->name('stokbahanbaku.edit');
    Route::patch('/stok-bahanbaku/{id}', [GudangController::class, 'updateStok'])->name('stokbahanbaku.update');
    Route::get('/stok-bahanbaku/update', [GudangController::class, 'create'])->name('stokbahanbaku.create');
    Route::post('/stok-bahanbaku/update', [GudangController::class, 'store_update'])->name('stokbahanbaku.store_update');
    Route::resource('stokproduk', StokProdukController::class)->only(['index', 'edit', 'update']);
});


// Route ini bisa diakses oleh berbagai user
Route::middleware(['auth'])->group(function () {
    Route::get('/pesanan', [AdminPesananController::class, 'index'])->name('pesanan.index');
    Route::get('/pesanan/{id}', [AdminPesananController::class, 'show'])->name('pesanan.show');
    Route::put('/pesanan/{id}', [AdminPesananController::class, 'update'])->name('pesanan.update');
    
    // Route Pengiriman (HANYA SATU dan memanggil dataPengiriman)
    Route::get('/pengiriman', [AdminPesananController::class, 'dataPengiriman'])->name('pengiriman.index');
    Route::patch('/pengiriman/{id}/status', [AdminPesananController::class, 'updateStatus'])->name('admin.pengiriman.updateStatus');

    // Route Export & Lainnya
    Route::get('/admin/pesanan/export', [AdminPesananController::class, 'export'])->name('admin.pesanan.export');
    Route::get('staffgudang.stokproduk.export', [StokProdukController::class, 'export'])->name('stokproduk.export');
    Route::get('/produksi', [App\Http\Controllers\StaffProduksi\ProduksiController::class, 'index'])->name('produksi.index'); 

    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/password', [ProfileController::class, 'updatePassword'])->name('password.update');
    

});



// Group Customer / Mitra
Route::middleware(['auth'])->prefix('customer')->name('customer.')->group(function () {
    Route::get('/dashboard', [CustomerDashboardController::class, 'index'])->name('dashboard');
    Route::get('/pesanan/riwayat', [CustomerPesananController::class, 'riwayat'])->name('pesanan.riwayat');
    Route::resource('pesanan', CustomerPesananController::class);
    Route::get('/katalog', [KatalogController::class, 'index'])->name('katalog.index');
});

// Profile & Default Dashboard
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    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';