import type { Sponsor } from '@core/racing/domain/entities/sponsor/Sponsor';
import { GetSponsorsOutputDTO } from '../dtos/GetSponsorsOutputDTO';
import { SponsorDTO } from '../dtos/SponsorDTO';

export class GetSponsorsPresenter {
  private model: GetSponsorsOutputDTO | null = null;

  reset() {
    this.model = null;
  }

  present(sponsors: Sponsor[]): void {
    this.model = {
      sponsors: sponsors.map<SponsorDTO>((sponsor) => {
        const sponsorData = new SponsorDTO();
        sponsorData.id = sponsor.id.toString();
        sponsorData.name = sponsor.name.toString();
        sponsorData.contactEmail = sponsor.contactEmail.toString();
        sponsorData.createdAt = sponsor.createdAt.toDate();

        if (sponsor.logoUrl) {
          sponsorData.logoUrl = sponsor.logoUrl.toString();
        }

        if (sponsor.websiteUrl) {
          sponsorData.websiteUrl = sponsor.websiteUrl.toString();
        }

        return sponsorData;
      }),
    };
  }

  getResponseModel(): GetSponsorsOutputDTO | null {
    return this.model;
  }

  get responseModel(): GetSponsorsOutputDTO {
    if (!this.model) throw new Error('Presenter not presented');
    return this.model;
  }
}
