import { api } from "@/lib/api"
import type {
  PurchaseTopupPayload,
  TopupCredeitsBalanceResponse,
  TopupPlan,
  TopupPlanGetSecretResponse,
  UserTopupTransactionsResponse,
  CreditsUsageResponse,
  CreditRatesResponse
} from "@/types/topups"

export const getTopupPlans = ["topups", "list"]
export const getUserTopupCreditsBalance = ["topups", "balance"]
export const getUserTopUpTransactions = ["topups", "transactions"]
export const getTopUpCreditsUsageStats = (params: { salonId: string; page?: number; limit?: number }) => ["topups", "credits-usage", params]
export const getCreditRates = ["topups", "credit-rates"]

export async function fetchTopupPlans(): Promise<TopupPlan[]> {
  const { data } = await api.get("/topups")
  return data
}

export async function purchaseTopup(payload: PurchaseTopupPayload) {
  const { data } = await api.post<TopupPlanGetSecretResponse>("/topups/pay", payload)
  return data
}

export async function fetchUserTopupCreditsBalance() {
  const { data } = await api.get<TopupCredeitsBalanceResponse>("/topups/credits")
  return data
}

export async function fetchUserTopUpTRansactions() {
  const { data } = await api.get<UserTopupTransactionsResponse>("/topups/history")
  return data
}

export async function fetchTopUpCreditsUsageStats(params: { page?: number; limit?: number }): Promise<CreditsUsageResponse> {
  const { data } = await api.get<CreditsUsageResponse>("/topups/salon-credit-usage", { params })
  return data
}

export async function fetchCreditRates(): Promise<CreditRatesResponse> {
  const { data } = await api.get<CreditRatesResponse>("/topups/credit-rates")
  return data
}
