import { request } from '../httpClient.ts';
import { serviceItemSchema, servicePageSchema, type ServiceItem, type ServicePageResponse } from './types.ts';

export interface ListServicesParams {
  limit?: number;
  page?: number;
  q?: string;
  category?: string;
  minPrice?: number;
  maxPrice?: number;
}

const SERVICES_ENDPOINT = '/api/v1/services';

const buildQuery = (params?: ListServicesParams) => {
  if (!params) {
    return undefined;
  }

  const { limit, page, q, category, minPrice, maxPrice } = params;

  return {
    limit,
    page,
    q,
    category,
    minPrice,
    maxPrice,
  };
};

export const servicesApi = {
  async list(params?: ListServicesParams): Promise<ServicePageResponse> {
    return request(SERVICES_ENDPOINT, servicePageSchema, {
      query: buildQuery(params),
    });
  },
  async get(slug: string | number): Promise<ServiceItem> {
    return request(`${SERVICES_ENDPOINT}/${slug}`, serviceItemSchema);
  },
};
