import { createAction, Property } from '@activepieces/pieces-framework';
import {
  assistantProp,
  usernameProp,
  timezoneProp,
  getConversationContent,
  timeoutProp,
  createClient,
} from '../common';
import { dustAuth } from '../..';
import mime from 'mime-types';
import { PublicPostConversationsRequestBody } from '@dust-tt/client';

export const createConversation = createAction({
  // auth: check https://www.activepieces.com/docs/developers/piece-reference/authentication,
  name: 'createConversation',
  displayName: 'Create conversation',
  description: 'Create a new conversation with a specific Dust assistant',
  audience: 'both',
  aiMetadata: {
    description:
      'Start a new Dust conversation with a chosen assistant; pass a query to send the first message and have the assistant respond, or omit it to open an empty conversation for later replies. Optionally attach context by passing an uploaded File ID (takes precedence) or an inline file fragment. Each call creates a distinct conversation, so it is not idempotent.',
    idempotent: false,
  },
  auth: dustAuth,
  props: {
    assistant: assistantProp,
    username: usernameProp,
    timezone: timezoneProp,
    title: Property.ShortText({ displayName: 'Title', required: false }),
    query: Property.LongText({ displayName: 'Query', required: false }),
    fragment: Property.File({ displayName: 'Fragment', required: false }),
    fileId: Property.ShortText({
      displayName: 'File ID',
      required: false,
      description:
        'ID of the file to be added to the conversation (takes precedence over Fragment)',
    }),
    fragmentName: Property.ShortText({
      displayName: 'Fragment/file name',
      required: false,
    }),
    timeout: timeoutProp,
  },
  async run({ auth, propsValue }) {
    const client = createClient(auth.props);

    const payload: PublicPostConversationsRequestBody = {
      visibility: 'unlisted',
      title: propsValue.title || null,
    };

    if (propsValue.query) {
      payload.message = {
        content: propsValue.query,
        mentions: [{ configurationId: propsValue.assistant }],
        context: {
          timezone: propsValue.timezone,
          username: propsValue.username,
          email: null,
          fullName: null,
          profilePictureUrl: null,
        },
      };
    }

    if (propsValue.fileId) {
      payload.contentFragment = {
        title: propsValue.fragmentName || '',
        fileId: propsValue.fileId,
      };
    } else if (propsValue.fragment) {
      const mimeType = propsValue.fragmentName
        ? mime.lookup(propsValue.fragmentName) ||
          mime.lookup(propsValue.fragment.filename)
        : mime.lookup(propsValue.fragment.filename);
      payload.contentFragment = {
        title: propsValue.fragmentName || propsValue.fragment.filename,
        content: propsValue.fragment.data.toString('utf-8'),
        contentType: mimeType || 'text/plain',
        context: null,
        url: null,
      };
    }

    const response = await client.createConversation(payload);

    if (response.isErr()) {
      throw new Error(`API Error: ${response.error.message}`);
    }

    const conversationId = response.value.conversation.sId;
    if (propsValue.query) {
      return await getConversationContent(
        conversationId,
        propsValue.timeout,
        auth.props
      );
    } else {
      return response.value;
    }
  },
});
