import { ITeam, EntityType } from '../types/CalendarTypes';
import { IApiRepository } from './IApiRepository';

interface RawTeamData {
  id: string;
  name: string;
  resourceIds: string[];
  syncStatus?: string;
  [key: string]: unknown;
}

/**
 * MockTeamRepository - Loads team data from local JSON file
 */
export class MockTeamRepository implements IApiRepository<ITeam> {
  public readonly entityType: EntityType = 'Team';
  private readonly dataUrl = 'data/mock-teams.json';

  public async fetchAll(): Promise<ITeam[]> {
    try {
      const response = await fetch(this.dataUrl);

      if (!response.ok) {
        throw new Error(`Failed to load mock teams: ${response.status} ${response.statusText}`);
      }

      const rawData: RawTeamData[] = await response.json();
      return this.processTeamData(rawData);
    } catch (error) {
      console.error('Failed to load team data:', error);
      throw error;
    }
  }

  public async sendCreate(_team: ITeam): Promise<ITeam> {
    throw new Error('MockTeamRepository does not support sendCreate. Mock data is read-only.');
  }

  public async sendUpdate(_id: string, _updates: Partial<ITeam>): Promise<ITeam> {
    throw new Error('MockTeamRepository does not support sendUpdate. Mock data is read-only.');
  }

  public async sendDelete(_id: string): Promise<void> {
    throw new Error('MockTeamRepository does not support sendDelete. Mock data is read-only.');
  }

  private processTeamData(data: RawTeamData[]): ITeam[] {
    return data.map((team): ITeam => ({
      id: team.id,
      name: team.name,
      resourceIds: team.resourceIds,
      syncStatus: 'synced' as const
    }));
  }
}
