import { AuthenticatedUser, TabContext, Token } from "../../domain";
import { ILoginUserResponseDTO } from "../../dto";

export interface ILoginPresenter {
  toDTO: (data: {
    user: AuthenticatedUser;
    tabContext: TabContext;
    tokens: {
      accessToken: Token;
      refreshToken: Token;
    };
  }) => ILoginUserResponseDTO;
}

export const loginPresenter: ILoginPresenter = {
  toDTO: (data: {
    user: AuthenticatedUser;
    tabContext: TabContext;
    tokens: {
      accessToken: Token;
      refreshToken: Token;
    };
  }): ILoginUserResponseDTO => {
    const {
      user,
      tabContext,
      tokens: { accessToken, refreshToken },
    } = data;

    const userData = {
      id: user.id.toString(),
      username: user.username.toString(),
      email: user.email.toString(),
      //roles: user.getRoles().map((role) => role.toString()),
    };

    const tabContextData = {
      id: tabContext.id.toString(),
      tab_id: tabContext.tabId.toString(),
      user_id: tabContext.userId.toString(),
    };

    return {
      user: {
        id: userData.id,
        email: userData.email,
        username: userData.username,
        tab_id: tabContextData.tab_id,
      },
      tokens: {
        access_token: accessToken.toString(),
        refresh_token: refreshToken.toString(),
      },
    };
  },
};
