
import { api } from './api-client';

export const AuthAPI = {
  register: (payload: { first_name: string; last_name: string; email: string; password: string }, clientId?: string | number) =>
    api.post('/customer/register', payload, {
      params: {
        clientId: clientId, // Pass clientId as query param
      }
    }).then((res) => res.data),

  login: (email: string, password: string) => api.post('/customer/login', { email, password }).then((res) => res.data),

  getUser: async () => {
    const userId = localStorage.getItem('user_id');
    if (!userId) return Promise.resolve(null);
    const res = await api.get(`/customer/client_personal_info/${userId}`);
    return res.data.results;
  },
  getNewClientPersonalInfo: async (customerId: string | number) => {
    const res = await api.get(`customer/lead_profile/${customerId}`);
    return res.data.results;
  },
  updateUser: async (payload: {
    first_name?: string | null;
    last_name?: string | null;
    email?: string | null;
    phone_mobile?: string | null;
    ssn?: string | null;
    dob?: string | null;
    address_one?: string | null;
    address_two?: string | null;
    city?: string | null;
    state?: string | null;
    zipcode?: string | null;
    mailing_address_one?: string | null;
    mailing_address_two?: string | null;
    mailing_city?: string | null;
    mailing_state?: string | null;
    mailing_zipcode?: string | null;
  }) => {
    const userId = localStorage.getItem('user_id');
    if (!userId) return Promise.resolve(null);

    // Send POST with payload body
    const res = await api.post(`/customer/update_personal_info/${userId}`, payload,);

    return res.data;
  },
  updateClientPersonalInfo: async (payload: {
    first_name: string | null;
    last_name: string | null;
    email: string | null;
    phone_mobile: string | null;
    ssn: string | null;
    dob: string | null;
    address_one: string | null;
    address_two: string | null;
    city: string | null;
    state: string | null;
    zipcode: string | null;
    mailing_address_one: string | null;
    mailing_address_two: string | null;
    mailing_city: string | null;
    mailing_state: string | null;
    mailing_zipcode: string | null;
  }) => {
    const userId = localStorage.getItem('user_id');
    if (!userId) return Promise.resolve(null);

    // Send POST with payload body
    const res = await api.post(`/customer/update_client_personal_info/${userId}`, payload);

    return res.data;
  },
  updateAddress: async (payload: {
    address_one?: string | null;
    address_two?: string | null;
    city?: string | null;
    state?: string | null;
    zipcode?: string | null;
    ssn: string | null;
    dob: string | null;
    mailing_address_one?: string | null;
    mailing_address_two?: string | null;
    mailing_city?: string | null;
    mailing_state?: string | null;
    mailing_zipcode?: string | null;
  }) => {
    const userId = localStorage.getItem('user_id');
    if (!userId) return Promise.resolve(null);

    const res = await api.post(`/customer/update_address_info/${userId}`, payload);

    return res.data;
  },

  logout: () => api.post('/customer/logout'),
};
