import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { ValidationException } from '@modules/shared';
import {
  INDUSTRIAL_PARK_REPOSITORY,
  type IIndustrialParkRepository,
  type IndustrialParkDetailData,
} from '../../../domain/repositories/industrial-park.repository';
import { CompareIndustrialParksQuery } from './compare-industrial-parks.query';

@QueryHandler(CompareIndustrialParksQuery)
export class CompareIndustrialParksHandler implements IQueryHandler<CompareIndustrialParksQuery> {
  constructor(
    @Inject(INDUSTRIAL_PARK_REPOSITORY)
    private readonly repo: IIndustrialParkRepository,
  ) {}

  async execute(query: CompareIndustrialParksQuery): Promise<IndustrialParkDetailData[]> {
    if (query.ids.length < 2 || query.ids.length > 5) {
      throw new ValidationException('Compare requires 2-5 park IDs');
    }
    return this.repo.compareParks(query.ids);
  }
}
