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

export const findRunningTimerAction = createAction({
	auth: clockifyAuth,
	name: 'find-running-timer',
	displayName: 'Find Running Timer',
	description: 'Finds currently running timer on specified workspace.',
	audience: 'both',
	aiMetadata: {
		description:
			'Returns the in-progress (currently running) time entries for the authenticated user on the given workspace. Use to check whether a timer is active before starting or stopping one. Read-only and idempotent.',
		idempotent: true,
	},
	props: {
		workspaceId: workspaceId({
			displayName: 'Workspace',
			required: true,
		}),
	},
	async run(context) {
		const { workspaceId } = context.propsValue;

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

		return response;
	},
});
