import { HttpMethod, QueryParams } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { clockifyAuth } from '../auth';
import { clockifyApiCall } from '../common/client';
import { projectId, taskId, workspaceId } from '../common/props';

export const findTimeEntryAction = createAction({
	auth: clockifyAuth,
	name: 'find-time-entry',
	displayName: 'Find Time Entry',
	description: 'Finds a time entry by description, start datetime or end datetime.',
	audience: 'both',
	aiMetadata: {
		description:
			"Searches the authenticated user's time entries on a workspace, optionally filtered by description, start/end datetime, project, or task. With no filters it returns recent entries; supplied filters narrow the results. Use to locate an existing entry or verify logged time. Read-only and idempotent.",
		idempotent: true,
	},
	props: {
		workspaceId: workspaceId({
			displayName: 'Workspace',
			required: true,
		}),
		start: Property.DateTime({
			displayName: 'Start Datetime',
			required: false,
		}),
		end: Property.DateTime({
			displayName: 'End Datetime',
			required: false,
		}),
		description: Property.LongText({
			displayName: 'Entry Description',
			required: false,
		}),
		projectId: projectId({
			displayName: 'Project',
			required: false,
		}),
		taskId: taskId({
			displayName: 'Task',
			required: false,
		}),
	},
	async run(context) {
		const { workspaceId, projectId, start, end, description, taskId } = context.propsValue;

		const currentUserResponse = await clockifyApiCall<{ id: string; email: string }>({
			apiKey: context.auth.secret_text,
			method: HttpMethod.GET,
			resourceUri: `/user`,
		});

		const userId = currentUserResponse.id;

		const qs: QueryParams = { hydrated: 'true' };

		if (description) qs['description'] = description;
		if (start) qs['start'] = start;
		if (end) qs['end'] = end;
		if (projectId) qs['project'] = projectId as string;
		if (taskId) qs['task'] = taskId as string;

		const response = await clockifyApiCall({
			apiKey: context.auth.secret_text,
			method: HttpMethod.GET,
			resourceUri: `/workspaces/${workspaceId}/user/${userId}/time-entries`,
			query: qs,
		});

		return response;
	},
});
