<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Produk;

class LandingController extends Controller
{
    public function index()
    {
        $produks = Produk::latest()->take(8)->get();
        return view('landing.home', compact('produks'));
    }

    public function shop(Request $request)
    {
        $query = Produk::query();

        if ($request->has('search')) {
            $query->where('nama_produk', 'like', '%' . $request->search . '%');
        }

        $produks = $query->paginate(12);

        return view('landing.shop', compact('produks'));
    }

    public function detail($id)
    {
        $produk = Produk::with('petani')->findOrFail($id);
        $related_products = Produk::where('petani_id', $produk->petani_id)
            ->where('id', '!=', $id)
            ->take(4)
            ->get();

        return view('landing.detail', compact('produk', 'related_products'));
    }
}
