/**
 * Application Use Case: GetSponsorsUseCase
 *
 * Retrieves all sponsors.
 */

import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import type { GetSponsorsViewModel } from '../presenters/IGetSponsorsPresenter';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export class GetSponsorsUseCase {
  constructor(
    private readonly sponsorRepository: ISponsorRepository,
  ) {}

  async execute(): Promise<Result<GetSponsorsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
    try {
      const sponsors = await this.sponsorRepository.findAll();

      const viewModel: GetSponsorsViewModel = {
        sponsors: sponsors.map(sponsor => ({
          id: sponsor.id,
          name: sponsor.name,
          contactEmail: sponsor.contactEmail,
          websiteUrl: sponsor.websiteUrl,
          logoUrl: sponsor.logoUrl,
          createdAt: sponsor.createdAt,
        })),
      };

      return Result.ok(viewModel);
    } catch {
      return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch sponsors' });
    }
  }
}