import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { ampecoAuth } from '../../../common/auth';
import { handleApiError, makeAmpecoApiCall, prepareQueryParams, prepareRequestBody, processPathParameters } from '../../../common/utils';
import { CurrencyUpdateResponse } from '../../../models/responses';

/**
 * Generated from API version: 3.96.4
 */

// Endpoint: PATCH /public-api/resources/currencies/v2.0/{currency}

export const currencyUpdateAction = createAction({
  auth: ampecoAuth,
  name: 'currencyUpdate',
  displayName: 'Resources - Currencies - Update',
  description: 'Update a currency.',
  audience: 'both',
  aiMetadata: { description: 'Update an existing currency in AMPECO, identified by its numeric currency id, changing its display prefix/suffix, decimal precision, or minor-unit settings. Requires the currency to already exist; use Create Currency to add a new one. Re-running with the same values is safe, but supplied fields overwrite the current configuration.', idempotent: false },
  props: {
        
  currency: Property.Number({
    displayName: 'Currency',
    description: '',
    required: true,
  }),

  alphabeticCode: Property.StaticDropdown({
    displayName: 'Alphabetic Code',
    description: 'The currency 3-letter code.',
    required: true,
    options: {
      options: [
      { label: 'AED', value: 'AED' },
      { label: 'AFN', value: 'AFN' },
      { label: 'ALL', value: 'ALL' },
      { label: 'AMD', value: 'AMD' },
      { label: 'ANG', value: 'ANG' },
      { label: 'AOA', value: 'AOA' },
      { label: 'ARS', value: 'ARS' },
      { label: 'AUD', value: 'AUD' },
      { label: 'AWG', value: 'AWG' },
      { label: 'AZN', value: 'AZN' },
      { label: 'BAM', value: 'BAM' },
      { label: 'BBD', value: 'BBD' },
      { label: 'BDT', value: 'BDT' },
      { label: 'BGN', value: 'BGN' },
      { label: 'BHD', value: 'BHD' },
      { label: 'BIF', value: 'BIF' },
      { label: 'BMD', value: 'BMD' },
      { label: 'BND', value: 'BND' },
      { label: 'BOB', value: 'BOB' },
      { label: 'BOV', value: 'BOV' },
      { label: 'BRL', value: 'BRL' },
      { label: 'BSD', value: 'BSD' },
      { label: 'BTN', value: 'BTN' },
      { label: 'BWP', value: 'BWP' },
      { label: 'BYN', value: 'BYN' },
      { label: 'BZD', value: 'BZD' },
      { label: 'CAD', value: 'CAD' },
      { label: 'CDF', value: 'CDF' },
      { label: 'CHE', value: 'CHE' },
      { label: 'CHF', value: 'CHF' },
      { label: 'CHW', value: 'CHW' },
      { label: 'CLF', value: 'CLF' },
      { label: 'CLP', value: 'CLP' },
      { label: 'CNY', value: 'CNY' },
      { label: 'COP', value: 'COP' },
      { label: 'COU', value: 'COU' },
      { label: 'CRC', value: 'CRC' },
      { label: 'CUC', value: 'CUC' },
      { label: 'CUP', value: 'CUP' },
      { label: 'CVE', value: 'CVE' },
      { label: 'CZK', value: 'CZK' },
      { label: 'DJF', value: 'DJF' },
      { label: 'DKK', value: 'DKK' },
      { label: 'DOP', value: 'DOP' },
      { label: 'DZD', value: 'DZD' },
      { label: 'EGP', value: 'EGP' },
      { label: 'ERN', value: 'ERN' },
      { label: 'ETB', value: 'ETB' },
      { label: 'EUR', value: 'EUR' },
      { label: 'FJD', value: 'FJD' },
      { label: 'FKP', value: 'FKP' },
      { label: 'GBP', value: 'GBP' },
      { label: 'GEL', value: 'GEL' },
      { label: 'GHS', value: 'GHS' },
      { label: 'GIP', value: 'GIP' },
      { label: 'GMD', value: 'GMD' },
      { label: 'GNF', value: 'GNF' },
      { label: 'GTQ', value: 'GTQ' },
      { label: 'GYD', value: 'GYD' },
      { label: 'HKD', value: 'HKD' },
      { label: 'HNL', value: 'HNL' },
      { label: 'HRK', value: 'HRK' },
      { label: 'HTG', value: 'HTG' },
      { label: 'HUF', value: 'HUF' },
      { label: 'IDR', value: 'IDR' },
      { label: 'ILS', value: 'ILS' },
      { label: 'INR', value: 'INR' },
      { label: 'IQD', value: 'IQD' },
      { label: 'IRR', value: 'IRR' },
      { label: 'ISK', value: 'ISK' },
      { label: 'JMD', value: 'JMD' },
      { label: 'JOD', value: 'JOD' },
      { label: 'JPY', value: 'JPY' },
      { label: 'KES', value: 'KES' },
      { label: 'KGS', value: 'KGS' },
      { label: 'KHR', value: 'KHR' },
      { label: 'KMF', value: 'KMF' },
      { label: 'KPW', value: 'KPW' },
      { label: 'KRW', value: 'KRW' },
      { label: 'KWD', value: 'KWD' },
      { label: 'KYD', value: 'KYD' },
      { label: 'KZT', value: 'KZT' },
      { label: 'LAK', value: 'LAK' },
      { label: 'LBP', value: 'LBP' },
      { label: 'LKR', value: 'LKR' },
      { label: 'LRD', value: 'LRD' },
      { label: 'LSL', value: 'LSL' },
      { label: 'LYD', value: 'LYD' },
      { label: 'MAD', value: 'MAD' },
      { label: 'MDL', value: 'MDL' },
      { label: 'MGA', value: 'MGA' },
      { label: 'MKD', value: 'MKD' },
      { label: 'MMK', value: 'MMK' },
      { label: 'MNT', value: 'MNT' },
      { label: 'MOP', value: 'MOP' },
      { label: 'MRU', value: 'MRU' },
      { label: 'MUR', value: 'MUR' },
      { label: 'MVR', value: 'MVR' },
      { label: 'MWK', value: 'MWK' },
      { label: 'MXN', value: 'MXN' },
      { label: 'MXV', value: 'MXV' },
      { label: 'MYR', value: 'MYR' },
      { label: 'MZN', value: 'MZN' },
      { label: 'NAD', value: 'NAD' },
      { label: 'NGN', value: 'NGN' },
      { label: 'NIO', value: 'NIO' },
      { label: 'NOK', value: 'NOK' },
      { label: 'NPR', value: 'NPR' },
      { label: 'NZD', value: 'NZD' },
      { label: 'OMR', value: 'OMR' },
      { label: 'PAB', value: 'PAB' },
      { label: 'PEN', value: 'PEN' },
      { label: 'PGK', value: 'PGK' },
      { label: 'PHP', value: 'PHP' },
      { label: 'PKR', value: 'PKR' },
      { label: 'PLN', value: 'PLN' },
      { label: 'PYG', value: 'PYG' },
      { label: 'QAR', value: 'QAR' },
      { label: 'RON', value: 'RON' },
      { label: 'RSD', value: 'RSD' },
      { label: 'RUB', value: 'RUB' },
      { label: 'RWF', value: 'RWF' },
      { label: 'SAR', value: 'SAR' },
      { label: 'SBD', value: 'SBD' },
      { label: 'SCR', value: 'SCR' },
      { label: 'SDG', value: 'SDG' },
      { label: 'SEK', value: 'SEK' },
      { label: 'SGD', value: 'SGD' },
      { label: 'SHP', value: 'SHP' },
      { label: 'SLL', value: 'SLL' },
      { label: 'SOS', value: 'SOS' },
      { label: 'SRD', value: 'SRD' },
      { label: 'SSP', value: 'SSP' },
      { label: 'STN', value: 'STN' },
      { label: 'SVC', value: 'SVC' },
      { label: 'SYP', value: 'SYP' },
      { label: 'SZL', value: 'SZL' },
      { label: 'THB', value: 'THB' },
      { label: 'TJS', value: 'TJS' },
      { label: 'TMT', value: 'TMT' },
      { label: 'TND', value: 'TND' },
      { label: 'TOP', value: 'TOP' },
      { label: 'TRY', value: 'TRY' },
      { label: 'TTD', value: 'TTD' },
      { label: 'TWD', value: 'TWD' },
      { label: 'TZS', value: 'TZS' },
      { label: 'UAH', value: 'UAH' },
      { label: 'UGX', value: 'UGX' },
      { label: 'USD', value: 'USD' },
      { label: 'USN', value: 'USN' },
      { label: 'UYI', value: 'UYI' },
      { label: 'UYU', value: 'UYU' },
      { label: 'UYW', value: 'UYW' },
      { label: 'UZS', value: 'UZS' },
      { label: 'VES', value: 'VES' },
      { label: 'VND', value: 'VND' },
      { label: 'VUV', value: 'VUV' },
      { label: 'WST', value: 'WST' },
      { label: 'XAF', value: 'XAF' },
      { label: 'XAG', value: 'XAG' },
      { label: 'XAU', value: 'XAU' },
      { label: 'XBA', value: 'XBA' },
      { label: 'XBB', value: 'XBB' },
      { label: 'XBC', value: 'XBC' },
      { label: 'XBD', value: 'XBD' },
      { label: 'XCD', value: 'XCD' },
      { label: 'XDR', value: 'XDR' },
      { label: 'XOF', value: 'XOF' },
      { label: 'XPD', value: 'XPD' },
      { label: 'XPF', value: 'XPF' },
      { label: 'XPT', value: 'XPT' },
      { label: 'XSU', value: 'XSU' },
      { label: 'XTS', value: 'XTS' },
      { label: 'XUA', value: 'XUA' },
      { label: 'XXX', value: 'XXX' },
      { label: 'YER', value: 'YER' },
      { label: 'ZAR', value: 'ZAR' },
      { label: 'ZMW', value: 'ZMW' },
      { label: 'ZWL', value: 'ZWL' }
      ],
    },
  }),

  prefix: Property.ShortText({
    displayName: 'Prefix',
    description: 'When displaying amounts in this currency, the prefix is added in front of the amount. This could be symbols, for example $ or £.',
    required: false,
  }),

  suffix: Property.ShortText({
    displayName: 'Suffix',
    description: 'When displaying amounts in this currency, the suffix is added after the amount.',
    required: false,
  }),

  decimal: Property.Number({
    displayName: 'Decimal',
    description: 'The actual precision of the currency. This is used when charging a payment method or calculating totals.',
    required: false,
  }),

  unitPriceAndCalculationsDecimal: Property.Number({
    displayName: 'Unit Price And Calculations Decimal',
    description: 'Used when setting price per kWh and per minute in tariffs.',
    required: false,
  }),

  enableUseOfMinorCurrencyUnit: Property.StaticDropdown({
    displayName: 'Enable Use Of Minor Currency Unit',
    description: 'When enabled, the minor currency unit is used to set electricity price threshold for flexible tariffs and to set electricity cost tracking in the home charging app. The minor currency unit for the US dollar, for example, is the cent.',
    required: false,
    options: {
      options: [
      { label: 'true', value: 'true' },
      { label: 'false', value: 'false' }
      ],
    },
  }),

  minorUnitSign: Property.ShortText({
    displayName: 'Minor Unit Sign',
    description: 'The sign or letter for the minor currency unit. For US dollar, for example, this could be "¢" or "c" for cents.',
    required: false,
  }),
  },
  async run(context): Promise<CurrencyUpdateResponse> {
    try {
      const url = processPathParameters('/public-api/resources/currencies/v2.0/{currency}', context.propsValue);
      
      const queryParams = prepareQueryParams(context.propsValue, []);
      
      const body = prepareRequestBody(context.propsValue,
        ['alphabeticCode', 'prefix', 'suffix', 'decimal', 'unitPriceAndCalculationsDecimal', 'enableUseOfMinorCurrencyUnit', 'minorUnitSign']
      );

      
      return await makeAmpecoApiCall(
        context.auth,
        url,
        HttpMethod.PATCH,
        body,
        queryParams
      ) as CurrencyUpdateResponse;

    } catch (error) {
      handleApiError(error);
    }
  },
});
