import {
  ITeamDetailsPresenter,
  TeamDetailsResultDTO,
  TeamDetailsViewModel,
} from '@core/racing/application/presenters/ITeamDetailsPresenter';

export class TeamDetailsPresenter implements ITeamDetailsPresenter {
  private result: TeamDetailsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: TeamDetailsResultDTO) {
    const { team, membership } = dto;

    const canManage =
      membership !== null &&
      (membership.role === 'owner' || membership.role === 'manager');

    this.result = {
      team: {
        id: team.id,
        name: team.name,
        tag: team.tag,
        description: team.description,
        ownerId: team.ownerId,
        leagues: team.leagues || [],
        createdAt: team.createdAt?.toISOString() || new Date().toISOString(),
      },
      membership: membership
        ? {
            role: membership.role as 'owner' | 'manager' | 'member',
            joinedAt: membership.joinedAt.toISOString(),
            isActive: membership.status === 'active',
          }
        : null,
      canManage,
    };
  }

  getViewModel(): TeamDetailsViewModel | null {
    return this.result;
  }

  get viewModel(): TeamDetailsViewModel {
    if (!this.result) throw new Error('Presenter not presented');
    return this.result;
  }
}