/**
 * Domain Event: SessionStarted
 * Emitted when an `app_session_start` telemetry event is received.
 * Used to trigger side effects like DAU tracking and Kafka publishing.
 */
export class SessionStartedEvent {
  readonly eventType = 'session.started' as const;
  readonly occurredAt: Date;

  constructor(
    public readonly userId: string | null,
    public readonly installId: string,
    public readonly clientTimestamp: number,
    public readonly properties: Record<string, any>,
  ) {
    this.occurredAt = new Date();
  }

  /** Get the identifier for DAU tracking (userId preferred, fallback to installId) */
  getDauIdentifier(): string {
    return this.userId || this.installId;
  }

  /** Get the platform from properties if available */
  getPlatform(): string | undefined {
    return this.properties?.platform;
  }

  /** Get the region from properties if available */
  getRegion(): string | undefined {
    return this.properties?.region;
  }

  toJSON() {
    return {
      eventType: this.eventType,
      userId: this.userId,
      installId: this.installId,
      clientTimestamp: this.clientTimestamp,
      properties: this.properties,
      occurredAt: this.occurredAt.toISOString(),
    };
  }
}
