<?php

namespace App\Http\Controllers;

use App\Repositories\Catalog\CategoryRepository;
use App\Repositories\Catalog\GenderRepository;
use App\Repositories\Catalog\ProductRepository;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index(Request $request)
    {

        $page = $request->page ?? 1;

        $filters = $request->filter ?? [];
        $sortBy = $request->sort_by ?? 'relevance';

        $genderRepository = new GenderRepository;
        $categoryRepository = new CategoryRepository;

        $genders = $genderRepository->getList([]);
        $categories = $categoryRepository->getList([]);

        $productRepository = new ProductRepository;
        $products = $productRepository->getList([
            'limit' => 20,
            'sort_by' => $sortBy,
        ]);

        

        return view('shop.catalog-fashion', [
            'filters' => $filters,
            'genders' => $genders,
            'categories' => $categories,

            'products' => $products,
            'page'  => $page,
        ]);
    }

    public function detail($slug, Request $request, ProductRepository $productRepository)
    {

        $product = $productRepository->show($slug);

        $complete_look_products_data = $productRepository->getList([
            'category_id' => $product->category1_id,
            'limit' => 4,
        ]);

        $complete_look_products = collect($complete_look_products_data->items())->chunk(2);

        return view('shop.product-fashion', [
            'product' => $product,
            'complete_look_products' => $complete_look_products,

        ]);
    }
}
