import { api } from '../api'

export interface CostSheetItem {
  description?: string
  source?: string
  cost: number
  quantity: number
}

export interface CostSheet {
  id: string
  costSheetNumber: string
  dealId: string
  deal?: any
  version: number
  items: CostSheetItem[] | any
  totalCost: number
  suggestedPrice: number
  profitMargin: number
  status: string
  approvedBy?: string
  approvedAt?: string
  createdAt: string
  updatedAt: string
}

export interface CreateCostSheetData {
  dealId: string
  items: CostSheetItem[] | any[]
  totalCost: number
  suggestedPrice: number
  profitMargin: number
}

export const costSheetsAPI = {
  getByDeal: async (dealId: string): Promise<CostSheet[]> => {
    const response = await api.get(`/crm/deals/${dealId}/cost-sheets`)
    return response.data.data || []
  },

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

  create: async (data: CreateCostSheetData): Promise<CostSheet> => {
    const response = await api.post('/crm/cost-sheets', data)
    return response.data.data
  },

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

  reject: async (id: string): Promise<CostSheet> => {
    const response = await api.post(`/crm/cost-sheets/${id}/reject`)
    return response.data.data
  }
}
