import { Injectable } from '@nestjs/common';
import { DailyActiveStats as PrismaDailyActiveStats, Prisma } from '@prisma/client';
import { DailyActiveStats } from '../../../domain/aggregates/daily-active-stats/daily-active-stats.aggregate';

export type DailyActiveStatsCreateInput = Prisma.DailyActiveStatsCreateInput;

@Injectable()
export class DailyActiveStatsMapper {
  toDomain(prisma: PrismaDailyActiveStats): DailyActiveStats {
    const dauByProvince = new Map<string, number>(
      Object.entries((prisma.dauByProvince as Record<string, number>) ?? {}),
    );
    const dauByCity = new Map<string, number>(
      Object.entries((prisma.dauByCity as Record<string, number>) ?? {}),
    );

    return DailyActiveStats.reconstitute({
      day: prisma.day,
      dauCount: prisma.dauCount,
      dauByProvince,
      dauByCity,
      calculatedAt: prisma.calculatedAt,
      version: prisma.version,
    });
  }

  toPersistence(domain: DailyActiveStats): DailyActiveStatsCreateInput {
    return {
      day: domain.day,
      dauCount: domain.dauCount,
      dauByProvince: Object.fromEntries(domain.dauByProvince) as Prisma.InputJsonValue,
      dauByCity: Object.fromEntries(domain.dauByCity) as Prisma.InputJsonValue,
      calculatedAt: domain.calculatedAt,
      version: domain.version,
    };
  }
}
