import { ISession } from "@models/Session.model";
import ApiRequest from "./Api.Service";
import { ApiVoidResult } from "@core/Models/Api.VoidResult.type";
import { CheckUserPhoneParams, RecoveryAccountParams } from "@core/Models/User.model";

/**
 * Logs in a user with the given email and password.
 * @param {string} email - The user's email.
 * @param {string} password - The user's password.
 * @returns {Promise<ISession>} - The session object containing the user's session details.
 */
export async function login(email: string, password: string): Promise<ISession> {
    // Send a POST request to the "users/login" endpoint with the email and password
    return ApiRequest.post<ISession>("users/login", {
        email: email,
        password: password,
    });
}

/**
 * Logs in a user using a token.
 *
 * @param {string} token - The token to authenticate the user.
 * @returns {Promise<ISession>} - A promise that resolves to a session object if the login is successful, or null if it fails.
 */
export async function loginByToken(token: string): Promise<ISession> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    return ApiRequest.post<ISession>("users/loginbytoken", {
        token: token,
    });
}

export async function checkUserPhone(data: CheckUserPhoneParams): Promise<boolean> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    return ApiRequest.post<boolean>("users/check-phone", data);
}

export async function loginByGoogle(token: string): Promise<ISession> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    return ApiRequest.post<ISession>("users/loginbygoogle", {
        token: token,
    });
}

export async function loginByAndroid(token: string): Promise<ISession> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    return ApiRequest.post<ISession>("users/loginbyandroid", {
        token: token,
    });
}

export async function loginAndVerificate(
    email: string,
    password: string,
    verificationCode: string
): Promise<ISession> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    return ApiRequest.post<ISession>("users/verificate", {
        email,
        password,
        verificationCode,
    });
}

export async function sendRecoveryCode(email: string): Promise<ApiVoidResult> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    const data: RecoveryAccountParams = {
        email,
        sendRecoveryCode: true,
    };

    return ApiRequest.post<ApiVoidResult>("users/recovery", data);
}

export async function recoveryAccountWithCode(
    email: string,
    newPassword: string,
    recoveryCode: string
): Promise<ApiVoidResult> {
    // Make a POST request to the "users/loginbytoken" endpoint with the provided token
    const data: RecoveryAccountParams = {
        email,
        newPassword,
        recoveryCode: recoveryCode,
        sendRecoveryCode: false,
    };

    return ApiRequest.post<ApiVoidResult>("users/recovery", data);
}
