import { AppConnectionValueForAuthProperty, OAuth2PropertyValue, Property } from '@activepieces/pieces-framework';
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';
import { getTaskListsDropdown, createTodoClient } from '../common';
import { microsoftToDoAuth } from '../auth';
import { PageCollection } from '@microsoft/microsoft-graph-client';
import dayjs from 'dayjs';
import { TodoTask } from '@microsoft/microsoft-graph-types';

const polling: Polling<AppConnectionValueForAuthProperty<typeof microsoftToDoAuth>, { task_list_id: string }> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, store, lastFetchEpochMS }) {
		const taskListId = propsValue.task_list_id;
		const client = createTodoClient(auth);

		const tasks = [];

		const filter =
			lastFetchEpochMS === 0
				? '$top=10'
				: `$filter=lastModifiedDateTime gt ${dayjs(lastFetchEpochMS).toISOString()}`;

		let response: PageCollection = await client
			.api(`/me/todo/lists/${taskListId}/tasks?${filter}`)
			.get();

		if (lastFetchEpochMS === 0) {
			for (const task of response.value as TodoTask[]) {
				tasks.push(task);
			}
		} else {
			while (response.value.length > 0) {
				for (const task of response.value as TodoTask[]) {
					tasks.push(task);
				}

				if (response['@odata.nextLink']) {
					response = await client.api(response['@odata.nextLink']).get();
				} else {
					break;
				}
			}
		}

		return tasks.map((task) => ({
			epochMilliSeconds: dayjs(task.lastModifiedDateTime).valueOf(),
			data: task,
		}));
	},
};

export const newOrUpdatedTaskTrigger = createTrigger({
	name: 'new_or_updated_task',
	displayName: 'New or Updated Task',
	description: 'Triggers when a new task is created or an existing task is updated.',
	aiMetadata: {
		description: 'Fires when a task in the selected Microsoft To Do task list is created or modified in any way. Each event represents the current state of one task that was added or changed; polls on the task last-modified time, so the same task can fire again whenever it is edited.',
	},
	auth: microsoftToDoAuth,
	props: {
		task_list_id: Property.Dropdown({
   auth: microsoftToDoAuth,
			displayName: 'Task List',
			required: true,
			refreshers: [],
			options: async ({ auth }) => {
				if (!(auth as OAuth2PropertyValue)?.access_token) {
					return { disabled: true, placeholder: 'Connect your account first', options: [] };
				}
				return await getTaskListsDropdown(auth as OAuth2PropertyValue);
			},
		}),
	},
	type: TriggerStrategy.POLLING,
	async onEnable(context) {
		await pollingHelper.onEnable(polling, {
			auth: context.auth,
			store: context.store,
			propsValue: context.propsValue,
		});
	},
	async onDisable(context) {
		await pollingHelper.onDisable(polling, {
			auth: context.auth,
			store: context.store,
			propsValue: context.propsValue,
		});
	},
	async test(context) {
		return await pollingHelper.test(polling, context);
	},
	async run(context) {
		return await pollingHelper.poll(polling, context);
	},
	sampleData: {
		'@odata.etag': 'W/"vVwdQvxCiE6779iYhchMrAAGgwrb4w=="',
		importance: 'normal',
		isReminderOn: false,
		status: 'notStarted',
		title: 'fdfdfd',
		createdDateTime: '2025-05-08T12:40:55.3080693Z',
		lastModifiedDateTime: '2025-05-08T12:40:55.3533435Z',
		hasAttachments: false,
		categories: [],
		id: 'AQMkADAwATM3ZmYAZS0xNGVmLWNiZmYALTAwAi0wMAoARgAAAw8tTPoZEYtLvE5mK48wuvIHAL1cHUL8QohOu_-YmIXITKwABoMc5_AAAAC9XB1C-EKITrvv2JiFyEysAAaDHTv2AAAA',
		body: {
			content: '',
			contentType: 'text',
		},
	},
});
