import { injectable } from 'inversify';
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
import { SponsorViewModel } from '@/lib/view-models/SponsorViewModel';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { Service, type DomainError } from '@/lib/contracts/services/Service';
import { Result } from '@/lib/contracts/Result';
import type { SponsorDashboardDTO } from '@/lib/types/generated/SponsorDashboardDTO';
import type { SponsorSponsorshipsDTO } from '@/lib/types/generated/SponsorSponsorshipsDTO';

@injectable()
export class SponsorService implements Service {
  private readonly apiClient: SponsorsApiClient;

  constructor() {
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new EnhancedErrorReporter(logger);
    this.apiClient = new SponsorsApiClient(baseUrl, errorReporter, logger);
  }

  async getAllSponsors(): Promise<SponsorViewModel[]> {
    const data = await this.apiClient.getAll();
    return data.sponsors.map(s => new SponsorViewModel(s));
  }

  async getSponsorDashboard(sponsorId: string): Promise<Result<SponsorDashboardDTO, DomainError>> {
    try {
      const data = await this.apiClient.getDashboard(sponsorId);
      if (!data) return Result.err({ type: 'notFound', message: 'Sponsor dashboard not found' });
      return Result.ok(data);
    } catch (error) {
      return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
    }
  }

  async getSponsorSponsorships(sponsorId: string): Promise<Result<SponsorSponsorshipsDTO, DomainError>> {
    try {
      const data = await this.apiClient.getSponsorships(sponsorId);
      if (!data) return Result.err({ type: 'notFound', message: 'Sponsor sponsorships not found' });
      return Result.ok(data);
    } catch (error) {
      return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
    }
  }

  async createSponsor(input: any): Promise<any> {
    return this.apiClient.create(input);
  }

  async getSponsorshipPricing(): Promise<any> {
    return this.apiClient.getPricing();
  }

  async getAvailableLeagues(): Promise<Result<any[], DomainError>> {
    try {
      const data = await this.apiClient.getAvailableLeagues();
      return Result.ok(data);
    } catch (error) {
      return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
    }
  }

  async getLeagueDetail(leagueId: string): Promise<Result<any, DomainError>> {
    try {
      const data = await this.apiClient.getLeagueDetail(leagueId);
      return Result.ok(data);
    } catch (error) {
      return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
    }
  }
}
