import { createAction } from '@activepieces/pieces-framework';
import { AuthenticationType, httpClient, HttpMethod } from '@activepieces/pieces-common';
import { manychatAuth } from '../auth';
import { BASE_URL, subscriberId, tagIdDropdown } from '../common/props';

export const addTagToUserAction = createAction({
	auth: manychatAuth,
	name: 'addTagToUser',
	displayName: 'Add Tag to User',
	description: 'Adds a tag to a user.',
	audience: 'both',
	aiMetadata: { description: 'Assigns an existing tag to a Manychat subscriber, identified by subscriber ID and tag ID, then returns the updated subscriber info. Use to label or segment a known contact (e.g. mark them as a lead). Idempotent: re-adding the same tag leaves the subscriber in the same state.', idempotent: true },
	props: {
		subscriberId: subscriberId,
		tagId: tagIdDropdown,
	},
	async run({ auth, propsValue }) {
		const { subscriberId, tagId } = propsValue;

		const addTagResponse = await httpClient.sendRequest<{ status: string }>({
			url: `${BASE_URL}/subscriber/addTag`,
			method: HttpMethod.POST,
			authentication: {
				type: AuthenticationType.BEARER_TOKEN,
				token: auth.secret_text,
			},
			body: {
				subscriber_id: subscriberId,
				tag_id: tagId,
			},
		});

		if (addTagResponse.body.status !== 'success') {
			throw Error(`Unexpected Error occured : ${JSON.stringify(addTagResponse.body)}`);
		}

		const userResponse = await httpClient.sendRequest<{ data: Record<string, any> }>({
			method: HttpMethod.GET,
			url: `${BASE_URL}/subscriber/getInfo`,
			authentication: {
				type: AuthenticationType.BEARER_TOKEN,
				token: auth.secret_text,
			},
			queryParams: {
				subscriber_id: `${subscriberId}`,
			},
		});

		return userResponse.body.data;
	},
});
