import { Inject } from '@nestjs/common';
import { type ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import { NotFoundException } from '@modules/shared';
import {
  INDUSTRIAL_PARK_REPOSITORY,
  type IIndustrialParkRepository,
} from '../../../domain/repositories/industrial-park.repository';
import { UpdateIndustrialParkCommand } from './update-industrial-park.command';

@CommandHandler(UpdateIndustrialParkCommand)
export class UpdateIndustrialParkHandler implements ICommandHandler<UpdateIndustrialParkCommand> {
  constructor(
    @Inject(INDUSTRIAL_PARK_REPOSITORY)
    private readonly repo: IIndustrialParkRepository,
  ) {}

  async execute(cmd: UpdateIndustrialParkCommand): Promise<void> {
    const entity = await this.repo.findById(cmd.id);
    if (!entity) {
      throw new NotFoundException('Industrial park', cmd.id);
    }

    entity.updateDetails({
      name: cmd.name,
      nameEn: cmd.nameEn,
      developer: cmd.developer,
      operator: cmd.operator,
      status: cmd.status,
      occupancyRate: cmd.occupancyRate,
      remainingAreaHa: cmd.remainingAreaHa,
      tenantCount: cmd.tenantCount,
      landRentUsdM2Year: cmd.landRentUsdM2Year,
      rbfRentUsdM2Month: cmd.rbfRentUsdM2Month,
      rbwRentUsdM2Month: cmd.rbwRentUsdM2Month,
      managementFeeUsd: cmd.managementFeeUsd,
      infrastructure: cmd.infrastructure,
      connectivity: cmd.connectivity,
      incentives: cmd.incentives,
      targetIndustries: cmd.targetIndustries,
      description: cmd.description,
      descriptionEn: cmd.descriptionEn,
      isVerified: cmd.isVerified,
    });

    await this.repo.update(entity);
  }
}
