import { Property, createAction, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import { getTaskListsDropdown, createTodoClient } from '../common';
import { microsoftToDoAuth } from '../auth';
import { PageCollection } from '@microsoft/microsoft-graph-client';

import { TodoTask } from '@microsoft/microsoft-graph-types';

export const findTaskByTitleAction = createAction({
	auth: microsoftToDoAuth,
	name: 'find_task_by_title',
	displayName: 'Find Task',
	description: 'Finds tasks by title.',
	audience: 'both',
	aiMetadata: { description: 'Search a specific Microsoft To Do task list for tasks whose title matches the given text and return all matches. The match mode is configurable — contains (default), starts-with, or exact. Use to resolve a task id from its title before reading, updating, completing, or deleting it. Requires a task list id; read-only and idempotent.', idempotent: true },
	props: {
		task_list_id: Property.Dropdown({
   auth: microsoftToDoAuth,
			displayName: 'Task List',
			description: 'Select a specific task list to search within.',
			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);
			},
		}),
		title: Property.ShortText({
			displayName: 'Task Title',
			description: 'The title (or partial title) of the task to find.',
			required: true,
		}),

		match_type: Property.StaticDropdown({
			displayName: 'Match Type',
			description: 'How to match the task title.',
			required: false,
			defaultValue: 'contains',
			options: {
				options: [
					{ label: 'Contains', value: 'contains' },
					{ label: 'Starts With', value: 'startsWith' },
					{ label: 'Exact Match', value: 'exact' },
				],
			},
		}),
	},
	async run(context) {
		const { auth, propsValue } = context;
		const { title, task_list_id, match_type } = propsValue;

		const client = createTodoClient(auth);

		let titleFilterString = '';
		switch (match_type) {
			case 'startsWith':
				titleFilterString = `startsWith(title, '${title}')`;
				break;
			case 'exact':
				titleFilterString = `title eq '${title}'`;
				break;
			case 'contains':
			default:
				titleFilterString = `contains(title, '${title}')`;
				break;
		}

		const result = [];

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

		while (response.value.length > 0) {
			for (const task of response.value as TodoTask[]) {
				result.push(task);
			}
			if (response['@odata.nextLink']) {
				response = await client.api(response['@odata.nextLink']).get();
			} else {
				break;
			}
		}

		return result;
	},
});
