import { apiRequest } from '../lib/api'
import type {
  BusinessesListResponse,
  BusinessListItem,
  CreateBusinessPayload,
  MigrateFromOldPayload,
  MigrateFromOldResponse,
  PurgeBusinessDataPayload,
  PurgeBusinessDataResponse,
} from '../types/business'

export interface ListBusinessesParams {
  page?: number
  pageSize?: number
  name?: string
  domain?: string
  category?: string
}

export interface UpdateBusinessPayload {
  name?: string
  slug?: string
  oldBusinessId?: number | null
}

export interface AddDomainPayload {
  host: string
  isPrimary?: boolean
}

export interface DisableBusinessPayload {
  isActive: boolean
}

export interface UpdateDomainPayload {
  host: string
}

export async function listBusinesses(params: ListBusinessesParams, signal?: AbortSignal) {
  const q = new URLSearchParams()
  if (params.page !== undefined) q.set('page', String(params.page))
  if (params.pageSize !== undefined) q.set('pageSize', String(params.pageSize))
  if (params.name) q.set('name', params.name)
  if (params.domain) q.set('domain', params.domain)
  if (params.category) q.set('category', params.category)

  const query = q.toString()
  const path = `/businesses${query ? `?${query}` : ''}`
  return apiRequest<BusinessesListResponse>(path, { auth: true, signal })
}

export async function createBusiness(payload: CreateBusinessPayload) {
  return apiRequest<BusinessListItem>('/businesses', {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export async function updateBusiness(businessId: string, payload: UpdateBusinessPayload) {
  return apiRequest<BusinessListItem>(`/businesses/${businessId}`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export async function addBusinessDomain(businessId: string, payload: AddDomainPayload) {
  return apiRequest(`/businesses/${businessId}/domains`, {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export async function updateBusinessDomain(
  businessId: string,
  domainId: number,
  payload: UpdateDomainPayload,
) {
  return apiRequest(`/businesses/${businessId}/domains/${domainId}`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export async function setBusinessActive(businessId: string, payload: DisableBusinessPayload) {
  return apiRequest(`/businesses/${businessId}/disable`, {
    method: 'PATCH',
    auth: true,
    body: payload,
  })
}

export async function removeBusiness(businessId: string) {
  return apiRequest(`/businesses/${businessId}`, {
    method: 'DELETE',
    auth: true,
  })
}

export async function migrateBusinessFromOld(
  businessId: string,
  payload: MigrateFromOldPayload,
) {
  return apiRequest<MigrateFromOldResponse>(`/businesses/${businessId}/migrate-from-old`, {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export async function purgeBusinessData(
  businessId: string,
  payload: PurgeBusinessDataPayload,
) {
  return apiRequest<PurgeBusinessDataResponse>(`/businesses/${businessId}/purge-data`, {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export interface BusinessDetail {
  id: string
  name: string
  nameFa: string | null
  slug: string
  isActive: boolean
  oldBusinessId: string | null
}

export async function getBusiness(businessId: string, signal?: AbortSignal) {
  return apiRequest<BusinessDetail>(`/businesses/${businessId}`, {
    auth: true,
    signal,
  })
}

