import { api } from '../api'

export interface QuoteItem {
  description: string
  quantity: number
  unitPrice: number
  total: number
}

export interface Quote {
  id: string
  quoteNumber: string
  dealId: string
  deal?: any
  version: number
  items: QuoteItem[] | any
  subtotal: number
  discountType?: string
  discountValue?: number
  taxRate: number
  taxAmount: number
  total: number
  validUntil: string
  paymentTerms?: string
  deliveryTerms?: string
  notes?: string
  status: string
  sentAt?: string
  viewedAt?: string
  approvedBy?: string
  approvedAt?: string
  createdAt: string
  updatedAt: string
}

export interface CreateQuoteData {
  dealId: string
  items: QuoteItem[] | any[]
  subtotal: number
  taxRate: number
  taxAmount: number
  total: number
  validUntil: string
  paymentTerms?: string
  deliveryTerms?: string
  notes?: string
}

export const quotesAPI = {
  getByDeal: async (dealId: string): Promise<Quote[]> => {
    const response = await api.get(`/crm/deals/${dealId}/quotes`)
    return response.data.data || []
  },

  getById: async (id: string): Promise<Quote> => {
    const response = await api.get(`/crm/quotes/${id}`)
    return response.data.data
  },

  create: async (data: CreateQuoteData): Promise<Quote> => {
    const response = await api.post('/crm/quotes', data)
    return response.data.data
  },

  approve: async (id: string): Promise<Quote> => {
    const response = await api.post(`/crm/quotes/${id}/approve`)
    return response.data.data
  },

  send: async (id: string): Promise<Quote> => {
    const response = await api.post(`/crm/quotes/${id}/send`)
    return response.data.data
  }
}
