// Mobile Money Service
// Handles mobile money connector abstraction and operations

import { v4 as uuidv4 } from 'uuid';
import { requireCapability } from '@/shared/solacenet/sdk';
import { ledgerPostingAPI } from '@/core/ledger/posting-api';
import { logger } from '@/infrastructure/monitoring/logger';

export interface MobileMoneyRequest {
  tenantId: string;
  programId?: string;
  region?: string;
  provider: string; // e.g., 'm-pesa', 'mtn', 'orange'
  phoneNumber: string;
  amount: string;
  currencyCode: string;
  transactionType: 'cash-in' | 'cash-out' | 'transfer';
  referenceId?: string;
}

export interface MobileMoneyResponse {
  transactionId: string;
  status: 'pending' | 'success' | 'failed';
  providerResponse?: Record<string, unknown>;
}

export class MobileMoneyService {
  /**
   * Process mobile money transaction
   */
  async processTransaction(request: MobileMoneyRequest): Promise<MobileMoneyResponse> {
    await requireCapability('mobile-money-connector', {
      tenantId: request.tenantId,
      programId: request.programId,
      region: request.region,
      capabilityId: 'mobile-money-connector',
      channel: 'API',
    });

    // Route to specific capability based on transaction type
    if (request.transactionType === 'cash-in') {
      await requireCapability('mobile-money-cash-in', {
        tenantId: request.tenantId,
        region: request.region,
        capabilityId: 'mobile-money-cash-in',
        channel: 'API',
      });
    } else if (request.transactionType === 'cash-out') {
      await requireCapability('mobile-money-cash-out', {
        tenantId: request.tenantId,
        region: request.region,
        capabilityId: 'mobile-money-cash-out',
        channel: 'API',
      });
    } else if (request.transactionType === 'transfer') {
      await requireCapability('mobile-money-transfers', {
        tenantId: request.tenantId,
        region: request.region,
        capabilityId: 'mobile-money-transfers',
        channel: 'API',
      });
    }

    // In production, call provider connector
    const transactionId = uuidv4();

    // Post to ledger
    await ledgerPostingAPI.postEntries({
      referenceId: request.referenceId || transactionId,
      entries: [
        {
          debitAccountId: `mobile-money-${request.provider}`,
          creditAccountId: `wallet-${request.phoneNumber}`,
          amount: request.amount,
          currencyCode: request.currencyCode,
          assetType: 'fiat',
          description: `Mobile money ${request.transactionType}`,
        },
      ],
      transactionType: 'TYPE_A',
      metadata: {
        provider: request.provider,
        phoneNumber: request.phoneNumber,
        transactionType: request.transactionType,
      },
    });

    logger.info(`Mobile money transaction processed`, {
      transactionId,
      provider: request.provider,
      type: request.transactionType,
    });

    return {
      transactionId,
      status: 'success',
    };
  }
}

export const mobileMoneyService = new MobileMoneyService();
