import { Container } from '@n8n/di';

import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { EventService } from '@/events/event.service';
import { WorkflowHistoryService } from '@/workflows/workflow-history/workflow-history.service';

import type { WorkflowRequest } from '../../../types';
import type { PublicAPIEndpoint } from '../../shared/handler.types';
import {
	publicApiScope,
	projectScope,
	deprecated,
} from '../../shared/middlewares/global.middleware';

const VERSION_PATH_DEPRECATED_SINCE = new Date('2026-08-26T00:00:00Z');

type WorkflowHandlers = {
	getWorkflowVersion: PublicAPIEndpoint<WorkflowRequest.GetVersion>;
};

const workflowHandlers: WorkflowHandlers = {
	getWorkflowVersion: [
		deprecated({ since: VERSION_PATH_DEPRECATED_SINCE }),
		publicApiScope('workflow:read'),
		projectScope('workflow:read', 'workflow'),
		async (req, res) => {
			const { id: workflowId, versionId } = req.params;

			try {
				const version = await Container.get(WorkflowHistoryService).getVersion(
					req.user,
					workflowId,
					versionId,
					{ includePublishHistory: false },
				);

				Container.get(EventService).emit('user-retrieved-workflow-version', {
					userId: req.user.id,
					publicApi: true,
				});

				const { autosaved, ...versionWithoutInternalFields } = version;

				return res.json(versionWithoutInternalFields);
			} catch {
				throw new NotFoundError('Version not found');
			}
		},
	],
};

export = workflowHandlers;
