import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
import { PrismaService } from '@modules/shared';
import { GetPriceHistoryQuery } from './get-price-history.query';

export interface PriceHistoryItem {
  id: string;
  oldPrice: bigint;
  newPrice: bigint;
  source: string;
  changedAt: Date;
}

@QueryHandler(GetPriceHistoryQuery)
export class GetPriceHistoryHandler implements IQueryHandler<GetPriceHistoryQuery> {
  constructor(private readonly prisma: PrismaService) {}

  async execute(query: GetPriceHistoryQuery): Promise<PriceHistoryItem[]> {
    return this.prisma.priceHistory.findMany({
      where: { listingId: query.listingId },
      orderBy: { changedAt: 'desc' },
      select: {
        id: true,
        oldPrice: true,
        newPrice: true,
        source: true,
        changedAt: true,
      },
    });
  }
}
