import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { chatlingAuth } from '../auth';
import { makeRequest } from '../common';
import {
  chatbotIdDropdown,
  aiModelIdDropdown,
  languageIdDropdown,
  contactIdDropdown,
  conversationIdDropdown,
} from '../common/props';

export const sendMessage = createAction({
  auth: chatlingAuth,
  name: 'send_message',
  displayName: 'Send Message',
  description: 'Send a message to the chatbot and receive an AI response.',
  audience: 'both',
  aiMetadata: {
    description:
      'Send a user message to a specific Chatling chatbot and get back the AI-generated reply, querying the bot\'s knowledge base. Use to converse with or test a deployed chatbot programmatically. Requires the chatbot ID and message; optionally pass a conversation_id to continue an existing thread (omit it to start a new one) and tune the AI model, language, temperature, or extra instructions. Not idempotent — each call generates a fresh response and appends to the conversation.',
    idempotent: false,
  },
  props: {
    chatbotId: chatbotIdDropdown,
    message: Property.LongText({
      displayName: 'Message',
      description: 'The message to send to the AI',
      required: true,
    }),
    ai_model_id: aiModelIdDropdown,
    conversation_id: conversationIdDropdown,
    contact_id: contactIdDropdown,
    language_id: languageIdDropdown,
    temperature: Property.Number({
      displayName: 'Temperature',
      description:
        'Controls randomness (0 = focused, 1 = creative). Default is 0.',
      required: false,
      defaultValue: 0,
    }),
    instructions: Property.Array({
      displayName: 'Instructions',
      description: 'Additional instructions to tailor the AI response',
      required: false,
    }),
  },

  async run(context) {
    const {
      chatbotId,
      message,
      ai_model_id,
      conversation_id,
      contact_id,
      language_id,
      temperature,
      instructions,
    } = context.propsValue;

    const apiKey = context.auth.secret_text;

    const body: Record<string, unknown> = {
      message,
      ai_model_id,
    };

    if (conversation_id) body['conversation_id'] = conversation_id;
    if (contact_id) body['contact_id'] = contact_id;
    if (language_id) body['language_id'] = language_id;
    if (temperature !== undefined && temperature !== null)
      body['temperature'] = temperature;
    if (instructions && instructions.length > 0)
      body['instructions'] = instructions;

    const response = await makeRequest(
      apiKey,
      HttpMethod.POST,
      `/chatbots/${chatbotId}/ai/kb/chat`,
      body
    );

    return response;
  },
});

