import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import {
  BundleSale,
  BundleSalePostingCheck,
  BundleSaleSummary,
  BundleSaleTemplate,
  BundleSaleTemplateSummary,
  CreateBundleSaleRequest,
  UpdateBundleSaleRequest,
} from "@/types/bundles"

export const bundleApi = {
  listBundles(params: { page?: number; pageSize?: number; status?: string; customerId?: number; warehouseId?: number; q?: string } = {}): Promise<PagedResponse<BundleSaleSummary>> {
    return apiRequest<PagedResponse<BundleSaleSummary>>(`/bundle-sales${buildQuery(params)}`)
  },

  getBundle(bundleSaleId: number): Promise<BundleSale> {
    return apiRequest<BundleSale>(`/bundle-sales/${bundleSaleId}`)
  },

  createBundle(request: CreateBundleSaleRequest): Promise<BundleSale> {
    return apiRequest<BundleSale>("/bundle-sales", { method: "POST", body: request })
  },

  updateBundle(bundleSaleId: number, request: UpdateBundleSaleRequest): Promise<BundleSale> {
    return apiRequest<BundleSale>(`/bundle-sales/${bundleSaleId}`, { method: "PUT", body: request })
  },

  postBundle(bundleSaleId: number): Promise<BundleSale> {
    return apiRequest<BundleSale>(`/bundle-sales/${bundleSaleId}/post`, { method: "POST" })
  },

  cancelBundle(bundleSaleId: number): Promise<BundleSale> {
    return apiRequest<BundleSale>(`/bundle-sales/${bundleSaleId}/cancel`, { method: "POST" })
  },

  checkBundlePosting(bundleSaleId: number): Promise<BundleSalePostingCheck> {
    return apiRequest<BundleSalePostingCheck>(`/bundle-sales/${bundleSaleId}/posting-check`)
  },

  listTemplates(params: { page?: number; pageSize?: number; q?: string } = {}): Promise<PagedResponse<BundleSaleTemplateSummary>> {
    return apiRequest<PagedResponse<BundleSaleTemplateSummary>>(`/bundle-sales/templates${buildQuery(params)}`)
  },

  getTemplate(bundleSaleTemplateId: number): Promise<BundleSaleTemplate> {
    return apiRequest<BundleSaleTemplate>(`/bundle-sales/templates/${bundleSaleTemplateId}`)
  },
}
