import { Inject } from '@nestjs/common';
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs';
import {
  MARKET_INDEX_REPOSITORY,
  type IMarketIndexRepository,
  type MarketReportResult,
} from '../../../domain/repositories/market-index.repository';
import { GenerateReportCommand } from './generate-report.command';

export interface GenerateReportResult {
  city: string;
  period: string;
  data: MarketReportResult[];
  generatedAt: string;
}

@CommandHandler(GenerateReportCommand)
export class GenerateReportHandler implements ICommandHandler<GenerateReportCommand> {
  constructor(
    @Inject(MARKET_INDEX_REPOSITORY) private readonly marketIndexRepo: IMarketIndexRepository,
  ) {}

  async execute(command: GenerateReportCommand): Promise<GenerateReportResult> {
    const data = await this.marketIndexRepo.getMarketReport(
      command.city,
      command.period,
      command.propertyType,
    );

    return {
      city: command.city,
      period: command.period,
      data,
      generatedAt: new Date().toISOString(),
    };
  }
}
