/**
 * Domain Entity: Sponsor
 *
 * Represents a sponsor that can sponsor leagues/seasons.
 * Aggregate root for sponsor information.
 */
import { Entity } from '@core/shared/domain/Entity';
import { SponsorCreatedAt } from './SponsorCreatedAt';
import { SponsorEmail } from './SponsorEmail';
import { SponsorId } from './SponsorId';
import { SponsorName } from './SponsorName';
import { Url } from './Url';

// TODO sponsor is not actually the racing domain in my opinion

export class Sponsor extends Entity<SponsorId> {
  readonly name: SponsorName;
  readonly contactEmail: SponsorEmail;
  readonly logoUrl: Url | undefined;
  readonly websiteUrl: Url | undefined;
  readonly createdAt: SponsorCreatedAt;

  private constructor(props: {
    id: SponsorId;
    name: SponsorName;
    contactEmail: SponsorEmail;
    logoUrl?: Url;
    websiteUrl?: Url;
    createdAt: SponsorCreatedAt;
  }) {
    super(props.id);
    
    this.name = props.name;
    this.contactEmail = props.contactEmail;
    this.logoUrl = props.logoUrl ?? undefined;
    this.websiteUrl = props.websiteUrl ?? undefined;
    this.createdAt = props.createdAt;
  }

  static create(props: {
    id: string;
    name: string;
    contactEmail: string;
    logoUrl?: string;
    websiteUrl?: string;
    createdAt?: Date;
  }): Sponsor {
    const id = SponsorId.create(props.id);
    const name = SponsorName.create(props.name);
    const contactEmail = SponsorEmail.create(props.contactEmail);
    const logoUrl = props.logoUrl ? Url.create(props.logoUrl) : undefined;
    const websiteUrl = props.websiteUrl ? Url.create(props.websiteUrl) : undefined;
    const createdAt = SponsorCreatedAt.create(props.createdAt ?? new Date());

    return new Sponsor({
      id,
      name,
      contactEmail,
      createdAt,
      ...(logoUrl !== undefined ? { logoUrl } : {}),
      ...(websiteUrl !== undefined ? { websiteUrl } : {}),
    });
  }

  static rehydrate(props: {
    id: string;
    name: string;
    contactEmail: string;
    logoUrl?: string;
    websiteUrl?: string;
    createdAt: Date;
  }): Sponsor {
    const id = SponsorId.create(props.id);
    const name = SponsorName.create(props.name);
    const contactEmail = SponsorEmail.create(props.contactEmail);
    const logoUrl = props.logoUrl ? Url.create(props.logoUrl) : undefined;
    const websiteUrl = props.websiteUrl ? Url.create(props.websiteUrl) : undefined;
    const createdAt = SponsorCreatedAt.create(props.createdAt);

    return new Sponsor({
      id,
      name,
      contactEmail,
      createdAt,
      ...(logoUrl !== undefined ? { logoUrl } : {}),
      ...(websiteUrl !== undefined ? { websiteUrl } : {}),
    });
  }

  /**
   * Update sponsor information
   */
  update(props: Partial<{
    name: string;
    contactEmail: string;
    logoUrl?: string;
    websiteUrl?: string;
  }>): Sponsor {
    const name = props.name ? SponsorName.create(props.name) : this.name;
    const contactEmail = props.contactEmail ? SponsorEmail.create(props.contactEmail) : this.contactEmail;
    const logoUrl = props.logoUrl !== undefined ? (props.logoUrl ? Url.create(props.logoUrl) : undefined) : this.logoUrl;
    const websiteUrl = props.websiteUrl !== undefined ? (props.websiteUrl ? Url.create(props.websiteUrl) : undefined) : this.websiteUrl;

    return new Sponsor({
      id: this.id,
      name,
      contactEmail,
      createdAt: this.createdAt,
      ...(logoUrl !== undefined ? { logoUrl } : {}),
      ...(websiteUrl !== undefined ? { websiteUrl } : {}),
    });
  }
}