import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { sapAribaAuth } from '../auth';
import { sapAribaCommon } from '../common';

export const listPurchaseOrders = createAction({
    auth: sapAribaAuth,
    name: 'list_purchase_orders',
    displayName: 'List Purchase Orders',
    description: 'Retrieves purchase order header information for a specified buyer.',
    audience: 'both',
    aiMetadata: { description: 'List purchase order headers from SAP Ariba Network for a buyer (required buyer ANID). Use to find or browse POs; optional filters (supplier ANID, document number, order/routing status, date range capped at 31 days) narrow the results, and leaving them empty returns all of the buyer\'s orders paginated. Read-only and idempotent.', idempotent: true },
    props: {
        buyerANID: Property.ShortText({
            displayName: 'Buyer ANID',
            description: "The buyer's Ariba Network ID.",
            required: true,
        }),
        supplierANID: Property.ShortText({
            displayName: 'Supplier ANID',
            description: "Filter by supplier's Ariba Network ID.",
            required: false,
        }),
        documentNumber: Property.ShortText({
            displayName: 'Document Number',
            description: 'Filter by order ID / purchase order number.',
            required: false,
        }),
        orderStatus: Property.StaticDropdown({
            displayName: 'Order Status',
            description: 'Filter by order processing state.',
            required: false,
            options: {
                disabled: false,
                options: [
                    { label: 'New', value: 'New' },
                    { label: 'Changed', value: 'Changed' },
                    { label: 'Confirmed', value: 'Confirmed' },
                    { label: 'Shipped', value: 'Shipped' },
                    { label: 'Invoiced', value: 'Invoiced' },
                    { label: 'Canceled', value: 'Canceled' },
                ],
            },
        }),
        routingStatus: Property.StaticDropdown({
            displayName: 'Routing Status',
            description: 'Filter by routing status.',
            required: false,
            options: {
                disabled: false,
                options: [
                    { label: 'Queued', value: 'Queued' },
                    { label: 'Sent', value: 'Sent' },
                    { label: 'Acknowledged', value: 'Acknowledged' },
                    { label: 'Failed', value: 'Failed' },
                ],
            },
        }),
        startDate: Property.DateTime({
            displayName: 'Start Date',
            description: 'Filter orders received on or after this date. Max range: 31 days.',
            required: false,
        }),
        endDate: Property.DateTime({
            displayName: 'End Date',
            description: 'Filter orders received on or before this date. Max range: 31 days.',
            required: false,
        }),
        top: Property.Number({
            displayName: 'Page Size',
            description: 'Number of items to return.',
            required: false,
            defaultValue: 50,
        }),
        skip: Property.Number({
            displayName: 'Offset',
            description: 'Number of items to skip.',
            required: false,
            defaultValue: 0,
        }),
    },
    async run(context) {
        const { buyerANID, supplierANID, documentNumber, orderStatus, routingStatus, startDate, endDate, top, skip } = context.propsValue;

        const filters: string[] = [];

        if (supplierANID) {
            filters.push(`supplierANID eq '${supplierANID}'`);
        }
        if (documentNumber) {
            filters.push(`documentNumber eq '${documentNumber}'`);
        }
        if (orderStatus) {
            filters.push(`orderStatus eq '${orderStatus}'`);
        }
        if (routingStatus) {
            filters.push(`routingStatus eq '${routingStatus}'`);
        }
        if (startDate) {
            filters.push(`startDate eq '${startDate}'`);
        }
        if (endDate) {
            filters.push(`endDate eq '${endDate}'`);
        }

        const queryParams: Record<string, string> = {};

        if (filters.length > 0) {
            queryParams['$filter'] = filters.join(' and ');
        }
        if (top) {
            queryParams['$top'] = top.toString();
        }
        if (skip) {
            queryParams['$skip'] = skip.toString();
        }

        const response = await sapAribaCommon.makeRequest(
            context.auth,
            HttpMethod.GET,
            '/orders',
            queryParams,
            undefined,
            { 'X-ARIBA-NETWORK-ID': buyerANID }
        );

        return response;
    },
});

