import { createAction, Property } from '@activepieces/pieces-framework';
import { aidbaseAuth } from '../common/auth';
import { aidbaseClient } from '../common/client';

export const createFaq = createAction({
  auth: aidbaseAuth,
  name: 'create_faq',
  displayName: 'Create FAQ',
  description: 'Creates a new FAQ entry with title and description.',
  audience: 'both',
  aiMetadata: {
    description:
      'Creates a new, empty FAQ knowledge base in Aidbase identified by a title (description optional). Use this first to obtain an FAQ id before adding entries with Add FAQ Item. Not idempotent: each call creates a separate FAQ even with the same title.',
    idempotent: false,
  },

  props: {
    title: Property.ShortText({
      displayName: 'Title',
      description: 'The title of the new FAQ knowledge base.',
      required: true,
    }),
    description: Property.LongText({
      displayName: 'Description',
      description: 'An optional description for the FAQ.',
      required: false,
    }),
  },

  async run(context) {
    const { auth: apiKey, propsValue } = context;
    const { title, description } = propsValue;

    return await aidbaseClient.createFaq(apiKey.secret_text, {
      title,
      description,
    });
  },
});
