import type { GetPurchaseOrderParams, SapProxyResponse } from '@/tools/sap_s4hana/types'
import {
  baseProxyBody,
  buildEntityQuery,
  quoteOdataKey,
  SAP_PROXY_URL,
  transformSapProxyResponse,
} from '@/tools/sap_s4hana/utils'
import type { ToolConfig } from '@/tools/types'

export const getPurchaseOrderTool: ToolConfig<GetPurchaseOrderParams, SapProxyResponse> = {
  id: 'sap_s4hana_get_purchase_order',
  name: 'SAP S/4HANA Get Purchase Order',
  description:
    'Retrieve a single purchase order by PurchaseOrder key from SAP S/4HANA Cloud (API_PURCHASEORDER_PROCESS_SRV, A_PurchaseOrder).',
  version: '1.0.0',
  params: {
    subdomain: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description:
        'SAP BTP subaccount subdomain (technical name of your subaccount, not the S/4HANA host)',
    },
    region: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'BTP region (e.g. eu10, us10)',
    },
    clientId: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'OAuth client ID from the S/4HANA Communication Arrangement',
    },
    clientSecret: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'OAuth client secret from the S/4HANA Communication Arrangement',
    },
    deploymentType: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Deployment type: cloud_public (default), cloud_private, or on_premise',
    },
    authType: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Authentication type: oauth_client_credentials (default) or basic',
    },
    baseUrl: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Base URL of the S/4HANA host (Cloud Private / On-Premise)',
    },
    tokenUrl: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'OAuth token URL (Cloud Private / On-Premise + OAuth)',
    },
    username: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Username for HTTP Basic auth',
    },
    password: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Password for HTTP Basic auth',
    },
    purchaseOrder: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'PurchaseOrder key (string, up to 10 characters)',
    },
    select: {
      type: 'string',
      required: false,
      visibility: 'user-or-llm',
      description: 'Comma-separated fields to return ($select)',
    },
    expand: {
      type: 'string',
      required: false,
      visibility: 'user-or-llm',
      description: 'Comma-separated navigation properties to expand (e.g., "to_PurchaseOrderItem")',
    },
  },
  request: {
    url: SAP_PROXY_URL,
    method: 'POST',
    headers: () => ({ 'Content-Type': 'application/json' }),
    body: (params) => ({
      ...baseProxyBody(params),
      service: 'API_PURCHASEORDER_PROCESS_SRV',
      path: `/A_PurchaseOrder(${quoteOdataKey(params.purchaseOrder.trim())})`,
      method: 'GET',
      query: buildEntityQuery(params),
    }),
  },
  transformResponse: transformSapProxyResponse,
  outputs: {
    status: { type: 'number', description: 'HTTP status code returned by SAP' },
    data: {
      type: 'json',
      description: 'OData v2 response envelope; entity at output.data.d',
      properties: {
        d: {
          type: 'json',
          description: 'A_PurchaseOrder entity',
          properties: {
            PurchaseOrder: { type: 'string', description: 'Purchase order number' },
            PurchaseOrderType: { type: 'string', description: 'PO document type' },
            CompanyCode: { type: 'string', description: 'Company code' },
            PurchasingOrganization: { type: 'string', description: 'Purchasing organization' },
            PurchasingGroup: { type: 'string', description: 'Purchasing group' },
            Supplier: { type: 'string', description: 'Supplier business partner key' },
            DocumentCurrency: {
              type: 'string',
              description: 'Document currency',
              optional: true,
            },
            NetAmount: {
              type: 'string',
              description: 'Net amount of the purchase order',
              optional: true,
            },
            CreationDate: {
              type: 'string',
              description: 'Creation date (OData /Date(ms)/)',
              optional: true,
            },
            CreatedByUser: {
              type: 'string',
              description: 'User who created the PO',
              optional: true,
            },
            PurchaseOrderDate: {
              type: 'string',
              description: 'Purchase order date',
              optional: true,
            },
            ValidityStartDate: {
              type: 'string',
              description: 'Validity start date',
              optional: true,
            },
            ValidityEndDate: {
              type: 'string',
              description: 'Validity end date',
              optional: true,
            },
            IncotermsClassification: {
              type: 'string',
              description: 'Incoterms classification (e.g., FOB)',
              optional: true,
            },
            PaymentTerms: {
              type: 'string',
              description: 'Payment terms key',
              optional: true,
            },
            LastChangeDateTime: {
              type: 'string',
              description: 'Last change timestamp (OData /Date(ms)/)',
              optional: true,
            },
            to_PurchaseOrderItem: {
              type: 'json',
              description: 'Expanded PO items (when $expand=to_PurchaseOrderItem)',
              optional: true,
            },
          },
        },
      },
    },
  },
}
