import { createAction, Property } from '@activepieces/pieces-framework';
import { NinoxAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';
import {
	teamidDropdown,
	databaseIdDropdown,
	tableIdDropdown,
	recordIdDropdown,
} from '../common/props';
import mime from 'mime-types';

export const uploadFile = createAction({
	auth: NinoxAuth,
	name: 'uploadFile',
	displayName: 'Upload File',
	description: 'Attach a file to a record.',
	audience: 'both',
	aiMetadata: {
		description: 'Attaches a file as an upload to one Ninox record, addressed by team, database, table, and record id. Use to add a file attachment to an existing record. Not idempotent — each call appends another attachment.',
		idempotent: false,
	},
	props: {
		teamid: teamidDropdown,
		dbid: databaseIdDropdown,
		tid: tableIdDropdown,
		rid: recordIdDropdown,
		file: Property.File({
			displayName: 'File',
			description: 'The file to upload to the record.',
			required: true,
		}),
	},
	async run({ auth, propsValue }) {
		const { teamid, dbid, tid, rid, file } = propsValue;

		const path = `/teams/${teamid}/databases/${dbid}/tables/${tid}/records/${rid}/files`;

		try {
			// Determine MIME type from file extension
			const mimeType = file.extension
				? mime.lookup(file.extension) || 'application/octet-stream'
				: 'application/octet-stream';

			// Create FormData for multipart/form-data upload
			const formData = new FormData();
			const blob = new Blob([file.data as unknown as ArrayBuffer], { type: mimeType });
			formData.append('file', blob, file.filename);

			const response = await makeRequest(
				auth.secret_text,
				HttpMethod.POST,
				path,
				formData,
				'multipart/form-data',
			);
			return {
				success: true,
				response: response,
			};
		} catch (error) {
			throw new Error(`Failed to upload file: ${error}`);
		}
	},
});
