import { confluenceAuth } from '../auth';
import { createAction, Property } from "@activepieces/pieces-framework";
import { folderIdProp, spaceIdProp, templateIdProp, templateVariablesProp } from "../common/props";
import { confluenceApiCall, escapeStorageValue } from "../common";
import { HttpMethod } from "@activepieces/pieces-common";

export const createPageFromTemplateAction = createAction({
    auth:confluenceAuth,
    name:'create-page-from-template',
    displayName:'Create Page from Template',
    description:'Creates a new page from a template with the given title and variables.',
    audience: 'both',
    aiMetadata: { description: 'Creates a new Confluence page by fetching a template, substituting its declared variables with the provided values, and posting the rendered content as a page (published or draft) in the given space. Use when a page should follow a predefined template rather than free-form body. Requires the template ID and matching variable names. Not idempotent: each call creates another page.', idempotent: false },
    props:{
        spaceId:spaceIdProp,
        templateId:templateIdProp,
        folderId:folderIdProp,
        title:Property.ShortText({
            displayName:'Title',
            required:true,
        }),
        status:Property.StaticDropdown({
            displayName:'Status',
            required:true,
            defaultValue:'draft',
            options:{
                disabled:false,
                options:[
                    {
                        label:'Published ',
                        value:'current'
                    },
                    {
                        label:'Draft',
                        value:'draft'
                    }
                ]
            }
        }),
        templateVariables:templateVariablesProp,
    },
    async run(context){
        const {spaceId,templateId,title,status,folderId} = context.propsValue;
        const variables = context.propsValue.templateVariables ??{};

        const template = await confluenceApiCall<{ body: { storage: { value: string } } }>({
                    domain: context.auth.props.confluenceDomain,
                    username: context.auth.props.username,
                    password: context.auth.props.password,
                    method: HttpMethod.GET,
                    version: 'v1',
                    resourceUri: `/template/${templateId}`,
                });
        
        const body = template.body.storage.value;

        let content = body.replace(/<at:declarations>[\s\S]*?<\/at:declarations>/, "").trim();
        Object.entries(variables).forEach(([key, value]) => {
            const varRegex = new RegExp(`<at:var at:name=(['"])${key}\\1\\s*\\/?>`, "g");
            const safeValue = typeof value === 'string' ? escapeStorageValue(value) : '';
            content = content.replace(varRegex, safeValue);
          });

        const response = await confluenceApiCall({
            domain: context.auth.props.confluenceDomain,
            username: context.auth.props.username,
            password: context.auth.props.password,
            method: HttpMethod.POST,
            version: 'v2',
            resourceUri: '/pages',
            body: {
                spaceId:spaceId,
                title,
                parentId:folderId,
                status,
                body:{
                    representation:'storage',
                    value:content,
                }
            }
        })

        return response;
    }
})