<?php

namespace App\Http\Controllers\Marketplace;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Product;
use Inertia\Inertia;

class DealController extends Controller
{
    public function index(Request $request)
    {
        // Fetch products that have a discount, ordered by the highest discount first
        $products = Product::with(['images', 'seller:id,company_name,name,country_id', 'seller.country'])
            ->where('discount_percentage', '>', 0)
            ->orderBy('discount_percentage', 'desc')
            ->latest()
            ->paginate(15);

        // Return JSON for the "Load More" Ajax request
        if ($request->wantsJson()) {
            return response()->json($products);
        }

        return Inertia::render('Marketplace/SuperDeals', [
            'initialProducts' => $products
        ]);
    }
}