<?php

namespace App\Http\Controllers\Api\V1\Product;

use App\Http\Controllers\Api\V1\Product\Resources\ProductIndexResource;
use App\Http\Controllers\Api\V1\Product\Resources\ProductShowResource;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Product\ProductIndexRequest;
use App\Models\Ecommerce\Product\Product\Product;
use App\Repositories\Ecommerce\Product\ProductRepository;
use Illuminate\Http\JsonResponse;

class ProductController extends Controller
{
    /**
     * Products (index)
     */
    public function index(ProductIndexRequest $request): JsonResponse
    {
        return response()->rest_paginate(
            ProductIndexResource::collection(
                ProductRepository::make($request)
                    ->applyBasicQueries()
                    ->applyFilters()
                    ->applySorting()
                    ->simplePaginate()
            )
        );
    }

    /**
     * Products (show)
     */
    public function show(Product $product): JsonResponse
    {
        $product->load([
            'channels:id,name',
            'properties',
            'media' => function ($query) {
                $query->orderBy('order_column', 'asc');
            },
            'variations' => [
                'media',
                'properties',
            ],
            'reviews',
            'categories:id,name',
        ]);

        return response()->rest(new ProductShowResource($product));
    }
}
