import { Injectable } from '@nestjs/common';
import { EventLog as PrismaEventLog, Prisma } from '@prisma/client';
import { EventLog } from '../../../domain/entities/event-log.entity';
import { InstallId } from '../../../domain/value-objects/install-id.vo';
import { EventName } from '../../../domain/value-objects/event-name.vo';
import { EventProperties, EventPropertiesData } from '../../../domain/value-objects/event-properties.vo';

export type EventLogCreateInput = Prisma.EventLogCreateManyInput;

@Injectable()
export class EventLogMapper {
  toDomain(prisma: PrismaEventLog): EventLog {
    return EventLog.reconstitute({
      id: prisma.id,
      userId: prisma.userId,
      installId: InstallId.fromString(prisma.installId),
      eventName: EventName.fromString(prisma.eventName),
      eventTime: prisma.eventTime,
      deviceBrand: prisma.deviceBrand,
      deviceModel: prisma.deviceModel,
      deviceOs: prisma.deviceOs,
      appVersion: prisma.appVersion,
      locale: prisma.locale,
      properties: EventProperties.fromData((prisma.properties as EventPropertiesData) ?? {}),
      createdAt: prisma.createdAt,
    });
  }

  toPersistence(domain: EventLog): EventLogCreateInput {
    return {
      userId: domain.userId,
      installId: domain.installId.value,
      eventName: domain.eventName.value,
      eventTime: domain.eventTime,
      deviceBrand: domain.deviceBrand,
      deviceModel: domain.deviceModel,
      deviceOs: domain.deviceOs,
      appVersion: domain.appVersion,
      locale: domain.locale,
      properties: domain.properties.toJSON() as Prisma.InputJsonValue,
    };
  }
}
