import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
  INDUSTRIAL_PARK_REPOSITORY,
  type IIndustrialParkRepository,
  type IndustrialParkListItem,
  type PaginatedResult,
} from '../../../domain/repositories/industrial-park.repository';
import { ListIndustrialParksQuery } from './list-industrial-parks.query';

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

  async execute(query: ListIndustrialParksQuery): Promise<PaginatedResult<IndustrialParkListItem>> {
    return this.repo.search({
      query: query.query,
      province: query.province,
      region: query.region,
      status: query.status,
      minAreaHa: query.minAreaHa,
      maxRentUsdM2: query.maxRentUsdM2,
      targetIndustry: query.targetIndustry,
      ownerId: query.ownerId,
      page: query.page,
      limit: query.limit,
    });
  }
}
