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

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

//Endpoint: GET /public-api/resources/roaming-tariffs/v2.0)

export const roamingTariffsListingAction = createAction({
  auth: ampecoAuth,
  name: 'roamingTariffsListing',
  displayName: 'Resources - Roaming Tariffs - Listing',
  description: 'Get all Roaming Tariffs.',
  audience: 'both',
  aiMetadata: { description: 'List roaming tariffs, optionally auto-paginating across all pages to gather every record. Use to browse or find a roaming tariff; read-only and safe to retry. To fetch one known tariff by ID use the read action instead.', idempotent: true },
  props: {
        
    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<RoamingTariffsListingResponse> {
    try {
      const url = processPathParameters('/public-api/resources/roaming-tariffs/v2.0', context.propsValue);
      
      const queryParams = prepareQueryParams(context.propsValue, ['page', 'per_page']);
      
      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 RoamingTariffsListingResponse;
    }

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

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