import axiosInstance from '@/utills/axiosInstance';
import { removeEmptyKeys } from '@/utills/helper';

// Numbers saved in our DB (whatsapp_phonenumbers), paged + filtered. Does not call Meta.
export async function getDbWhatsAppPhoneNumbers(params?: any): Promise<any> {
  const filterParam = params ? removeEmptyKeys(params) : undefined;
  return await axiosInstance.get('/api/v1/whatsapp/phone-numbers', { params: filterParam });
}

// Pulls the numbers from the WhatsApp Cloud API into our DB (adds missing, updates changed) and returns
// the refreshed DB list for the given page/filters plus a summary.
export async function syncWhatsAppPhoneNumbers(params?: any): Promise<any> {
  const filterParam = params ? removeEmptyKeys(params) : undefined;
  return await axiosInstance.post('/api/v1/whatsapp/phone-numbers/sync', {}, { params: filterParam });
}

// Numbers stored in the DB; `id` is what users.whatsapp_number_id references.
// Returns only CONNECTED, not-deregistered numbers; includeId keeps the user's current number.
export async function getAvailableWhatsAppPhoneNumbers(includeId?: number | null): Promise<any> {
  return await axiosInstance.get('/api/v1/whatsapp/phone-numbers/available', {
    params: includeId ? { include_id: includeId } : undefined,
  });
}

export async function addWhatsAppPhoneNumber(data: {
  cc: string;
  phone_number: string;
  verified_name: string;
  description?: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/add', data);
}

// Bulk-add numbers to the WhatsApp Business Account from a .csv/.xlsx/.xls file (multipart field "file").
export async function importWhatsAppPhoneNumbers(formData: FormData): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/import', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
}

export async function requestVerificationCode(data: {
  phone_number_id: string;
  code_method: 'SMS' | 'VOICE';
  locale: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/request-code', data);
}

export async function verifyPhoneNumberCode(data: {
  phone_number_id: string;
  code: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/verify-code', data);
}

export async function registerPhoneNumber(data: {
  phone_number_id: string;
  pin?: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/register', data);
}

export async function changePhoneNumberPin(data: {
  phone_number_id: string;
  pin: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/change-pin', data);
}

// Meta's deregister: the number stops working on the Cloud API until it is registered again (super admin only)
export async function deregisterPhoneNumber(phoneNumberId: string): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/deregister', {
    phone_number_id: phoneNumberId,
  });
}

export async function updatePhoneNumberProfile(data: {
  phone_number_id: string;
  verified_name?: string;
  description?: string;
}): Promise<any> {
  return await axiosInstance.post('/api/v1/whatsapp/phone-number/profile', data);
}

