import { LoginDatasourceInf } from "../../../infraestructure/datasources/login_datasource";
import { LoginFormValues } from "../../../infraestructure/entities/login_form_values";
import axios from "axios";
import { LoggedInUser } from "../../../infraestructure/entities/user";
import { LoggedInUserModel } from "../../models/prod/LoggedInUserModel";
import { UserRole } from "../../../constants/roles";
import { APIUrl } from "../../../constants/api_url";

export class LoginDatasourceProd implements LoginDatasourceInf{
    async getToken(form: LoginFormValues): Promise<LoggedInUser> {
        const {email, password} = form;
        const {data} = await axios.post<LoggedInUserModel>(
            APIUrl + "/admin/signin",
            {
                email: email,
                password: password,
            }
        );
        const user: LoggedInUser = {
            user: {
                email: data.email,
                name: data.name,
                role: data.role === UserRole.SUPERADMIN ? UserRole.SUPERADMIN : UserRole.ADMIN,
            },
            token: data.token,
        };
        
        return user;
    }
}