import { apiRequest } from '../lib/api'
import type { BusinessPrimaryColorId } from '../utils/businessPrimaryColors'

export type DashboardLocale = 'en' | 'fa'

export interface BrandingSettings {
  primaryColor: BusinessPrimaryColorId
  defaultLocale: DashboardLocale
}

export interface BusinessSettings {
  branding: BrandingSettings
  dashboard: {
    comments: { autoApprove: boolean }
    expertReviews: { autoApprove: boolean }
  }
  store: {
    onlineSellEnabled: boolean
    orderProcessSteps: Array<{ id: string; label: string; color: string }>
  }
}

export interface BusinessSettingsResponse {
  businessId: string
  settings: BusinessSettings
}

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

export async function updateBusinessPrimaryColor(
  businessId: string,
  primaryColor: BusinessPrimaryColorId,
) {
  return apiRequest<BusinessSettingsResponse>(`/businesses/${businessId}/settings`, {
    method: 'PATCH',
    auth: true,
    body: {
      branding: { primaryColor },
    },
  })
}

export async function updateBusinessDefaultLocale(
  businessId: string,
  defaultLocale: DashboardLocale,
) {
  return apiRequest<BusinessSettingsResponse>(`/businesses/${businessId}/settings`, {
    method: 'PATCH',
    auth: true,
    body: {
      branding: { defaultLocale },
    },
  })
}

export async function updateBusinessBranding(
  businessId: string,
  branding: Partial<BrandingSettings>,
) {
  return apiRequest<BusinessSettingsResponse>(`/businesses/${businessId}/settings`, {
    method: 'PATCH',
    auth: true,
    body: { branding },
  })
}
