import api from './client'

export interface PricingTier {
  id: number
  category_key: string
  quality_level: string
  price_per_item: number
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export async function listPricingTiers(): Promise<PricingTier[]> {
  const res = await api.get<PricingTier[]>('/pricing')
  return res.data
}

export async function createPricingTier(data: {
  category_key: string
  quality_level: string
  price_per_item: number
  description?: string
  is_active?: boolean
}): Promise<PricingTier> {
  const res = await api.post<PricingTier>('/pricing', data)
  return res.data
}

export async function updatePricingTier(
  id: number,
  data: { category_key?: string; quality_level?: string; price_per_item?: number; description?: string; is_active?: boolean },
): Promise<PricingTier> {
  const res = await api.patch<PricingTier>(`/pricing/${id}`, data)
  return res.data
}

export async function deletePricingTier(id: number): Promise<void> {
  await api.delete(`/pricing/${id}`)
}

export interface PriceEstimateLine {
  product_id: string
  output_type_id: string | null
}

export interface PriceEstimate {
  total: number
  line_count: number
  breakdown: Array<{
    output_type_id: string | null
    product_id: string | null
    unit_price: number | null
  }>
  has_unpriced: boolean
}

export async function estimatePrice(lines: PriceEstimateLine[]): Promise<PriceEstimate> {
  const res = await api.post<PriceEstimate>('/pricing/estimate', { lines })
  return res.data
}
