import { Inject } from '@nestjs/common';
import { CommandHandler, type EventBus, type ICommandHandler } from '@nestjs/cqrs';
import { LISTING_REPOSITORY, type IListingRepository } from '@modules/listings/domain/repositories/listing.repository';
import { NotFoundException, ValidationException } from '@modules/shared';
import { ListingApprovedEvent } from '../../../domain/events/listing-approved.event';
import { ApproveListingCommand } from './approve-listing.command';

export interface ApproveListingResult {
  listingId: string;
  status: string;
  message: string;
}

@CommandHandler(ApproveListingCommand)
export class ApproveListingHandler implements ICommandHandler<ApproveListingCommand> {
  constructor(
    @Inject(LISTING_REPOSITORY) private readonly listingRepo: IListingRepository,
    private readonly eventBus: EventBus,
  ) {}

  async execute(command: ApproveListingCommand): Promise<ApproveListingResult> {
    const listing = await this.listingRepo.findById(command.listingId);
    if (!listing) {
      throw new NotFoundException('Listing không tồn tại');
    }

    if (listing.status !== 'PENDING_REVIEW') {
      throw new ValidationException(
        `Listing đang ở trạng thái ${listing.status}, chỉ có thể duyệt listing đang chờ duyệt`,
        { currentStatus: listing.status },
      );
    }

    listing.approve();

    if (command.moderationNotes) {
      listing.setModerationScore(1.0, command.moderationNotes);
    }

    await this.listingRepo.update(listing);

    this.eventBus.publish(
      new ListingApprovedEvent(listing.id, command.adminId, command.moderationNotes),
    );

    return {
      listingId: listing.id,
      status: 'ACTIVE',
      message: 'Listing đã được duyệt thành công',
    };
  }
}
