import { createAction, Property } from '@activepieces/pieces-framework';
import { checkoutComAuth, getEnvironmentFromApiKey } from '../common/auth';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';

export const getPaymentDetailsAction = createAction({
  name: 'get_payment_details',
  auth: checkoutComAuth,
  displayName: 'Get Payment Details',
  description: 'Check transaction amount and status before refunding.',
  audience: 'both',
  aiMetadata: { description: 'Retrieves the full details of a single Checkout.com payment by its payment ID (resolved from an order reference), including amount and status. Choose it to inspect a transaction, for example to verify it is captured before issuing a refund. Read-only and idempotent.', idempotent: true },
  props: {
    reference: Property.ShortText({
      displayName: 'Reference',
      description: 'A reference, such as an order ID, that can be used to identify the payment',
      required: true,
    }),
    paymentId: Property.Dropdown({
      auth: checkoutComAuth,
      displayName: 'Payment ID',
      description: 'Select the payment to get details for',
      required: true,
      refreshers: ['reference'],
      options: async ({ auth, reference }) => {
        if (!reference || !auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please enter a reference first',
          };
        }

        try {
          const { baseUrl } = getEnvironmentFromApiKey(auth.secret_text);
          
          const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: `${baseUrl}/payments`,
            queryParams: {
              reference: reference as string,
              limit: '100',
            },
            headers: {
              Authorization: `Bearer ${auth.secret_text}`,
              'Content-Type': 'application/json',
            },
          });

          const payments = response.body.data || [];
          
          if (payments.length === 0) {
            return {
              disabled: true,
              options: [],
              placeholder: 'No payments found for this reference',
            };
          }

          return {
            disabled: false,
            options: payments.map((payment: any) => ({
              label: `${payment.id} - ${payment.amount} ${payment.currency} (${payment.status})`,
              value: payment.id,
            })),
          };
        } catch (error) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Error loading payments',
          };
        }
      },
    }),
  },
  async run(context) {
    const { paymentId } = context.propsValue;
    
    const { baseUrl } = getEnvironmentFromApiKey(context.auth.secret_text);
    
    if (!paymentId.match(/^(pay|sid)_[a-zA-Z0-9]{26}$/)) {
      throw new Error('Invalid payment ID format. Must start with "pay_" or "sid_" followed by 26 alphanumeric characters.');
    }
    
    try {
      const response = await httpClient.sendRequest({
        method: HttpMethod.GET,
        url: `${baseUrl}/payments/${paymentId}`,
        headers: {
          Authorization: `Bearer ${context.auth.secret_text}`,
          'Content-Type': 'application/json',
        },
      });
      
      return response.body;
    } catch (error: any) {
      if (error.response?.status === 401) {
        throw new Error('Unauthorized: Invalid API key or insufficient permissions');
      } else if (error.response?.status === 404) {
        throw new Error('Payment not found: The specified payment ID does not exist or is not accessible');
      } else {
        throw new Error(`Failed to get payment details: ${error.message}`);
      }
    }
  },
}); 