import { LeagueTimezone } from './LeagueTimezone';

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

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

export class ScheduledRaceSlot implements ValueObject<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: ValueObject<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)
    );
  }
}