import { createAction, Property } from '@activepieces/pieces-framework';
import { getGraphBaseUrl } from '../common/microsoft-cloud';
import { Client } from '@microsoft/microsoft-graph-client';
import { microsoftOutlookAuth } from '../common/auth';
import { messageIdDropdown } from '../common/props';

export const addLabelToEmailAction = createAction({
	auth: microsoftOutlookAuth,
	name: 'addLabelToEmail',
	displayName: 'Add Label to Email',
	description: 'Adds a category (label) to an email message.',
	audience: 'both',
	aiMetadata: { description: 'Adds one or more Outlook categories (labels) to a specific message, merging them with any categories already present. Use this to tag or classify an email. Idempotent: re-adding the same categories leaves the message unchanged since duplicates are de-duplicated.', idempotent: true },
	props: {
		messageId: messageIdDropdown({
			displayName: 'Email',
			description: 'Select the email message to add the label to.',
			required: true,
		}),
		categories: Property.Array({
			displayName: 'Categories',
			description: 'Categories to add to the email.',
			required: true,
		}),
	},
	async run(context) {
		const { messageId, categories } = context.propsValue;

		const cloud = context.auth.props?.['cloud'] as string | undefined;
		const client = Client.initWithMiddleware({
			authProvider: {
				getAccessToken: () => Promise.resolve(context.auth.access_token),
			},
			baseUrl: getGraphBaseUrl(cloud),
		});

		const message = await client.api(`/me/messages/${messageId}`).get();
		const existingCategories = message.categories || [];

		const updatedCategories = [...new Set([...existingCategories, ...categories])];

		const response = await client.api(`/me/messages/${messageId}`).patch({
			categories: updatedCategories,
		});
		
		return response;
	},
});
