import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { NotFoundException } from '@modules/shared';
import {
  AGENT_REPOSITORY,
  type AgentDashboardData,
  type IAgentRepository,
} from '../../../domain/repositories/agent.repository';
import { GetAgentDashboardQuery } from './get-agent-dashboard.query';

@QueryHandler(GetAgentDashboardQuery)
export class GetAgentDashboardHandler
  implements IQueryHandler<GetAgentDashboardQuery>
{
  constructor(
    @Inject(AGENT_REPOSITORY)
    private readonly agentRepo: IAgentRepository,
  ) {}

  async execute(query: GetAgentDashboardQuery): Promise<AgentDashboardData> {
    const agent = await this.agentRepo.findByUserId(query.userId);
    if (!agent) {
      throw new NotFoundException('Không tìm thấy thông tin môi giới');
    }

    return this.agentRepo.getDashboard(agent.id);
  }
}
