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 { UpdateUtilityResponse } from '../../../models/responses';

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

// Endpoint: PUT /public-api/resources/utilities/v1.0/{utility}

export const updateUtilityAction = createAction({
  auth: ampecoAuth,
  name: 'updateUtility',
  displayName: 'Resources - Utilities - Update Utility',
  description: 'Update a single Utility.',
  audience: 'both',
  aiMetadata: { description: 'Update an existing AMPECO utility (electricity provider) record by its numeric ID, setting its name. Not idempotent in the create sense: the utility must already exist and this overwrites its name. Use the create utility action to add a new utility.', idempotent: false },
  props: {
        
  utility: Property.Number({
    displayName: 'Utility',
    description: '',
    required: true,
  }),

  name: Property.ShortText({
    displayName: 'Name',
    description: '',
    required: true,
  }),
  },
  async run(context): Promise<UpdateUtilityResponse> {
    try {
      const url = processPathParameters('/public-api/resources/utilities/v1.0/{utility}', context.propsValue);
      
      const queryParams = prepareQueryParams(context.propsValue, []);
      
      const body = prepareRequestBody(context.propsValue,
        ['name']
      );

      
      return await makeAmpecoApiCall(
        context.auth,
        url,
        HttpMethod.PUT,
        body,
        queryParams
      ) as UpdateUtilityResponse;

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