<?php

namespace App\Services;

use App\Repositories\ProductRepository;

class ProductService extends BaseService
{
    private ProductRepository $repository;

    public function __construct()
    {
        $this->repository = new ProductRepository();
    }

    /**
     * Get all products by params
     * 
     * @throws Exception
     * 
     * @return array
     * 
     * @param array $params
     */
    public function getAllProducts(array $params): array
    {
        $this->validateParams($params);
        
        $params['options'] = $this->normalizePagination(
            $params['options'] ?? []
        );

        return  $this->repository->getAllProducts($params);
    }

    /**
     * Get product by id
     * 
     * @throws Exception
     * 
     * @return array|null
     * 
     * @param int $id
     */
    public function getById(int $id): ?array
    {
        if ($id <= 0) {
            throw new \InvalidArgumentException('INVALID_PRODUCT_ID');
        }

        $product = $this->repository->getById($id);

        if (!$product) {
            throw new \Exception('PRODUCT_NOT_FOUND');
        }

        if (!$product['is_active']) {
            throw new \Exception('PRODUCT_INACTIVE');
        }

        return $product;
    }
}
