/**
 * Custom Domains API - Manage custom domains for businesses
 */

import apiClient from './client';
import { CustomDomain } from '../types';

/**
 * Get all custom domains for the current business
 */
export const getCustomDomains = async (): Promise<CustomDomain[]> => {
  const response = await apiClient.get<CustomDomain[]>('/api/business/domains/');
  return response.data;
};

/**
 * Add a new custom domain
 */
export const addCustomDomain = async (domain: string): Promise<CustomDomain> => {
  const response = await apiClient.post<CustomDomain>('/api/business/domains/', {
    domain: domain.toLowerCase().trim(),
  });
  return response.data;
};

/**
 * Delete a custom domain
 */
export const deleteCustomDomain = async (domainId: number): Promise<void> => {
  await apiClient.delete(`/api/business/domains/${domainId}/`);
};

/**
 * Verify a custom domain by checking DNS
 */
export const verifyCustomDomain = async (domainId: number): Promise<{ verified: boolean; message: string }> => {
  const response = await apiClient.post<{ verified: boolean; message: string }>(
    `/api/business/domains/${domainId}/verify/`
  );
  return response.data;
};

/**
 * Set a custom domain as the primary domain
 */
export const setPrimaryDomain = async (domainId: number): Promise<CustomDomain> => {
  const response = await apiClient.post<CustomDomain>(
    `/api/business/domains/${domainId}/set-primary/`
  );
  return response.data;
};
