import { createAction, Property } from '@activepieces/pieces-framework';
import { placidAuth } from '../auth';
import { PlacidClient } from '../common/client';
import {
	videoTemplateDropdown,
	webhookProperty,
	createNowProperty,
	passthroughProperty,
	templateLayersProperty,
} from '../common/props';
import { isNil } from '@activepieces/pieces-framework';
import { PlacidCreateVideoRequest } from '../common';

export const createVideo = createAction({
	auth: placidAuth,
	name: 'create_video',
	displayName: 'Create Video',
	description: 'Produces a video based on a template.',
	audience: 'both',
	aiMetadata: { description: 'Render a video from a Placid template by filling its layers with provided data; choose this to produce a rendered video asset from a template. Requires a template UUID. By default the render is queued and returns immediately; enabling create_now blocks and polls until the video finishes. Not idempotent — each call queues a new render job.', idempotent: false },
	props: {
		template: videoTemplateDropdown,
		layers: templateLayersProperty('video'),
		outputFps: Property.Number({
			displayName: 'Output FPS',
			required: false,
			defaultValue: 25,
		}),
		outputFilename: Property.ShortText({
			displayName: 'Output File Name',
			required: false,
		}),
		webhook_success: webhookProperty,
		create_now: createNowProperty,
		passthrough: passthroughProperty,
	},
	async run(context) {
		const { template, outputFps, outputFilename, webhook_success, create_now, passthrough } =
			context.propsValue;

		const layers = context.propsValue.layers ?? {};

		const modifiedLayers: Record<string, any> = {};

		for (const [key, value] of Object.entries(layers)) {
			if (value === '' || isNil(value)) continue;

			const [mainKey, subKey] = key.split(':::');
			if (!mainKey || !subKey) continue;

			if (!modifiedLayers[mainKey]) {
				modifiedLayers[mainKey] = {};
			}

			modifiedLayers[mainKey][subKey] = value;
		}

		const client = new PlacidClient(context.auth);

		const modifications = {
			...(outputFilename && { filename: outputFilename }),
			...(outputFps && { fps: outputFps }),
		};

		// Videos require a clips array structure
		const request: PlacidCreateVideoRequest = {
			clips: [
				{
					template_uuid: template,
					...(modifiedLayers && { layers: modifiedLayers }),
				},
			],
			...(Object.keys(modifications).length && { modifications }),
			...(webhook_success && { webhook_success }),
			...(create_now !== undefined && { create_now }),
			...(passthrough && { passthrough }),
		};

		return await client.createVideo(request);
	},
});
