import { Injectable } from '@nestjs/common';
// eslint-disable-next-line @typescript-eslint/consistent-type-imports -- NestJS DI requires value imports
import { CommandBus } from '@nestjs/cqrs';
import {
  type IPaymentInitiator,
  type InitiatePaymentInput,
  type InitiatePaymentResult,
} from '@modules/shared';
import { CreatePaymentCommand } from '../../application/commands/create-payment/create-payment.command';
import { type CreatePaymentResult } from '../../application/commands/create-payment/create-payment.handler';

/**
 * Adapter exposing the payments module through the shared `IPaymentInitiator`
 * port. Other modules (e.g. listings) depend on the port instead of importing
 * application-layer commands from payments — see A-10.
 */
@Injectable()
export class CommandBusPaymentInitiator implements IPaymentInitiator {
  constructor(private readonly commandBus: CommandBus) {}

  async initiate(input: InitiatePaymentInput): Promise<InitiatePaymentResult> {
    const result: CreatePaymentResult = await this.commandBus.execute(
      new CreatePaymentCommand(
        input.userId,
        input.provider,
        input.type,
        input.amountVND,
        input.description,
        input.returnUrl,
        input.ipAddress,
        input.transactionId,
        input.idempotencyKey,
      ),
    );
    return result;
  }
}
