import { createAction, Property } from '@activepieces/pieces-framework';
import { jiraDataCenterAuth } from '../../auth';
import { HttpMethod } from '@activepieces/pieces-common';
import { sendJiraRequest } from '../common';
import FormData from 'form-data';
import { getProjectIdDropdown, getIssueIdDropdown } from '../common/props';

export const addAttachmentToIssueAction = createAction({
	auth: jiraDataCenterAuth,
	name: 'add_issue_attachment',
	displayName: 'Add Attachment to Issue',
	description: 'Adds an attachment to an issue.',
	audience: 'both',
	aiMetadata: {
		description:
			'Uploads a file and attaches it to a Jira Data Center/Server issue. Use to add a document, image, or log to an existing issue. Not idempotent — each call adds another attachment, even if the same file was already attached.',
		idempotent: false,
	},
	props: {
		projectId: getProjectIdDropdown(),
		issueId: getIssueIdDropdown({ refreshers: ['projectId'] }),
		attachment: Property.File({
			displayName: 'Attachment',
			required: true,
		}),
	},
	async run(context) {
		const { issueId, attachment } = context.propsValue;
		const formData = new FormData();
		const fileBuffer = Buffer.from(attachment.base64, 'base64');
		formData.append('file', fileBuffer, attachment.filename);

		const response = await sendJiraRequest({
			method: HttpMethod.POST,
			url: `issue/${issueId}/attachments`,
			auth: context.auth,
			headers: {
				'X-Atlassian-Token': 'no-check',
				...formData.getHeaders(),
			},
			body: formData,
		});
		return response.body;
	},
});
