import { apiSlice } from '.';
import URLS from './constants';

interface Phone {
  dialCode: number;
  number: number;
}

interface CompanyId {
  name: string;
  userType: string;
  id: string;
}

interface Company {
  id: CompanyId;
  role: string[];
}

interface SubCompany {
  role: string[];
}

interface User {
  phone: Phone;
  company: Company;
  subCompany: SubCompany;
  firstName: string;
  lastName: string;
  email: string;
  password: string;
  isEmailVerified: boolean;
  team: any[];
  status: string;
  isDeleted: boolean;
  memberType: string;
  joiningDate: string;
  id: string;
}

interface TokenInfo {
  token: string;
  expires: string;
}

interface Tokens {
  access: TokenInfo;
  refresh: TokenInfo;
}

interface LoginResponse {
  code: number;
  message: string;
  data: {
    user: User;
    tokens: Tokens;
  };
  success: boolean;
  error?: object;
}

interface ResetPasswordResponse {
  code: number;
  message: string;
  data: boolean;
  success: boolean;
  err: string;
}

interface ResetPasswordPayload {
  password: string;
  token: string;
}
interface ResendPasswordEmailResponse {
  code: number;
  message: string;
  data: boolean;
  success: boolean;
  err?: string;
}

export const authApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    login: builder.mutation<LoginResponse, { email: string; password: string }>({
      query: (credentials) => ({
        url: `/auth/login`,
        method: 'POST',
        body: credentials,
      }),
    }),

    resetPassword: builder.mutation<ResetPasswordResponse, ResetPasswordPayload>({
      query: ({ password, token }) => ({
        url: `/auth/reset-password?token=${token}`,
        method: 'POST',
        body: { password },
      }),
    }),
    resendPasswordEmail: builder.mutation<
      ResendPasswordEmailResponse,
      { userId: string; type?: string }
    >({
      query: ({ userId, type }) => ({
        url: `/auth/resend-password-email/${userId}${type ? `?type=${type}` : ''}`,
        method: 'POST',
      }),
    }),

    forgotPassword: builder.mutation<
      {
        code: number;
        message: string;
        data: boolean;
        success: boolean;
      },
      { email: string }
    >({
      query: (body) => ({
        url: `/auth/forgot-password`,
        method: 'POST',
        body,
      }),
    }),

  }),
});

export const {
  useLoginMutation,
  useResetPasswordMutation,
  useResendPasswordEmailMutation,
  useForgotPasswordMutation,
} = authApi;
