import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { feedhiveAuth } from '../common/auth';
import { feedhiveCommon } from '../common';

export const createPostAction = createAction({
  auth: feedhiveAuth,
  name: 'create_post',
  displayName: 'Create Post',
  description: 'Creates a new post in FeedHive (draft or scheduled).',
  audience: 'both',
  aiMetadata: { description: 'Creates a new FeedHive social media post from provided text, either saved as a draft or scheduled for a chosen publish date (set Status to "scheduled" and supply a date/time). Use to author and queue content for one or more connected social accounts. Each call creates a separate post, so it is not idempotent.', idempotent: false },
  props: {
    text: Property.LongText({
      displayName: 'Post Text',
      description: 'The main content of your post.',
      required: true,
    }),
    status: Property.StaticDropdown({
      displayName: 'Status',
      description: 'Choose "Draft" to save without scheduling, or "Scheduled" to set a publish date.',
      required: true,
      defaultValue: 'draft',
      options: {
        options: [
          { label: 'Draft', value: 'draft' },
          { label: 'Scheduled', value: 'scheduled' },
        ],
      },
    }),
    scheduled_at: Property.DateTime({
      displayName: 'Scheduled Date & Time',
      description: 'Required when Status is "Scheduled". The exact date and time the post should be published.',
      required: false,
    }),
    accounts: feedhiveCommon.socialsMultiDropdown,
    labels: feedhiveCommon.labelsMultiDropdown,
    notes: Property.LongText({
      displayName: 'Internal Notes',
      description: 'Private notes visible only to your team in FeedHive. Not published.',
      required: false,
    }),
    short_link_enabled: Property.Checkbox({
      displayName: 'Enable Short Links',
      description: 'Automatically shorten URLs in your post text.',
      required: false,
      defaultValue: false,
    }),
  },
  async run(context) {
    const { text, status, scheduled_at, accounts, labels, notes, short_link_enabled } =
      context.propsValue;

    if (status === 'scheduled' && !scheduled_at) {
      throw new Error('Scheduled Date & Time is required when Status is set to "Scheduled".');
    }

    const body: Record<string, unknown> = { text, status };
    if (accounts && accounts.length > 0) body['accounts'] = accounts;
    if (labels && labels.length > 0) body['labels'] = labels;
    if (scheduled_at) body['scheduled_at'] = scheduled_at;
    if (notes) body['notes'] = notes;
    if (short_link_enabled !== undefined) body['short_link_enabled'] = short_link_enabled;

    const response = await feedhiveCommon.apiCall<{ data: Record<string, unknown> }>({
      token: context.auth.secret_text,
      method: HttpMethod.POST,
      path: '/posts',
      body,
    });

    return feedhiveCommon.flattenPost(response.body.data);
  },
});
