import instance from "../axiosInstance"
import type { DealCard } from "../../models/deal/DealCard"
import { mapDealCardResponseToDeal } from "../../adapters/responses/dealCardAdapter"
import { endpoints } from "@shared/contracts"

const { deals } = endpoints

export async function searchDeals(
  query: string,
  page = 1
): Promise<DealCard[]> {
  const requestData = deals.dealsListRequestSchema.parse({
    q: query,
    page,
  })
  const { data } = await instance.get("/deals/", {
    params: requestData,
  })

  const response = deals.dealsListResponseSchema.parse(data)
  return response.results.map(mapDealCardResponseToDeal)
}
