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

export const addOrUpdateSubscription = createAction({
  name: 'addOrUpdateSubscription',
  displayName: 'Add or Update Subscription',
  description: 'Creates a new subscription.',
  audience: 'both',
  aiMetadata: { description: 'Creates or updates a product subscription for a contact in the ChargeKeep/Sperse CRM. Identifies the contact by Contact ID or External Contact ID (Contact XREF) and requires a Product Code and Payment Period Type (Monthly, Annual, or LifeTime) that already exist on the product. Use to enroll or change a contact in a subscription plan; repeating the same input updates the same subscription rather than creating a duplicate.', idempotent: true },
  auth: chargekeepAuth,
  props: {
    contactId: Property.Number({
      displayName: 'Contact ID',
      defaultValue: 0,
      required: false,
    }),
    productId: Property.Number({
      displayName: 'Product ID',
      required: false,
    }),
    contactXref: Property.ShortText({
      displayName: 'External Contact ID',
      description:
        'ContactXref have to be specified and correct to look up the correct contact',
      required: false,
    }),
    productCode: Property.ShortText({
      displayName: 'Product Code',
      description:
        'Product Code (Unique product identifier). ProductCode have to be specified and correct to look up the correct product',
      required: true,
    }),
    paymentPeriodType: Property.StaticDropdown({
      displayName: 'Payment Period Type',
      description:
        'The chosen Period Type has to be set for the Product on Sperse side',
      required: true,
      defaultValue: 'Monthly',
      options: {
        disabled: false,
        options: [
          {
            label: 'Monthly',
            value: 'Monthly',
          },
          {
            label: 'Annual',
            value: 'Annual',
          },
          {
            label: 'LifeTime',
            value: 'LifeTime',
          },
        ],
      },
    }),
    hasRecurringBilling: Property.StaticDropdown({
      displayName: 'Is it Recurring Billing',
      required: false,
      defaultValue: false,
      options: {
        disabled: false,
        options: [
          {
            label: 'Yes',
            value: true,
          },
          {
            label: 'No',
            value: false,
          },
        ],
      },
    }),
  },
  async run(context) {
    const subscription = {
      contactId: context.propsValue.contactId,
      contactXref: context.propsValue.contactXref,
      products: [
        {
          productId: context.propsValue.productId,
          productCode: context.propsValue.productCode,
          paymentPeriodType: context.propsValue.paymentPeriodType,
          hasRecurringBilling: context.propsValue.hasRecurringBilling,
        },
      ],
      productId: context.propsValue.productId,
      productCode: context.propsValue.productCode,
      paymentPeriodType: context.propsValue.paymentPeriodType,
      hasRecurringBilling: context.propsValue.hasRecurringBilling,
    };

    const res = await httpClient.sendRequest({  
      method: HttpMethod.PUT,
      url: `${context.auth.props.base_url}/api/services/CRM/OrderSubscription/Update`,
      headers: {
        'api-key': context.auth.props.api_key, // Pass API key in headers
        'Content-Type': 'application/json',
      },
      body: {
        ...subscription,
      },
    });

    return {
      status: res.status,
      body: res.body,
    };
  },
});
