import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { intercomAuth } from '../auth';
import { intercomClient } from '../common';

export const conversationAssigned = createTrigger({
	name: 'conversationAssigned',
	displayName: 'Conversation assigned to any Intercom admin',
	description: 'Triggers when a conversation is assigned to an admin',
	aiMetadata: {
		description: 'Fires when a conversation is assigned to an Intercom admin or teammate, whether manually or via routing rules. Outputs the conversation object including its admin_assignee_id and team_assignee_id.',
	},
	props: {},
	sampleData: undefined,
	auth: intercomAuth,
	type: TriggerStrategy.APP_WEBHOOK,
	async onEnable(context) {
		const client = intercomClient(context.auth);
		const response = await client.admins.identify();

		if (!response.app?.id_code) {
			throw new Error('Could not find admin id code');
		}

		context.app.createListeners({
			events: ['conversation.admin.assigned'],
			identifierValue: response['app']['id_code'],
		});
	},
	async onDisable(context) {
		// implement webhook deletion logic
	},
	async run(context) {
		return [context.payload.body];
	},
});
