import type { GetDriverTeamResult } from '@core/racing/application/use-cases/GetDriverTeamUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { GetDriverTeamOutputDTO } from '../dtos/GetDriverTeamOutputDTO';

export class DriverTeamPresenter implements UseCaseOutputPort<GetDriverTeamResult> {
  private result: GetDriverTeamOutputDTO | null = null;

  reset() {
    this.result = null;
  }

  present(result: GetDriverTeamResult): void {
    const isOwner = result.team.ownerId.toString() === result.driverId;
    const canManage = isOwner || result.membership.role === 'owner' || result.membership.role === 'manager';

    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()) || [],
        isRecruiting: result.team.isRecruiting,
        createdAt: result.team.createdAt.toDate().toISOString(),
      },
      membership: {
        role: result.membership.role === 'driver' ? 'member' : result.membership.role,
        joinedAt: result.membership.joinedAt.toISOString(),
        isActive: result.membership.status === 'active',
      },
      isOwner,
      canManage,
    };
  }

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