import { Injectable } from '@nestjs/common';
import { Markup } from 'telegraf';
import { InlineKeyboardMarkup } from 'telegraf/typings/core/types/typegram';
import { PrismaService } from '../../common/services/prisma.service';
import { NowPaymentsService } from 'src/payments/nowpayments.service';
import { PaystarService } from 'src/payments/paystar.service';
@Injectable()
export class PaymentService {
  constructor(
    private prisma: PrismaService,
    private nowPayments: NowPaymentsService,
    private paystar: PaystarService,
  ) { }

  async createCryptoPayment(userId: number) {
    const { paymentUrl, orderId } = await this.nowPayments.createPremiumPayment(userId);
    await this.prisma.payment.create({
      data: { userId, type: 'crypto', amount: Number(process.env.USD_PRICE) || 10, currency: 'usd', orderId, paymentUrl },
    });
    return { paymentUrl, orderId };
  }

  async createRialPayment(userId: number) {
    const { paymentUrl, orderId } = await this.paystar.createPremiumPayment(userId);
    await this.prisma.payment.create({
      data: { userId, type: 'rial', amount: 999000, currency: 'irr', orderId, paymentUrl },
    });
    return { paymentUrl, orderId };
  }

  async getPaymentByOrderId(orderId: string) {
    return await this.prisma.payment.findUnique({
      where: { orderId },
      include: { user: true },
    });
  }

  createUpgradeKeyboard(): Markup.Markup<InlineKeyboardMarkup> {
    return Markup.inlineKeyboard([
      // [Markup.button.callback('💎 پرداخت کریپتو', 'pay_crypto')],
      [Markup.button.callback('💳 پرداخت ریالی', 'pay_rial')],
      [Markup.button.callback('🔙 بازگشت', 'back')],
    ]);
  }

  createCryptoPaymentKeyboard(paymentUrl: string, orderId: string): Markup.Markup<InlineKeyboardMarkup> {
    return Markup.inlineKeyboard([
      [Markup.button.url('💳 پرداخت', paymentUrl)],
      [Markup.button.callback('بررسی وضعیت پرداخت', `check_payment_${orderId}`)],
      [Markup.button.callback('🔙 بازگشت', 'upgrade_premium')],
    ]);
  }

  createRialPaymentKeyboard(paymentUrl: string): Markup.Markup<InlineKeyboardMarkup> {
    return Markup.inlineKeyboard([
      [Markup.button.url('💳 پرداخت', paymentUrl)],
      [Markup.button.callback('🔙 بازگشت', 'upgrade_premium')],
    ]);
  }
}