import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { GetTeamMembersResult } from '@core/racing/application/use-cases/GetTeamMembersUseCase';
import type { GetTeamMembersOutputDTO } from '../dtos/GetTeamMembersOutputDTO';

export class TeamMembersPresenter implements UseCaseOutputPort<GetTeamMembersResult> {
  private result: GetTeamMembersOutputDTO | null = null;

  reset() {
    this.result = null;
  }

  present(result: GetTeamMembersResult): void {
    const members = result.members
      .filter(member => member.driver !== null)
      .map(member => ({
        driverId: member.membership.driverId,
        driverName: member.driver!.name.toString(),
        role: (member.membership.role === 'driver' ? 'member' : member.membership.role) as 'owner' | 'manager' | 'member',
        joinedAt: member.membership.joinedAt.toISOString(),
        isActive: member.membership.status === 'active',
        avatarUrl: '', // TODO: get avatar
      }));

    const ownerCount = result.members.filter(m => m.membership.role === 'owner').length;
    const managerCount = result.members.filter(m => m.membership.role === 'manager').length;
    const memberCount = result.members.filter(m => m.membership.role === 'driver').length;

    this.result = {
      members,
      totalCount: members.length,
      ownerCount,
      managerCount,
      memberCount,
    };
  }

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