import { PoolInjectionBatch as PrismaPoolInjectionBatch, Prisma } from '@prisma/client';
import {
  PoolInjectionBatch,
  PoolInjectionBatchData,
} from '../../../domain/aggregates/pool-injection-batch.aggregate';
import { BatchStatus } from '../../../domain/value-objects/batch-status.enum';

export class PoolInjectionBatchMapper {
  static toDomain(prismaBatch: PrismaPoolInjectionBatch): PoolInjectionBatch {
    const data: PoolInjectionBatchData = {
      id: prismaBatch.id,
      batchNo: prismaBatch.batchNo,
      startDate: prismaBatch.startDate,
      endDate: prismaBatch.endDate,
      orderCount: prismaBatch.orderCount,
      totalAmount: Number(prismaBatch.totalAmount),
      status: prismaBatch.status as BatchStatus,
      scheduledInjectionTime: prismaBatch.scheduledInjectionTime,
      actualInjectionTime: prismaBatch.actualInjectionTime,
      injectionTxHash: prismaBatch.injectionTxHash,
    };

    return PoolInjectionBatch.reconstitute(data);
  }

  static toPersistence(batch: PoolInjectionBatch): {
    id?: bigint;
    batchNo: string;
    startDate: Date;
    endDate: Date;
    orderCount: number;
    totalAmount: Prisma.Decimal;
    status: string;
    scheduledInjectionTime: Date | null;
    actualInjectionTime: Date | null;
    injectionTxHash: string | null;
  } {
    const data = batch.toPersistence();

    return {
      id: data.id,
      batchNo: data.batchNo,
      startDate: data.startDate,
      endDate: data.endDate,
      orderCount: data.orderCount,
      totalAmount: new Prisma.Decimal(data.totalAmount),
      status: data.status,
      scheduledInjectionTime: data.scheduledInjectionTime || null,
      actualInjectionTime: data.actualInjectionTime || null,
      injectionTxHash: data.injectionTxHash || null,
    };
  }
}
