import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
  PROJECT_REPOSITORY,
  type IProjectRepository,
  type ProjectDetailData,
} from '../../../domain/repositories/project-development.repository';
import { GetProjectQuery } from './get-project.query';

@QueryHandler(GetProjectQuery)
export class GetProjectHandler implements IQueryHandler<GetProjectQuery> {
  constructor(
    @Inject(PROJECT_REPOSITORY)
    private readonly repo: IProjectRepository,
  ) {}

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