import { apiRequest } from '../lib/api'
import type {
  CreateInvoiceItemTemplatePayload,
  CreateInvoicePayload,
  CreateInvoiceTemplatePayload,
  Invoice,
  InvoiceItemTemplate,
  InvoiceItemTemplatesResponse,
  InvoiceStatus,
  InvoiceTemplate,
  InvoiceTemplatesListResponse,
  InvoicesListResponse,
  PublicInvoice,
  UpdateInvoiceItemTemplatePayload,
  UpdateInvoicePayload,
  UpdateInvoiceTemplatePayload,
} from '../types/invoice'

export function listInvoiceItemTemplates(signal?: AbortSignal) {
  return apiRequest<InvoiceItemTemplatesResponse>('/invoice-item-templates', {
    auth: true,
    signal,
  })
}

export function createInvoiceItemTemplate(payload: CreateInvoiceItemTemplatePayload) {
  return apiRequest<InvoiceItemTemplate>('/invoice-item-templates', {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export function updateInvoiceItemTemplate(
  templateId: string,
  payload: UpdateInvoiceItemTemplatePayload,
) {
  return apiRequest<InvoiceItemTemplate>(`/invoice-item-templates/${templateId}`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export function deleteInvoiceItemTemplate(templateId: string) {
  return apiRequest<{ ok: boolean }>(`/invoice-item-templates/${templateId}`, {
    method: 'DELETE',
    auth: true,
  })
}

export function listInvoiceTemplates(signal?: AbortSignal) {
  return apiRequest<InvoiceTemplatesListResponse>('/invoice-templates', {
    auth: true,
    signal,
  })
}

export function getInvoiceTemplate(templateId: string, signal?: AbortSignal) {
  return apiRequest<InvoiceTemplate>(`/invoice-templates/${templateId}`, {
    auth: true,
    signal,
  })
}

export function createInvoiceTemplate(payload: CreateInvoiceTemplatePayload) {
  return apiRequest<InvoiceTemplate>('/invoice-templates', {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export function updateInvoiceTemplate(templateId: string, payload: UpdateInvoiceTemplatePayload) {
  return apiRequest<InvoiceTemplate>(`/invoice-templates/${templateId}`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export function deleteInvoiceTemplate(templateId: string) {
  return apiRequest<{ ok: boolean }>(`/invoice-templates/${templateId}`, {
    method: 'DELETE',
    auth: true,
  })
}

export interface ListBusinessInvoicesParams {
  page?: number
  pageSize?: number
  status?: InvoiceStatus
}

export function listBusinessInvoices(
  businessId: string,
  params: ListBusinessInvoicesParams = {},
  signal?: AbortSignal,
) {
  const search = new URLSearchParams()
  if (params.page) search.set('page', String(params.page))
  if (params.pageSize) search.set('pageSize', String(params.pageSize))
  if (params.status) search.set('status', params.status)
  const qs = search.toString()
  return apiRequest<InvoicesListResponse>(
    `/businesses/${businessId}/invoices${qs ? `?${qs}` : ''}`,
    { auth: true, signal },
  )
}

export function getBusinessInvoice(businessId: string, invoiceId: string, signal?: AbortSignal) {
  return apiRequest<Invoice>(`/businesses/${businessId}/invoices/${invoiceId}`, {
    auth: true,
    signal,
  })
}

export function createBusinessInvoice(businessId: string, payload: CreateInvoicePayload) {
  return apiRequest<Invoice>(`/businesses/${businessId}/invoices`, {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export function updateBusinessInvoice(
  businessId: string,
  invoiceId: string,
  payload: UpdateInvoicePayload,
) {
  return apiRequest<Invoice>(`/businesses/${businessId}/invoices/${invoiceId}`, {
    method: 'PUT',
    auth: true,
    body: payload,
  })
}

export function updateBusinessInvoiceStatus(
  businessId: string,
  invoiceId: string,
  payload: { status: InvoiceStatus; notes?: string },
) {
  return apiRequest<Invoice>(`/businesses/${businessId}/invoices/${invoiceId}`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export function deleteBusinessInvoice(businessId: string, invoiceId: string) {
  return apiRequest<{ ok: boolean }>(`/businesses/${businessId}/invoices/${invoiceId}`, {
    method: 'DELETE',
    auth: true,
  })
}

/** Public show-page payload (no auth). Lookup by opaque publicId. */
export function getPublicInvoice(publicId: string, signal?: AbortSignal) {
  return apiRequest<PublicInvoice>(`/public/invoices/${publicId}`, {
    auth: false,
    signal,
  })
}

/** Public approve (no auth). issued → approved. */
export function approvePublicInvoice(publicId: string) {
  return apiRequest<PublicInvoice>(`/public/invoices/${publicId}/approve`, {
    method: 'POST',
    auth: false,
  })
}
