import { Injectable, ServiceUnavailableException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

/**
 * Google Play Billing verification. Per the architecture decision in
 * docs/01-ANALYSIS-ARCHITECTURE-DB-API-MIGRATION.md §6.5, the CURRENT
 * business model sells access via Activation Codes purchased outside the
 * app (WhatsApp/website) — this endpoint exists as the integration point
 * for a FUTURE switch to in-app Google Play purchases, gated behind the
 * `billing_enabled` feature flag so it's a no-op until that decision is
 * made.
 *
 * Production implementation must call the Google Play Developer API
 * (androidpublisher.purchases.products.get) with a service-account
 * credential — never trust the client-supplied token's contents alone.
 * See docs/PAYMENTS.md.
 */
@Injectable()
export class BillingService {
  constructor(private readonly prisma: PrismaService) {}

  async verifyPurchase(userId: string, purchaseToken: string, productId: string) {
    const flag = await this.prisma.featureFlag.findUnique({ where: { key: 'billing_enabled' } });
    if (!flag?.value) {
      throw new ServiceUnavailableException(
        'الشراء داخل التطبيق غير مُفعّل حاليًا — التفعيل الحالي يتم عبر الأكواد فقط.',
      );
    }

    // Integration point: call Google Play Developer API here with
    // `purchaseToken` + `productId`, verify purchaseState === 0 (purchased)
    // and acknowledgementState, then grant entitlement. Left unimplemented
    // pending Play Console product configuration — see docs/PAYMENTS.md.
    void userId;
    void purchaseToken;
    void productId;
    throw new ServiceUnavailableException('التحقق من المشتريات عبر Google Play غير مُهيّأ بعد.');
  }
}
