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

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

// Endpoint: GET /public-api/resources/partner-contracts/v1.0

export const partnerContractsListingAction = createAction({
  auth: ampecoAuth,
  name: 'partnerContractsListing',
  displayName: 'Resources - Partner Contracts - Listing',
  description: 'Get all Partner Contracts.',
  audience: 'both',
  aiMetadata: { description: 'List all AMPECO partner contracts, optionally auto-paginating across pages to gather every result. Use to discover contract IDs or survey existing partner agreements before reading or creating one; for a known single contract use the partner contract read action. Read-only and idempotent.', 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<PartnerContractsListingResponse> {
    try {
      const url = processPathParameters('/public-api/resources/partner-contracts/v1.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 PartnerContractsListingResponse;
    }

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

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