import { apiRequest } from '../lib/api' import { getActiveBusinessId } from '../lib/businessContext' import type { BusinessPrimaryColorId } from '../utils/businessPrimaryColors' export interface BrandingSettings { primaryColor: BusinessPrimaryColorId defaultLocale?: 'en' | 'fa' } export interface DashboardSettings { comments: { autoApprove: boolean } expertReviews: { autoApprove: boolean } } export interface OrderProcessStep { id: string label: string labelFa: string color: string } export type PaymentGatewayId = | 'mellat' | 'sep' | 'snappay' | 'digipay' | 'zarinpal' export interface MellatGatewaySettings { enabled: boolean terminalId: string username: string /** Always empty in API responses; use passwordSet. */ password: string passwordSet?: boolean } export interface ZarinpalGatewaySettings { enabled: boolean merchantId: string sandbox: boolean } export interface StubGatewaySettings { enabled: boolean } export interface EPaymentSettings { enabled: boolean defaultGateway: PaymentGatewayId | null gateways: { mellat: MellatGatewaySettings sep: StubGatewaySettings snappay: StubGatewaySettings digipay: StubGatewaySettings zarinpal: ZarinpalGatewaySettings } } export interface StoreSettings { onlineSellEnabled: boolean orderProcessSteps: OrderProcessStep[] ePayment: EPaymentSettings } export const DEFAULT_EPAYMENT_SETTINGS: EPaymentSettings = { enabled: false, defaultGateway: null, gateways: { mellat: { enabled: false, terminalId: '', username: '', password: '', passwordSet: false, }, sep: { enabled: false }, snappay: { enabled: false }, digipay: { enabled: false }, zarinpal: { enabled: false, merchantId: '', sandbox: false, }, }, } export interface ModulesSettings { enabled: string[] } export type SpecialProductsSource = 'product' | 'store_item' export interface WebsiteSitemapConfig { baseUrl?: string | null staticPages?: Array<{ path: string changefreq?: string priority?: number }> templates?: Record syncedAt?: string | null } export interface WebsiteSettings { specialProductsSource: SpecialProductsSource sitemapConfig?: WebsiteSitemapConfig | null } export interface BusinessSettings { branding: BrandingSettings dashboard: DashboardSettings store: StoreSettings modules?: ModulesSettings website?: WebsiteSettings } export interface SettingsResponse { businessId: string settings: BusinessSettings } export const DEFAULT_ORDER_PROCESS_STEPS: OrderProcessStep[] = [ { id: 'processing', label: 'Under processing', labelFa: 'در حال پردازش', color: '#3B82F6', }, { id: 'ready-for-shipping', label: 'Ready for shipping', labelFa: 'آماده ارسال', color: '#F59E0B', }, { id: 'shipped', label: 'Shipped', labelFa: 'ارسال‌شده', color: '#8B5CF6', }, { id: 'delivered', label: 'Delivered', labelFa: 'تحویل‌شده', color: '#22C55E', }, ] function settingsPath() { const businessId = getActiveBusinessId() if (!businessId) { throw new Error('No active business selected. Please sign in again.') } return `/businesses/${businessId}/settings` } export async function getSettings(signal?: AbortSignal) { return apiRequest(settingsPath(), { auth: true, signal }) } export async function updateDashboardSettings( dashboard: Partial, ) { return apiRequest(settingsPath(), { method: 'PATCH', auth: true, body: { dashboard }, }) } export async function updateStoreSettings(store: Partial) { return apiRequest(settingsPath(), { method: 'PATCH', auth: true, body: { store }, }) } export async function updateWebsiteSettings(website: Partial) { return apiRequest(settingsPath(), { method: 'PATCH', auth: true, body: { website }, }) }