import { Inject } from '@nestjs/common';
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
import { type ListingSearchItem } from '../../../domain/repositories/listing-read.dto';
import { LISTING_REPOSITORY, type IListingRepository, type PaginatedResult } from '../../../domain/repositories/listing.repository';
import { SearchListingsQuery } from './search-listings.query';

@QueryHandler(SearchListingsQuery)
export class SearchListingsHandler implements IQueryHandler<SearchListingsQuery> {
  constructor(
    @Inject(LISTING_REPOSITORY) private readonly listingRepo: IListingRepository,
  ) {}

  async execute(query: SearchListingsQuery): Promise<PaginatedResult<ListingSearchItem>> {
    return this.listingRepo.search({
      status: query.status,
      transactionType: query.transactionType,
      propertyType: query.propertyType,
      city: query.city,
      district: query.district,
      minPrice: query.minPrice,
      maxPrice: query.maxPrice,
      minArea: query.minArea,
      maxArea: query.maxArea,
      bedrooms: query.bedrooms,
      page: query.page,
      limit: query.limit,
    });
  }
}
