<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\ProductManagement\Category;
use App\Models\OrderManagement\Coupon;
use App\Models\ProductManagement\Product;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class HomeController extends Controller{

    public function index(): View{
        $coupon = Coupon::active()->public()->first();
        $categories = Category::active()
                                ->whereParentId(null)
                               // ->limit(4)
                                ->get();

        $disk = Storage::disk("assets");

        $sliders = $disk->allFiles("frontend/img/sliders");

        return view('frontend.index', compact('categories', 'coupon',"sliders"));
    }

    public function search(Request $request): JsonResponse{
        $data = Product::select('slug', 'name')
                        ->where('name', 'like', "%$request->productName%")
                        ->active()
                        ->available()
                      //  ->hasQuantity()
                        ->activeCategory()
                        ->take(5)
                        ->get();

        return response()->json($data);
    }
}
