import { createTrigger, Property, TriggerStrategy } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { autocallsAuth } from '../..';
import { baseApiUrl } from '../..';

export const inboundCall = createTrigger({
    auth:autocallsAuth,
    name: 'inboundCall',
    displayName: 'Inbound Call',
    description: 'Triggers for variables before connecting an inbound call.',
    aiMetadata: {
      description: 'Fires via webhook when an inbound call reaches the selected Autocalls assistant, before the call is connected, providing the caller and assistant phone numbers so the flow can supply variables for the conversation.',
    },
    props: {
        assistant: Property.Dropdown({
            auth: autocallsAuth,
            displayName: 'Assistant',
            description: 'Select an assistant',
            required: true,
            refreshers: ['auth'],
            refreshOnSearch: false,
            options: async ({ auth }) => {
                const res = await httpClient.sendRequest({
                    method: HttpMethod.GET,
                    url: baseApiUrl + 'api/user/assistants',
                    headers: {
                        Authorization: "Bearer " + auth?.secret_text,
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
                });

                if (res.status !== 200) {
                    return {
                        disabled: true,
                        placeholder: 'Error fetching assistants',
                        options: [],
                    };
                } else if (res.body.length === 0) {
                    return {
                        disabled: true,
                        placeholder: 'No assistants found. Create one first.',
                        options: [],
                    };
                }

                return {
                    options: res.body.map((assistant: any) => ({
                        value: assistant.id,
                        label: assistant.name,
                    })),
                };
            }
        }),
    },
    sampleData: {
        customer_phone: '+16380991171',
        assistant_phone: '+16380991171',
    },
    type: TriggerStrategy.WEBHOOK,
    async onEnable(context) {
        await httpClient.sendRequest({
            method: HttpMethod.POST,
            url: baseApiUrl + 'api/user/assistants/enable-inbound-webhook',
            body: {
                assistant_id: context.propsValue['assistant'],
                webhook_url: context.webhookUrl,
            },
            headers: {
                Authorization: "Bearer " + context.auth.secret_text,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            },
        });
    },
    async onDisable(context) {
        await httpClient.sendRequest({
            method: HttpMethod.POST,
            url: baseApiUrl + 'api/user/assistants/disable-inbound-webhook',
            body: {
                assistant_id: context.propsValue['assistant'],
            },
            headers: {
                Authorization: "Bearer " + context.auth.secret_text,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            },
        });
    },
    async run(context) {
        return [context.payload.body]
    }
})
