import { hubspotAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import {
    getDefaultPropertiesForObject,
    standardObjectDynamicProperties,
    standardObjectPropertiesDropdown,

} from '../common/props';
import { OBJECT_TYPE } from '../common/constants';
import { MarkdownVariant } from '@activepieces/pieces-framework';
import { Client } from '@hubspot/api-client';

export const updateProductAction = createAction({
    auth: hubspotAuth,
    name: 'update-product',
    displayName: 'Update Product',
    description: 'Updates a product in Hubspot.',
    audience: 'both',
    aiMetadata: { description: 'Updates properties on an existing product identified by its product ID, such as name, price, description, or tax, then returns the refreshed product. Use to modify a known product in the product library. Idempotent: applying the same property values converges to the same product state.', idempotent: true },
    props: {
        productId:Property.ShortText({
            displayName:'Product ID',
            description:'The ID of the product to update.',
            required:true
        }),
        objectProperties: standardObjectDynamicProperties(OBJECT_TYPE.PRODUCT,[]),
        markdown: Property.MarkDown({
            variant: MarkdownVariant.INFO,
            value: `### Properties to retrieve:
                                    
                    createdate, description, name, price, tax, hs_lastmodifieddate
                                            
                    **Specify here a list of additional properties to retrieve**`,
        }),
        additionalPropertiesToRetrieve: standardObjectPropertiesDropdown({
            objectType: OBJECT_TYPE.PRODUCT,
            displayName: 'Additional properties to retrieve',
            required: false,
        }),
    },
    async run(context) {
        const productId = context.propsValue.productId;
        const objectProperties = context.propsValue.objectProperties ?? {};
        const additionalPropertiesToRetrieve = context.propsValue.additionalPropertiesToRetrieve ?? [];

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

        // Add additional properties to the productProperties object
        Object.entries(objectProperties).forEach(([key, value]) => {
            if ((Array.isArray(value) && value.length === 0)) {
                return;  
			}
            // Format values if they are arrays
            productProperties[key] = Array.isArray(value) ? value.join(';') : value;
        });

        const client = new Client({ accessToken: context.auth.access_token });

        const updatedProduct = await client.crm.products.basicApi.update(productId, {
            properties: productProperties,
        });
        // Retrieve default properties for the product and merge with additional properties to retrieve
        const defaultproductProperties = getDefaultPropertiesForObject(OBJECT_TYPE.PRODUCT);

        const productDetails = await client.crm.products.basicApi.getById(updatedProduct.id, [
            ...defaultproductProperties,
            ...additionalPropertiesToRetrieve,
        ]);

        return productDetails;
    },
});
