import { InstallId } from '../value-objects/install-id.vo';
import { EventName } from '../value-objects/event-name.vo';
import { EventProperties } from '../value-objects/event-properties.vo';

export class EventLog {
  private _id: bigint | null;
  private _userId: string | null;
  private _installId: InstallId;
  private _eventName: EventName;
  private _eventTime: Date;
  private _deviceBrand: string | null;
  private _deviceModel: string | null;
  private _deviceOs: string | null;
  private _appVersion: string | null;
  private _locale: string | null;
  private _properties: EventProperties;
  private _createdAt: Date;

  private constructor() {}

  get id(): bigint | null { return this._id; }
  get userId(): string | null { return this._userId; }
  get installId(): InstallId { return this._installId; }
  get eventName(): EventName { return this._eventName; }
  get eventTime(): Date { return this._eventTime; }
  get deviceBrand(): string | null { return this._deviceBrand; }
  get deviceModel(): string | null { return this._deviceModel; }
  get deviceOs(): string | null { return this._deviceOs; }
  get appVersion(): string | null { return this._appVersion; }
  get locale(): string | null { return this._locale; }
  get properties(): EventProperties { return this._properties; }
  get createdAt(): Date { return this._createdAt; }

  get dauIdentifier(): string {
    return this._userId ?? this._installId.value;
  }

  static create(props: {
    userId?: string | null;
    installId: InstallId;
    eventName: EventName;
    eventTime: Date;
    deviceBrand?: string | null;
    deviceModel?: string | null;
    deviceOs?: string | null;
    appVersion?: string | null;
    locale?: string | null;
    properties?: EventProperties;
  }): EventLog {
    const log = new EventLog();
    log._id = null;
    log._userId = props.userId ?? null;
    log._installId = props.installId;
    log._eventName = props.eventName;
    log._eventTime = props.eventTime;
    log._deviceBrand = props.deviceBrand ?? null;
    log._deviceModel = props.deviceModel ?? null;
    log._deviceOs = props.deviceOs ?? null;
    log._appVersion = props.appVersion ?? null;
    log._locale = props.locale ?? null;
    log._properties = props.properties ?? EventProperties.empty();
    log._createdAt = new Date();
    return log;
  }

  static reconstitute(props: {
    id: bigint;
    userId: string | null;
    installId: InstallId;
    eventName: EventName;
    eventTime: Date;
    deviceBrand: string | null;
    deviceModel: string | null;
    deviceOs: string | null;
    appVersion: string | null;
    locale: string | null;
    properties: EventProperties;
    createdAt: Date;
  }): EventLog {
    const log = new EventLog();
    log._id = props.id;
    log._userId = props.userId;
    log._installId = props.installId;
    log._eventName = props.eventName;
    log._eventTime = props.eventTime;
    log._deviceBrand = props.deviceBrand;
    log._deviceModel = props.deviceModel;
    log._deviceOs = props.deviceOs;
    log._appVersion = props.appVersion;
    log._locale = props.locale;
    log._properties = props.properties;
    log._createdAt = props.createdAt;
    return log;
  }
}
