/**
 * AllTeamsPresenter - Pure data transformer
 * Transforms API response to view model without DI dependencies.
 */

import { apiClient, type AllTeamsViewModel as ApiAllTeamsViewModel } from '@/lib/apiClient';

export interface TeamListItemViewModel {
  id: string;
  name: string;
  tag?: string | undefined;
  description?: string | undefined;
  memberCount: number;
  logoUrl?: string | undefined;
  rating?: number | undefined;
}

export interface AllTeamsViewModel {
  teams: TeamListItemViewModel[];
  totalCount: number;
}

export interface IAllTeamsPresenter {
  reset(): void;
  getViewModel(): AllTeamsViewModel | null;
}

/**
 * Transform API response to view model
 */
function transformApiResponse(apiResponse: ApiAllTeamsViewModel): AllTeamsViewModel {
  const teamItems: TeamListItemViewModel[] = apiResponse.teams.map((team) => {
    const viewModel: TeamListItemViewModel = {
      id: team.id,
      name: team.name,
      memberCount: team.memberCount ?? 0,
    };

    if (team.logoUrl) {
      viewModel.logoUrl = team.logoUrl;
    }
    if (team.rating) {
      viewModel.rating = team.rating;
    }

    return viewModel;
  });

  return {
    teams: teamItems,
    totalCount: teamItems.length,
  };
}

export class AllTeamsPresenter implements IAllTeamsPresenter {
  private viewModel: AllTeamsViewModel | null = null;

  reset(): void {
    this.viewModel = null;
  }

  async fetchAndPresent(): Promise<void> {
    const apiResponse = await apiClient.teams.getAll();
    this.viewModel = transformApiResponse(apiResponse);
  }

  getViewModel(): AllTeamsViewModel | null {
    return this.viewModel;
  }
}

/**
 * Convenience function to fetch and transform all teams
 */
export async function fetchAllTeams(): Promise<AllTeamsViewModel> {
  const apiResponse = await apiClient.teams.getAll();
  return transformApiResponse(apiResponse);
}