import apiClient from '@/lib/apiClient';

import {
  type GetProfileApiResponse,
  type SaveProfileApiResponse,
  type PhoneNumberApiResponse,
  type ConfirmPhoneNumberApiResponse,
  type SaveSettingsApiResponse,
  type DeleteSessionsApiResponse,
  type PasswordApiResponse,
  type SendEmailCodeApiResponse,
  type ConfirmEmailCodeApiResponse,
  type ChangeEmailApiResponse,
} from '../types/settingsApiType';
import { type InfoRowData } from '../types/settingsType';

export async function fetchProfile() {
  return apiClient.post<GetProfileApiResponse>('/Profile/GetProfile', {});
}

export async function saveProfile(payload: {
  data: InfoRowData;
  imageUrl: File | null;
}) {
  const { data, imageUrl } = payload;

  const formData = new FormData();
  formData.append('FirstName', data.firstName || '');
  formData.append('LastName', data.lastName || '');
  formData.append('NationalCode', data.nationalCode || '');
  formData.append('Gender', String(data.gender));
  formData.append('CountryCode', data.country || '');

  if (imageUrl) {
    formData.append('Image', imageUrl);
  }

  return apiClient.post<SaveProfileApiResponse>(
    '/Profile/SaveProfilePersonalInforamtion',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    },
  );
}

export async function sendVerificationCode(payload: { phoneNumber: string }) {
  return apiClient.post<PhoneNumberApiResponse>(
    '/Profile/SendVerfiyPhoneNumberCode',
    payload,
  );
}

export async function confirmPhoneNumberCode(payload: {
  phoneNumber: string;
  verifyCode: string;
}) {
  return apiClient.post<ConfirmPhoneNumberApiResponse>(
    '/Profile/ConfirmPhoneNumberChangeCode',
    payload,
  );
}

export async function changePhoneNumber(payload: { phoneNumber: string }) {
  return apiClient.post<PhoneNumberApiResponse>(
    '/Profile/ChangePhoneNumber',
    payload,
  );
}

export async function saveSettings(payload: {
  theme: number;
  calendarType: number;
  language: number;
}) {
  return apiClient.post<SaveSettingsApiResponse>(
    '/Profile/SaveSetting',
    payload,
  );
}

export async function deleteSessions(payload: { keys: string[] }) {
  return apiClient.post<DeleteSessionsApiResponse>(
    '/Profile/DeleteSessions',
    payload,
  );
}

export async function addPassword(payload: { newPassword: string }) {
  return apiClient.post<PasswordApiResponse>('/Profile/AddPassword', payload);
}

export async function changePassword(payload: {
  oldPassword: string;
  newPassword: string;
}) {
  return apiClient.post<PasswordApiResponse>(
    '/Profile/ChangePassword',
    payload,
  );
}

export async function sendEmailCode(payload: { email: string }) {
  return apiClient.post<SendEmailCodeApiResponse>(
    'Profile/SendEmailChangeCode',
    payload,
  );
}

export async function confirmEmailCode(payload: {
  email: string;
  verifyCode: string;
}) {
  return apiClient.post<ConfirmEmailCodeApiResponse>(
    'Profile/ConfirmEmailChangeCode',
    payload,
  );
}

export async function changeEmail(payload: { email: string }) {
  return apiClient.post<ChangeEmailApiResponse>('Profile/ChangeEmail', payload);
}
