<?php

namespace App\Services;

use App\Repositories\CategoryRepository;

class CategoryService extends BaseService
{
    private CategoryRepository $repository;

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

    /**
     * Get all categories by params
     * 
     * @throws Exception    
     * 
     * @return array
     */
    public function getAllCategories(array $params): array
    {
        $this->validateParams($params);

        $params['options'] = $this->normalizePagination(
            $params['options'] ?? []
        );

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

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

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

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

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