import type { LeagueRosterMemberDTO } from '@/lib/types/generated/LeagueRosterMemberDTO';
import type { LeagueRosterJoinRequestDTO } from '@/lib/types/generated/LeagueRosterJoinRequestDTO';
import type { LeagueRosterAdminViewData, RosterMemberData, JoinRequestData } from '@/lib/view-data/LeagueRosterAdminViewData';
import { DateDisplay } from '@/lib/display-objects/DateDisplay';

/**
 * LeagueRosterAdminViewDataBuilder
 *
 * Transforms API DTOs into LeagueRosterAdminViewData for server-side rendering.
 * Deterministic; side-effect free; no HTTP calls.
 */
export class LeagueRosterAdminViewDataBuilder {
  static build(input: {
    leagueId: string;
    members: LeagueRosterMemberDTO[];
    joinRequests: LeagueRosterJoinRequestDTO[];
  }): LeagueRosterAdminViewData {
    const { leagueId, members, joinRequests } = input;
    
    // Transform members
    const rosterMembers: RosterMemberData[] = members.map(member => ({
      driverId: member.driverId,
      driver: {
        id: member.driverId,
        name: member.driver?.name || 'Unknown Driver',
      },
      role: member.role,
      joinedAt: member.joinedAt,
      formattedJoinedAt: DateDisplay.formatShort(member.joinedAt),
    }));
    
    // Transform join requests
    const requests: JoinRequestData[] = joinRequests.map(req => ({
      id: req.id,
      driver: {
        id: req.driverId,
        name: 'Unknown Driver', // driver field is unknown type
      },
      requestedAt: req.requestedAt,
      formattedRequestedAt: DateDisplay.formatShort(req.requestedAt),
      message: req.message,
    }));
    
    return {
      leagueId,
      members: rosterMembers,
      joinRequests: requests,
    };
  }
}