import { User } from "@/contexts/users/domain";
import { IUserContext } from "@/contexts/users/infrastructure/User.context";
import { ICollection, IListResponse_DTO, IListUsers_Response_DTO } from "@shared/contexts";

export interface IListUsersPresenter {
  map: (user: User, context: IUserContext) => IListUsers_Response_DTO;

  mapArray: (
    users: ICollection<User>,
    context: IUserContext,
    params: {
      page: number;
      limit: number;
    }
  ) => IListResponse_DTO<IListUsers_Response_DTO>;
}

export const listUsersPresenter: IListUsersPresenter = {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  map: (user: User, context: IUserContext): IListUsers_Response_DTO => {
    return {
      id: user.id.toString(),
      name: user.name.toString(),
      email: user.email.toString(),
      roles: user.getRoles().map((rol) => rol.toString()),
    };
  },

  mapArray: (
    users: ICollection<User>,
    context: IUserContext,
    params: {
      page: number;
      limit: number;
    }
  ): IListResponse_DTO<IListUsers_Response_DTO> => {
    const { page, limit } = params;

    const totalCount = users.totalCount ?? 0;
    const items = users.items.map((user: User) => listUsersPresenter.map(user, context));

    const result = {
      page,
      per_page: limit,
      total_pages: Math.ceil(totalCount / limit),
      total_items: totalCount,
      items,
    };

    return result;
  },
};
