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

const { deals } = endpoints

type GetUserDealsOptions = {
  q?: string
  page?: number
  limit?: number
}

export async function getUserDeals(userName: string, options: GetUserDealsOptions = {}) {
  const requestData = deals.dealsListRequestSchema.parse({
    q: options.q ?? "",
    page: options.page ?? 1,
    limit: options.limit ?? 20,
  })

  const { data } = await instance.get(`/deals/users/${userName}/deals`, {
    params: requestData,
  })

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