/**
 * Domains API - NameSilo Integration for Domain Registration
 */

import apiClient from './client';

// Types
export interface DomainAvailability {
  domain: string;
  available: boolean;
  price: number | null;
  premium: boolean;
  premium_price: number | null;
}

export interface DomainPrice {
  tld: string;
  registration: number;
  renewal: number;
  transfer: number;
}

export interface RegistrantContact {
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  address: string;
  city: string;
  state: string;
  zip_code: string;
  country: string;
}

export interface DomainRegisterRequest {
  domain: string;
  years: number;
  whois_privacy: boolean;
  auto_renew: boolean;
  nameservers?: string[];
  contact: RegistrantContact;
  auto_configure: boolean;
}

export interface DomainRegistration {
  id: number;
  domain: string;
  status: 'pending' | 'active' | 'expired' | 'transferred' | 'failed';
  registered_at: string | null;
  expires_at: string | null;
  auto_renew: boolean;
  whois_privacy: boolean;
  purchase_price: number | null;
  renewal_price: number | null;
  nameservers: string[];
  days_until_expiry: number | null;
  is_expiring_soon: boolean;
  created_at: string;
  // Detail fields
  registrant_first_name?: string;
  registrant_last_name?: string;
  registrant_email?: string;
}

export interface DomainSearchHistory {
  id: number;
  searched_domain: string;
  was_available: boolean;
  price: number | null;
  searched_at: string;
}

// API Functions

/**
 * Search for domain availability
 */
export const searchDomains = async (
  query: string,
  tlds: string[] = ['.com', '.net', '.org']
): Promise<DomainAvailability[]> => {
  const response = await apiClient.post<DomainAvailability[]>('/domains/search/search/', {
    query,
    tlds,
  });
  return response.data;
};

/**
 * Get TLD pricing
 */
export const getDomainPrices = async (): Promise<DomainPrice[]> => {
  const response = await apiClient.get<DomainPrice[]>('/domains/search/prices/');
  return response.data;
};

/**
 * Register a new domain
 */
export const registerDomain = async (
  data: DomainRegisterRequest
): Promise<DomainRegistration> => {
  const response = await apiClient.post<DomainRegistration>('/domains/search/register/', data);
  return response.data;
};

/**
 * Get all registered domains for current business
 */
export const getRegisteredDomains = async (): Promise<DomainRegistration[]> => {
  const response = await apiClient.get<DomainRegistration[]>('/domains/registrations/');
  return response.data;
};

/**
 * Get a single domain registration
 */
export const getDomainRegistration = async (id: number): Promise<DomainRegistration> => {
  const response = await apiClient.get<DomainRegistration>(`/domains/registrations/${id}/`);
  return response.data;
};

/**
 * Update nameservers for a domain
 */
export const updateNameservers = async (
  id: number,
  nameservers: string[]
): Promise<DomainRegistration> => {
  const response = await apiClient.post<DomainRegistration>(
    `/domains/registrations/${id}/update_nameservers/`,
    { nameservers }
  );
  return response.data;
};

/**
 * Toggle auto-renewal for a domain
 */
export const toggleAutoRenew = async (
  id: number,
  autoRenew: boolean
): Promise<DomainRegistration> => {
  const response = await apiClient.post<DomainRegistration>(
    `/domains/registrations/${id}/toggle_auto_renew/`,
    { auto_renew: autoRenew }
  );
  return response.data;
};

/**
 * Renew a domain
 */
export const renewDomain = async (
  id: number,
  years: number = 1
): Promise<DomainRegistration> => {
  const response = await apiClient.post<DomainRegistration>(
    `/domains/registrations/${id}/renew/`,
    { years }
  );
  return response.data;
};

/**
 * Sync domain info from NameSilo
 */
export const syncDomain = async (id: number): Promise<DomainRegistration> => {
  const response = await apiClient.post<DomainRegistration>(
    `/domains/registrations/${id}/sync/`
  );
  return response.data;
};

/**
 * Get domain search history
 */
export const getSearchHistory = async (): Promise<DomainSearchHistory[]> => {
  const response = await apiClient.get<DomainSearchHistory[]>('/domains/history/');
  return response.data;
};
