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

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

// Endpoint: GET /public-api/resources/circuits/v2.0/{circuit}/charge-point-priorities

export const circuitChargePointPrioritiesListingAction = createAction({
  auth: ampecoAuth,
  name: 'circuitChargePointPrioritiesListing',
  displayName: 'Resources - Circuits - Circuit Charge Point Priorities Listing',
  description: 'Get all circuit&#x27;s charge point priorities.',
  audience: 'both',
  aiMetadata: { description: 'List the charge-point priority rules configured on a single AMPECO circuit, identified by numeric circuit ID. Read-only and safe to repeat; supports automatic pagination across pages. Use to enumerate which charge points are prioritized on one circuit, distinct from the user-priority and SoC-priority listings on the same circuit.', idempotent: true },
  props: {
        
  circuit: Property.Number({
    displayName: 'Circuit',
    required: true,
  }),
    per_page: Property.Number({
      displayName: 'Per page',
      description: 'When pagination is enabled: maximum total results across all pages. When pagination is disabled: number of results per API request (max 100).',
      required: false,
      defaultValue: 100,
    }),
    usePagination: Property.Checkbox({
      displayName: 'Paginate Results',
      description: 'Whether to automatically paginate to fetch all results',
      required: false,
      defaultValue: false,
    }),
  },
  async run(context): Promise<CircuitChargePointPrioritiesListingResponse> {
    try {
      const url = processPathParameters('/public-api/resources/circuits/v2.0/{circuit}/charge-point-priorities', context.propsValue);
      
      const queryParams = prepareQueryParams(context.propsValue, ['per_page', 'cursor']);
      
      const body = undefined;

          if (context.propsValue.usePagination) {
      return await paginate({
        auth: context.auth,
        method: 'GET',
        path: url,
        queryParams,
        body,
        perPage: context.propsValue.per_page ?? 100,
        dataPath: 'data',
      }) as CircuitChargePointPrioritiesListingResponse;
    }

      
      return await makeAmpecoApiCall(
        context.auth,
        url,
        HttpMethod.GET,
        body,
        queryParams
      ) as CircuitChargePointPrioritiesListingResponse;

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