<script>
import { ref } from 'vue';
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import { StripeModule } from "@/components/stripe/StripeModule.vue";

const paymentIntent = ref(null);

// Create a payment intent
const createPaymentIntent = async (reader, order_id, tax_percentage = 0) => {
  SessionUser.request(
      '/orders/module/stripe/payment_intent',
      'POST',
      {
        id: order_id,
        reader: reader.id,
        tax_percentage: tax_percentage,
      },
  ).then((response) => {
    console.log('Response from Stripe:', response.data.data);
    if (response.status === 200) {
      // Get the payment intent
      StripeModule.paymentIntents.getPaymentIntent(order_id);
      return response;
    } else {
      console.error('Error creating payment intent:', response);
      return null;
    }
  }).catch((error) => {
    console.error('Error creating payment intent:', error);
  })
}

// Get the payment intent
const getPaymentIntent = async (order_id) => {
  SessionUser.request(
      '/orders/module/stripe/payment_intent',
      'GET',
      {
        id: order_id,
      },
  ).then((response) => {
    console.log('Response from Stripe:', response.data.data);
    if (response.status === 200) {
      paymentIntent.value = response.data.data;
      return response;
    } else {
      console.error('Error getting payment intent:', response);
      return null;
    }
  }).catch((error) => {
    console.error('Error getting payment intent:', error);
    // Unset the payment intent if an error occurs
    paymentIntent.value = null;
  })
}

// Delete the payment intent
const deletePaymentIntent = async (order_id) => {
  SessionUser.request(
      '/orders/module/stripe/payment_intent',
      'DELETE',
      {
        id: order_id,
      },
  ).then((response) => {
    console.log('Response from Stripe:', response.data.data);
    if (response.status === 200) {
      paymentIntent.value = null;
      return response;
    } else {
      console.error('Error deleting payment intent:', response);
      return null;
    }
  }).catch((error) => {
    console.error('Error deleting payment intent:', error);
  })
}

// Is the payment intent ready to capture?
const isPaymentIntentReadyToCapture = (paymentIntent) => {
  return paymentIntent && paymentIntent.status === 'requires_capture';
}

// Get the amount of the ready to capture payment intent
// { amount: 0, amount_capturable: 0, currency: 'dkk' }
const getReadyToCapturePaymentAmount = (paymentIntent) => {
  let defaultAmount = {
    amount: null,
    amount_capturable: null,
    amount_received: null,
    currency: null,
  };
  if (isPaymentIntentReadyToCapture(paymentIntent) || paymentIntent.amount_received > 0) {
    defaultAmount.amount = paymentIntent.amount || 0;
    defaultAmount.amount_capturable = paymentIntent.amount_capturable || 0;
    defaultAmount.amount_received = paymentIntent.amount_received || 0;
    defaultAmount.currency = paymentIntent.currency.toUpperCase() || 'DKK';
  } else {
    console.error('Payment intent is not ready to capture:', paymentIntent);
  }
  return defaultAmount;
}

// Check if the payment intent amount equals the amount
const isPaymentIntentAmountEqualToOrderAmount = (paymentIntent, matchesAmount) => {
  if (paymentIntent && paymentIntent.amount) {
    if (paymentIntent.amount === matchesAmount) {
      console.log('Payment intent amount matches:', paymentIntent.amount, matchesAmount);
      return true;
    } else if (paymentIntent.amount === 0) {
      console.log('Payment intent amount is zero:', paymentIntent.amount, matchesAmount);
      return true;
    } else if (paymentIntent.amount > 0) {
      console.error('Payment intent amount is greater than zero:', paymentIntent.amount, matchesAmount);
    } else {
      console.error('Payment intent amount does not match:', paymentIntent.amount, matchesAmount);
    }
  } else {
    console.error('Payment intent is not valid:', paymentIntent);
  }
  return false;
}

// Capture the payment intent
const capturePaymentIntent = async (order_id) => {
  SessionUser.request(
      '/orders/module/stripe/payment_intent/capture',
      'POST',
      {
        id: order_id,
      },
  ).then((response) => {
    console.log('Response from Stripe:', response.data.data);
    if (response.status === 200) {
      paymentIntent.value = response.data.data;
      return response;
    } else {
      console.error('Error capturing payment intent:', response);
      return null;
    }
  }).catch((error) => {
    console.error('Error capturing payment intent:', error);
  })
}

// Check if the payment intent amount is received (status: 'succeeded')
const isPaymentIntentAmountReceived = (paymentIntent) => {
  if (paymentIntent && paymentIntent.status) {
    if (paymentIntent.status === 'succeeded') {
      console.log('Payment intent amount received:', paymentIntent.status);
      return true;
    } else {
      console.error('Payment intent amount not received:', paymentIntent.status);
    }
  } else {
    console.error('Payment intent is not valid:', paymentIntent);
  }
  return false;
}


export const StripePaymentIntent = {
  createPaymentIntent,
  getPaymentIntent,
  deletePaymentIntent,
  paymentIntent,
  isPaymentIntentReadyToCapture,
  getReadyToCapturePaymentAmount,
  capturePaymentIntent,
  isPaymentIntentAmountEqualToOrderAmount,
  isPaymentIntentAmountReceived,
}
</script>