import { createAction } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { ninjapipeAuth } from '../../';
import { ninjapipeApiCall, flattenCustomFields, getAuth, ninjapipeCommon } from '../common';

export const getBudget = createAction({
  auth: ninjapipeAuth,
  name: 'get_budget',
  displayName: 'Get Budget',
  description: 'Retrieves a budget by ID.',
  audience: 'both',
  aiMetadata: { description: 'Fetch a single budget by its ID. Read-only lookup for inspecting one budget; to enumerate or search budgets use a list action instead.', idempotent: true },
  props: {
    budgetId: ninjapipeCommon.budgetDropdownRequired,
  },
  async run(context) {
    const auth = getAuth(context);
    const response = await ninjapipeApiCall<Record<string, unknown>>({ auth, method: HttpMethod.GET, path: `/budgets/${encodeURIComponent(String(context.propsValue.budgetId))}` });
    return flattenCustomFields(response.body);
  },
});
