import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
  INDUSTRIAL_LISTING_REPOSITORY,
  type IIndustrialListingRepository,
  type IndustrialListingListItem,
  type PaginatedResult,
} from '../../../domain/repositories/industrial-listing.repository';
import { ListIndustrialListingsQuery } from './list-industrial-listings.query';

@QueryHandler(ListIndustrialListingsQuery)
export class ListIndustrialListingsHandler implements IQueryHandler<ListIndustrialListingsQuery> {
  constructor(
    @Inject(INDUSTRIAL_LISTING_REPOSITORY)
    private readonly repo: IIndustrialListingRepository,
  ) {}

  async execute(query: ListIndustrialListingsQuery): Promise<PaginatedResult<IndustrialListingListItem>> {
    return this.repo.search({
      parkId: query.parkId,
      propertyType: query.propertyType,
      leaseType: query.leaseType,
      status: query.status,
      minAreaM2: query.minAreaM2,
      maxAreaM2: query.maxAreaM2,
      minPriceUsdM2: query.minPriceUsdM2,
      maxPriceUsdM2: query.maxPriceUsdM2,
      query: query.query,
      page: query.page,
      limit: query.limit,
    });
  }
}
