import { injectable, unmanaged } from 'inversify';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
import { TeamsApiClient } from '@/lib/api/teams/TeamsApiClient';
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { isProductionEnvironment } from '@/lib/config/env';
import { HomeDiscoveryViewModel } from '@/lib/view-models/HomeDiscoveryViewModel';

/**
 * Landing Service - DTO Only
 *
 * Returns raw API DTOs. No ViewModels or UX logic.
 * All client-side presentation logic must be handled by hooks/components.
 */
@injectable()
export class LandingService implements Service {
  private racesApi: RacesApiClient;
  private leaguesApi: LeaguesApiClient;
  private teamsApi: TeamsApiClient;
  private authApi: AuthApiClient;

  constructor(
    @unmanaged() racesApi?: RacesApiClient,
    @unmanaged() leaguesApi?: LeaguesApiClient,
    @unmanaged() teamsApi?: TeamsApiClient
  ) {
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new EnhancedErrorReporter(logger, {
      showUserNotifications: true,
      logToConsole: true,
      reportToExternal: isProductionEnvironment(),
    });

    this.racesApi = racesApi || new RacesApiClient(baseUrl, errorReporter, logger);
    this.leaguesApi = leaguesApi || new LeaguesApiClient(baseUrl, errorReporter, logger);
    this.teamsApi = teamsApi || new TeamsApiClient(baseUrl, errorReporter, logger);
    this.authApi = new AuthApiClient(baseUrl, errorReporter, logger);
  }

  async getHomeDiscovery(): Promise<any> {
    try {
      const [racesRes, leaguesRes, teamsRes] = await Promise.all([
        this.racesApi.getPageData(),
        this.leaguesApi.getAllWithCapacity(),
        this.teamsApi.getAll(),
      ]);

      const racesData = (racesRes as any).value || racesRes;
      const leaguesData = (leaguesRes as any).value || leaguesRes;
      const teamsData = (teamsRes as any).value || teamsRes;

      return new HomeDiscoveryViewModel({
        topLeagues: leaguesData.leagues.slice(0, 4),
        teams: teamsData.teams.slice(0, 4),
        upcomingRaces: racesData.races.slice(0, 4),
      } as any);
    } catch (error: unknown) {
      throw error;
    }
  }

  async getLandingData(): Promise<Result<{ featuredLeagues: unknown[]; stats: Record<string, unknown> }, DomainError>> {
    return Result.ok({ featuredLeagues: [], stats: {} });
  }

  async signup(_email: string): Promise<Result<void, DomainError>> {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const email = _email;
    return Result.err({ type: 'notImplemented', message: 'Email signup endpoint' });
  }
}
