import { LeagueTimezone } from './LeagueTimezone';

import { RacingDomainValidationError } from '../errors/RacingDomainError';
import type { IValueObject } from '@core/shared/domain';

export interface ScheduledRaceSlotProps {
  roundNumber: number;
  scheduledAt: Date;
  timezone: LeagueTimezone;
}

export class ScheduledRaceSlot implements IValueObject<ScheduledRaceSlotProps> {
  readonly roundNumber: number;
  readonly scheduledAt: Date;
  readonly timezone: LeagueTimezone;

  constructor(params: { roundNumber: number; scheduledAt: Date; timezone: LeagueTimezone }) {
    if (!Number.isInteger(params.roundNumber) || params.roundNumber <= 0) {
      throw new RacingDomainValidationError('ScheduledRaceSlot.roundNumber must be a positive integer');
    }
    if (!(params.scheduledAt instanceof Date) || Number.isNaN(params.scheduledAt.getTime())) {
      throw new RacingDomainValidationError('ScheduledRaceSlot.scheduledAt must be a valid Date');
    }

    this.roundNumber = params.roundNumber;
    this.scheduledAt = params.scheduledAt;
    this.timezone = params.timezone;
  }

  get props(): ScheduledRaceSlotProps {
    return {
      roundNumber: this.roundNumber,
      scheduledAt: this.scheduledAt,
      timezone: this.timezone,
    };
  }

  equals(other: IValueObject<ScheduledRaceSlotProps>): boolean {
    const a = this.props;
    const b = other.props;
    return (
      a.roundNumber === b.roundNumber &&
      a.scheduledAt.getTime() === b.scheduledAt.getTime() &&
      a.timezone.equals(b.timezone)
    );
  }
}