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,
      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,
      properties: domain.properties.toJSON() as Prisma.InputJsonValue,
    };
  }
}
