// Helcim Payment Integration Utilities

export const processHelcimPayment = async (paymentData) => {
  const { amount, paymentToken, customerData } = paymentData;
  
  // Check if Helcim is configured
  const helcimAccountId = process.env.HELCIM_ACCOUNT_ID;
  const helcimApiToken = process.env.HELCIM_API_TOKEN;
  
  if (!helcimAccountId || !helcimApiToken) {
    console.warn('Helcim not configured - skipping payment processing');
    return {
      success: false,
      message: 'Payment processing not configured',
      testMode: true
    };
  }
  
  try {
    // In production, you would make API calls to Helcim here
    // Example structure:
    const response = await fetch('https://api.helcim.com/v2/payment/purchase', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'api-token': helcimApiToken,
        'account-id': helcimAccountId
      },
      body: JSON.stringify({
        amount,
        currency: 'CAD',
        paymentToken,
        customerCode: customerData.email,
        contactName: customerData.name,
        billingAddress: {
          contactName: customerData.name,
          email: customerData.email
        }
      })
    });
    
    const result = await response.json();
    
    return {
      success: result.success || false,
      transactionId: result.transactionId,
      customerId: result.customerCode,
      message: result.message
    };
  } catch (error) {
    console.error('Helcim payment error:', error);
    return {
      success: false,
      message: error.message || 'Payment processing failed'
    };
  }
};

export const createHelcimSubscription = async (subscriptionData) => {
  const { customerId, planId, amount } = subscriptionData;
  
  const helcimAccountId = process.env.HELCIM_ACCOUNT_ID;
  const helcimApiToken = process.env.HELCIM_API_TOKEN;
  
  if (!helcimAccountId || !helcimApiToken) {
    console.warn('Helcim not configured - skipping subscription creation');
    return {
      success: false,
      message: 'Subscription processing not configured',
      testMode: true
    };
  }
  
  try {
    // Create recurring payment plan
    const response = await fetch('https://api.helcim.com/v2/payment/plan', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'api-token': helcimApiToken,
        'account-id': helcimAccountId
      },
      body: JSON.stringify({
        customerCode: customerId,
        planName: `Ghost Guild ${planId}`,
        amount,
        currency: 'CAD',
        frequency: 'MONTHLY',
        startDate: new Date().toISOString().split('T')[0]
      })
    });
    
    const result = await response.json();
    
    return {
      success: result.success || false,
      subscriptionId: result.planId,
      message: result.message
    };
  } catch (error) {
    console.error('Helcim subscription error:', error);
    return {
      success: false,
      message: error.message || 'Subscription creation failed'
    };
  }
};

export const cancelHelcimSubscription = async (subscriptionId) => {
  const helcimApiToken = process.env.HELCIM_API_TOKEN;
  
  if (!helcimApiToken) {
    return {
      success: false,
      message: 'Subscription management not configured'
    };
  }
  
  try {
    const response = await fetch(`https://api.helcim.com/v2/payment/plan/${subscriptionId}/cancel`, {
      method: 'POST',
      headers: {
        'api-token': helcimApiToken
      }
    });
    
    const result = await response.json();
    
    return {
      success: result.success || false,
      message: result.message
    };
  } catch (error) {
    console.error('Helcim cancellation error:', error);
    return {
      success: false,
      message: error.message || 'Subscription cancellation failed'
    };
  }
};