import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
  INDUSTRIAL_PARK_REPOSITORY,
  type IIndustrialParkRepository,
  type IndustrialParkDetailData,
} from '../../../domain/repositories/industrial-park.repository';
import { GetIndustrialParkQuery } from './get-industrial-park.query';

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

  async execute(query: GetIndustrialParkQuery): Promise<IndustrialParkDetailData | null> {
    // Try slug first, then ID
    const result = await this.repo.findDetailBySlug(query.slugOrId);
    if (result) return result;
    return this.repo.findDetailById(query.slugOrId);
  }
}
