import axios from '@/network/axios';
import { apis } from '@/utils/apis/apis';
import addHeadersToAxiosInstance from '@/utils/helper/action/addHeadersToAxiosInstance';
import { SuccessResponse } from '@/utils/types/Responses';

export interface LoginResponseData {
  id: number;
  email: string;
  first_name: string;
  last_name: string;
  phone: string;
  brand_name: string;
  credit: number;
  requests_balance: number;
  search_balance: number;
  country: {
    id: string;
    name: string;
  };
  account_type_id: number;
  account_type: string;
  email_verified: boolean;
  is_activated: boolean;
  profile_id: number | null;
  roles: unknown[];
  permissions_routes_map: unknown[];
  reports: string;
  pptx_downloads: number;
  competitor_insights: number;
  competitor_analysis: number;
  is_paid_subscription: boolean;
  paid_subscription: null | string;
  owner: {
    is_owner: number;
    owner_id: number | null;
  };
  integrations: {
    zid_connect: boolean;
    salla_connect: boolean;
  };
  organization_type: string | null;
  organization_type_label: string | null;
  profile_image: string | null;
  token: string;
}

export const login = async (data: FormData) => {
  const response = await axios.post<SuccessResponse<LoginResponseData>>(
    apis.AUTH.LOGIN,
    data,
  );

  if (response) {
    const token = response.data?.data?.token;
    await addHeadersToAxiosInstance(token as string);
  }

  return response;
};
