import { RacingDomainValidationError } from '../errors/RacingDomainError';

export class LeagueSocialLinks {
  readonly discordUrl: string | undefined;
  readonly youtubeUrl: string | undefined;
  readonly websiteUrl: string | undefined;

  private constructor(props: {
    discordUrl?: string;
    youtubeUrl?: string;
    websiteUrl?: string;
  }) {
    this.discordUrl = props.discordUrl;
    this.youtubeUrl = props.youtubeUrl;
    this.websiteUrl = props.websiteUrl;
  }

  static create(props: {
    discordUrl?: string;
    youtubeUrl?: string;
    websiteUrl?: string;
  }): LeagueSocialLinks {
    // Basic validation, e.g., if provided, must be valid URL
    if (props.discordUrl && !this.isValidUrl(props.discordUrl)) {
      throw new RacingDomainValidationError('Invalid Discord URL');
    }
    if (props.youtubeUrl && !this.isValidUrl(props.youtubeUrl)) {
      throw new RacingDomainValidationError('Invalid YouTube URL');
    }
    if (props.websiteUrl && !this.isValidUrl(props.websiteUrl)) {
      throw new RacingDomainValidationError('Invalid website URL');
    }
    return new LeagueSocialLinks(props);
  }

  private static isValidUrl(url: string): boolean {
    try {
      new URL(url);
      return true;
    } catch {
      return false;
    }
  }

  equals(other: LeagueSocialLinks): boolean {
    return (
      this.discordUrl === other.discordUrl &&
      this.youtubeUrl === other.youtubeUrl &&
      this.websiteUrl === other.websiteUrl
    );
  }
}