import { api } from '../api'

export interface Contract {
  id: string
  contractNumber: string
  dealId: string
  deal?: any
  version?: number
  title: string
  type: string
  clientInfo: any
  companyInfo: any
  startDate: string
  endDate?: string
  value: number
  paymentTerms: any
  deliveryTerms: any
  terms: string
  status: string
  signedAt?: string
  documentUrl?: string
  createdAt: string
  updatedAt: string
}

export interface CreateContractData {
  dealId: string
  title: string
  type: string
  clientInfo: any
  companyInfo: any
  startDate: string
  endDate?: string
  value: number
  paymentTerms: any
  deliveryTerms: any
  terms: string
}

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

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

  create: async (data: CreateContractData): Promise<Contract> => {
    const response = await api.post('/crm/contracts', data)
    return response.data.data
  },

  update: async (id: string, data: Partial<CreateContractData>): Promise<Contract> => {
    const response = await api.put(`/crm/contracts/${id}`, data)
    return response.data.data
  },

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