import { ChangeDetectionStrategy, Component, input, output } from '@angular/core';

import { CheckoutPaymentQrComponent } from './components/checkout-payment-qr/checkout-payment-qr.component';
import { CheckoutPaymentTransferComponent } from './components/checkout-payment-transfer/checkout-payment-transfer.component';
import {
  PaymentMethod,
  PaymentMethodOption,
  QrPaymentStatus,
  TransferAccount,
  TransferField,
  TransferValidationStatus,
} from './checkout-page.models';

@Component({
  selector: 'app-checkout-payment-step',
  standalone: true,
  imports: [CheckoutPaymentQrComponent, CheckoutPaymentTransferComponent],
  templateUrl: './checkout-payment-step.component.html',
  styleUrl: './checkout-payment-step.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckoutPaymentStepComponent {
  readonly paymentMethods = input.required<ReadonlyArray<PaymentMethodOption>>();
  readonly selectedPaymentMethod = input.required<PaymentMethod>();
  readonly paymentMethodDisabled = input<boolean>(false);
  readonly copiedTransferField = input<TransferField | null>(null);
  readonly transferAccount = input<TransferAccount | null>(null);
  readonly transferDni = input<string>('');
  readonly isGeneratingIntent = input<boolean>(false);
  readonly qrData = input<string | null>(null);
  readonly qrPaymentStatus = input<QrPaymentStatus>('idle');
  readonly isCheckingQrPayment = input<boolean>(false);
  readonly qrPaymentAmount = input<number>(0);
  readonly whatsappUrl = input<string | null>(null);
  readonly transferValidationStatus = input<TransferValidationStatus>('idle');
  readonly transferVerificationErrorTitle = input<string | null>(null);

  readonly paymentMethodChange = output<PaymentMethod>();
  readonly copyTransferValue = output<{ field: TransferField; value: string }>();
  readonly cancelStep = output<void>();
  readonly complete = output<void>();
  readonly generateTransferIntent = output<string>();

  protected selectPaymentMethod(method: PaymentMethod): void {
    if (this.paymentMethodDisabled()) {
      return;
    }

    this.paymentMethodChange.emit(method);
  }

  protected requestCopy(field: TransferField, value: string): void {
    this.copyTransferValue.emit({ field, value });
  }
}
