<?php

namespace App\Http\Controllers\Web\Home;

use App\Http\Controllers\Controller;
use App\Models\Ecommerce\Product\Collection\Collection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View
     */
    public function index(Request $request)
    {
        $collections = Collection::query()
            ->ordered()
            ->where('is_visible', true)
            ->simplePaginate(
                perPage: 6,
                page: request('page') ?: 1
            );

        return $request->ajax()
                ? $this->ajaxPaginate($collections)
                : view('web.themes.shella.pages.home.index', [
                    'collections' => $collections,
                ]);
    }

    /**
     * Ajax paginate
     *
     * @param  Collection  $collections
     */
    public function ajaxPaginate($collections): JsonResponse
    {
        return response()->json([
            'pagination' => $collections,
            'collections' => view('web.themes.trendyol.components.products.collection.collections', [
                'collections' => $collections,
            ])->render(),
        ]);
    }

    public function userLikes()
    {
        $user_likes = auth()->check()
                        ? auth()->user()->favorites()->with(['media', 'brand'])->get()
                        : collect();

        cache()->put('user_favorites_likes_count', $user_likes->count());

        return $user_likes;
    }
}
