/**
 * 发票管理 API
 */
import { request } from '../utils/request'
import type {
  Invoice,
  InvoiceTitle,
  InvoiceApplyParam,
  InvoiceParam,
  InvoiceStatus,
  InvoiceType,
} from '../types/invoice'

// ==================== 发票抬头 ====================

/** 获取发票抬头列表 */
export const listInvoiceTitles = () => {
  return request<{ list: InvoiceTitle[] }>({
    url: '/enterprise/invoice/titles',
    method: 'GET',
  })
}

/** 创建发票抬头 */
export const createInvoiceTitle = (data: Omit<InvoiceTitle, 'id' | 'createTime'>) => {
  return request<InvoiceTitle>({
    url: '/enterprise/invoice/titles',
    method: 'POST',
    data,
  })
}

/** 更新发票抬头 */
export const updateInvoiceTitle = (id: number, data: Partial<InvoiceTitle>) => {
  return request<InvoiceTitle>({
    url: `/enterprise/invoice/titles/${id}`,
    method: 'PUT',
    data,
  })
}

/** 删除发票抬头 */
export const deleteInvoiceTitle = (id: number) => {
  return request<void>({
    url: `/enterprise/invoice/titles/${id}`,
    method: 'DELETE',
  })
}

/** 设置默认发票抬头 */
export const setDefaultInvoiceTitle = (id: number) => {
  return request<void>({
    url: `/enterprise/invoice/titles/${id}/default`,
    method: 'POST',
  })
}

// ==================== 发票申请 ====================

/** 获取发票列表 */
export const pageInvoice = (params: InvoiceParam) => {
  return request<{ list: Invoice[]; total: number }>({
    url: '/enterprise/invoice/page',
    method: 'GET',
    params,
  })
}

/** 获取发票详情 */
export const getInvoice = (id: number) => {
  return request<Invoice>({
    url: `/enterprise/invoice/${id}`,
    method: 'GET',
  })
}

/** 申请发票 */
export const applyInvoice = (data: InvoiceApplyParam) => {
  return request<Invoice>({
    url: '/enterprise/invoice/apply',
    method: 'POST',
    data,
  })
}

/** 取消发票申请 */
export const cancelInvoice = (id: number) => {
  return request<void>({
    url: `/enterprise/invoice/${id}/cancel`,
    method: 'POST',
  })
}

/** 重新申请发票 */
export const reapplyInvoice = (id: number, data: Partial<InvoiceApplyParam>) => {
  return request<Invoice>({
    url: `/enterprise/invoice/${id}/reapply`,
    method: 'POST',
    data,
  })
}

/** 获取可开票金额 */
export const getInvoiceableAmount = () => {
  return request<{ amount: number }>({
    url: '/enterprise/invoice/amount',
    method: 'GET',
  })
}
