import { EventType } from '../models/entities/EventType.entity';
import { env } from '../config/env';
import { User } from '../models/entities/User.entity';
import { ProfileEnhancementService } from '../services/ProfileEnhancementService';

const profileEnhancement = new ProfileEnhancementService();

function toPublicHost(host: User) {
  return {
    fullName: host.fullName,
    username: host.username,
    avatarUrl: host.avatarUrl,
    bannerUrl: host.bannerUrl,
    logoUrl: host.logoUrl,
    tagline: host.tagline,
    timezone: host.timezone,
    headline: host.headline,
    displayHeadline: profileEnhancement.buildDisplayHeadline(
      host.jobTitle,
      host.company,
      host.headline,
    ),
    bio: host.bio,
    jobTitle: host.jobTitle,
    company: host.company,
    location: host.location,
    linkedinUrl: host.linkedinUrl,
  };
}

export class EventTypeView {
  static toHost(eventType: EventType, username: string) {
    return {
      id: eventType.id,
      slug: eventType.slug,
      title: eventType.title,
      durationMinutes: eventType.durationMinutes,
      bufferBeforeMinutes: eventType.bufferBeforeMinutes,
      bufferAfterMinutes: eventType.bufferAfterMinutes,
      location: eventType.location,
      meetingUrl: eventType.meetingUrl,
      description: eventType.description,
      customQuestions: eventType.customQuestions,
      isActive: Boolean(eventType.isActive),
      color: eventType.color,
      bannerUrl: eventType.bannerUrl,
      logoUrl: eventType.logoUrl,
      tagline: eventType.tagline,
      publicUrl: `${env.bookingAppUrl}/book/${encodeURIComponent(username)}/${encodeURIComponent(eventType.slug)}`,
      availabilityConfigured: (eventType.availabilitySchedule?.weeklyRules?.length ?? 0) > 0,
      createdAt: eventType.createdAt.toISOString(),
    };
  }

  static toList(eventTypes: EventType[], username: string) {
    return { data: eventTypes.map((et) => EventTypeView.toHost(et, username)) };
  }

  static toPublicHostPage(host: User, eventTypes: EventType[]) {
    return {
      host: toPublicHost(host),
      eventTypes: eventTypes.map((eventType) => ({
        slug: eventType.slug,
        title: eventType.title,
        durationMinutes: eventType.durationMinutes,
        location: eventType.location,
        meetingUrl: eventType.meetingUrl,
        description: eventType.description,
        bannerUrl: eventType.bannerUrl,
        logoUrl: eventType.logoUrl,
        tagline: eventType.tagline,
        color: eventType.color,
        publicUrl: `${env.bookingAppUrl}/book/${encodeURIComponent(host.username)}/${encodeURIComponent(eventType.slug)}`,
      })),
    };
  }

  static toPublicPage(host: User, eventType: EventType) {
    return {
      host: toPublicHost(host),
      eventType: {
        slug: eventType.slug,
        title: eventType.title,
        durationMinutes: eventType.durationMinutes,
        location: eventType.location,
        meetingUrl: eventType.meetingUrl,
        description: eventType.description,
        bannerUrl: eventType.bannerUrl,
        logoUrl: eventType.logoUrl,
        tagline: eventType.tagline,
        color: eventType.color,
        /** Event availability timezone (what hosts configure on the event). */
        timezone: eventType.availabilitySchedule?.timezone ?? host.timezone,
        customQuestions: eventType.customQuestions,
      },
    };
  }
}
