import { LeaguesApiClient } from '../../../../apps/website/lib/api/leagues/LeaguesApiClient';
import { ApiError } from '../../../../apps/website/lib/api/base/ApiError';
import type { Logger } from '../../../../apps/website/lib/interfaces/Logger';
import type { ErrorReporter } from '../../../../apps/website/lib/interfaces/ErrorReporter';

/**
 * Mock LeaguesApiClient for testing
 * Allows controlled responses without making actual HTTP calls
 */
export class MockLeaguesApiClient extends LeaguesApiClient {
  private mockResponses: Map<string, any> = new Map();
  private mockErrors: Map<string, ApiError> = new Map();

  constructor(
    baseUrl: string = 'http://localhost:3001',
    errorReporter: ErrorReporter = {
      report: () => {},
    } as any,
    logger: Logger = {
      info: () => {},
      warn: () => {},
      error: () => {},
    } as any
  ) {
    super(baseUrl, errorReporter, logger);
  }

  /**
   * Set a mock response for a specific endpoint
   */
  setMockResponse(endpoint: string, response: any): void {
    this.mockResponses.set(endpoint, response);
  }

  /**
   * Set a mock error for a specific endpoint
   */
  setMockError(endpoint: string, error: ApiError): void {
    this.mockErrors.set(endpoint, error);
  }

  /**
   * Clear all mock responses and errors
   */
  clearMocks(): void {
    this.mockResponses.clear();
    this.mockErrors.clear();
  }

  /**
   * Override getAllWithCapacityAndScoring to return mock data
   */
  async getAllWithCapacityAndScoring(): Promise<any> {
    const endpoint = '/leagues/all-with-capacity-and-scoring';
    
    if (this.mockErrors.has(endpoint)) {
      throw this.mockErrors.get(endpoint);
    }
    
    if (this.mockResponses.has(endpoint)) {
      return this.mockResponses.get(endpoint);
    }
    
    // Default mock response
    return {
      leagues: [
        {
          id: 'league-1',
          name: 'Test League',
          description: 'A test league',
          ownerId: 'driver-1',
          createdAt: new Date().toISOString(),
          usedSlots: 5,
          settings: {
            maxDrivers: 10,
          },
          scoring: {
            gameId: 'game-1',
            gameName: 'Test Game',
            primaryChampionshipType: 'driver',
            scoringPresetId: 'preset-1',
            scoringPresetName: 'Test Preset',
            dropPolicySummary: 'No drops',
            scoringPatternSummary: 'Standard scoring',
          },
        },
      ],
      totalCount: 1,
    };
  }

  /**
   * Override getMemberships to return mock data
   */
  async getMemberships(leagueId: string): Promise<any> {
    const endpoint = `/leagues/${leagueId}/memberships`;
    
    if (this.mockErrors.has(endpoint)) {
      throw this.mockErrors.get(endpoint);
    }
    
    if (this.mockResponses.has(endpoint)) {
      return this.mockResponses.get(endpoint);
    }
    
    // Default mock response
    return {
      members: [
        {
          driverId: 'driver-1',
          driver: {
            id: 'driver-1',
            iracingId: '12345',
            name: 'Test Driver',
            country: 'US',
            joinedAt: new Date().toISOString(),
          },
          role: 'owner',
          status: 'active',
          joinedAt: new Date().toISOString(),
        },
      ],
    };
  }

  /**
   * Override getLeagueConfig to return mock data
   */
  async getLeagueConfig(leagueId: string): Promise<any> {
    const endpoint = `/leagues/${leagueId}/config`;
    
    if (this.mockErrors.has(endpoint)) {
      throw this.mockErrors.get(endpoint);
    }
    
    if (this.mockResponses.has(endpoint)) {
      return this.mockResponses.get(endpoint);
    }
    
    // Default mock response
    return {
      form: {
        scoring: {
          presetId: 'preset-1',
        },
      },
    };
  }
}
