import type { GetTeamDetailsResult } from '@core/racing/application/use-cases/GetTeamDetailsUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { GetTeamDetailsOutputDTO } from '../dtos/GetTeamDetailsOutputDTO';

export class TeamDetailsPresenter implements UseCaseOutputPort<GetTeamDetailsResult> {
  private result: GetTeamDetailsOutputDTO | null = null;

  reset() {
    this.result = null;
  }

  present(result: GetTeamDetailsResult): void {
    this.result = {
      team: {
        id: result.team.id,
        name: result.team.name.toString(),
        tag: result.team.tag.toString(),
        description: result.team.description?.toString() || '',
        ownerId: result.team.ownerId.toString(),
        leagues: result.team.leagues?.map(l => l.toString()) || [],
        category: result.team.category,
        isRecruiting: result.team.isRecruiting,
        createdAt: result.team.createdAt.toDate().toISOString(),
      },
      membership: result.membership
        ? {
            role: result.membership.role === 'driver' ? 'member' : result.membership.role,
            joinedAt: result.membership.joinedAt.toISOString(),
            isActive: result.membership.status === 'active',
          }
        : null,
      canManage: result.canManage,
    };
  }

  getResponseModel(): GetTeamDetailsOutputDTO | null {
    return this.result;
  }
}