import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { senjaAuth } from '../../';
import { senjaApiCall, INTEGRATION_OPTIONS, mapTestimonial } from '../common';

export const createTestimonialAction = createAction({
  auth: senjaAuth,
  name: 'create_testimonial',
  displayName: 'Create Testimonial',
  description: 'Add a new testimonial to your Senja project.',
  audience: 'both',
  aiMetadata: { description: 'Creates a new testimonial in a Senja project, either a text or a video testimonial (set by type). Use to import or record a customer testimonial along with its customer details, rating, tags, and source. Requires type and customer name; text needs body text and video needs a video URL. Not idempotent — each call creates a separate testimonial.', idempotent: false },
  props: {
    type: Property.StaticDropdown({
      displayName: 'Testimonial Type',
      description:
        'Choose "Text" for a written testimonial or "Video" for a video testimonial.',
      required: true,
      options: {
        options: [
          { label: 'Text', value: 'text' },
          { label: 'Video', value: 'video' },
        ],
      },
    }),
    customer_name: Property.ShortText({
      displayName: 'Customer Name',
      description: 'Full name of the person giving the testimonial.',
      required: true,
    }),
    title: Property.ShortText({
      displayName: 'Title',
      description: 'A short headline for the testimonial.',
      required: false,
    }),
    text: Property.LongText({
      displayName: 'Testimonial Text',
      description:
        'The body of the testimonial. Required for text testimonials.',
      required: false,
    }),
    rating: Property.Number({
      displayName: 'Rating',
      description: 'Star rating from 1 (lowest) to 5 (highest).',
      required: false,
    }),
    approved: Property.Checkbox({
      displayName: 'Approved',
      description:
        'Check to mark the testimonial as approved immediately. Leave unchecked to keep it pending.',
      required: false,
      defaultValue: false,
    }),
    date: Property.DateTime({
      displayName: 'Date',
      description:
        "Date the testimonial was given. Leave empty to use today's date.",
      required: false,
    }),
    url: Property.ShortText({
      displayName: 'Source URL',
      description:
        'Link to the original testimonial (e.g. a tweet URL, Google review link).',
      required: false,
    }),
    video_url: Property.ShortText({
      displayName: 'Video URL',
      description:
        'URL of the video testimonial. Required when Testimonial Type is "Video".',
      required: false,
    }),
    customer_email: Property.ShortText({
      displayName: 'Customer Email',
      description: 'Email address of the customer.',
      required: false,
    }),
    customer_company: Property.ShortText({
      displayName: 'Customer Company',
      description: 'Company or organisation the customer belongs to.',
      required: false,
    }),
    customer_tagline: Property.ShortText({
      displayName: 'Customer Tagline',
      description:
        'Short role or tagline for the customer (e.g. "CEO at Acme").',
      required: false,
    }),
    customer_username: Property.ShortText({
      displayName: 'Customer Username',
      description:
        'Social media handle or username of the customer (e.g. "@johndoe").',
      required: false,
    }),
    customer_url: Property.ShortText({
      displayName: 'Customer Profile URL',
      description: "Link to the customer's social profile or website.",
      required: false,
    }),
    customer_avatar: Property.ShortText({
      displayName: 'Customer Avatar URL',
      description: "Direct URL to the customer's profile picture.",
      required: false,
    }),
    customer_company_logo: Property.ShortText({
      displayName: 'Company Logo URL',
      description: "Direct URL to the customer's company logo.",
      required: false,
    }),
    integration: Property.StaticDropdown({
      displayName: 'Source / Integration',
      description: 'The platform where this testimonial originally came from.',
      required: false,
      options: {
        options: INTEGRATION_OPTIONS,
      },
    }),
    tags: Property.Array({
      displayName: 'Tags',
      description:
        'Tags to attach to this testimonial for organisation and filtering.',
      required: false,
    }),
    thumbnail_url: Property.ShortText({
      displayName: 'Thumbnail URL',
      description: 'URL of the thumbnail image for this testimonial.',
      required: false,
    }),
    form_id: Property.ShortText({
      displayName: 'Form ID',
      description: 'ID of the Senja form to associate this testimonial with.',
      required: false,
    }),
    media: Property.Array({
      displayName: 'Media (Images)',
      description:
        'List of image URLs to attach to this testimonial. Each entry must be a JSON object with a "url" field and optionally "alt" text, e.g. {"url": "https://...", "alt": "description"}.',
      required: false,
    }),
  },
  async run(context) {
    const {
      type,
      customer_name,
      title,
      text,
      rating,
      approved,
      date,
      url,
      video_url,
      customer_email,
      customer_company,
      customer_tagline,
      customer_username,
      customer_url,
      customer_avatar,
      customer_company_logo,
      integration,
      tags,
      thumbnail_url,
      form_id,
      media,
    } = context.propsValue;

    const body: Record<string, unknown> = {
      type,
      customer_name,
    };

    if (title) body['title'] = title;
    if (text) body['text'] = text;
    if (rating !== undefined && rating !== null) body['rating'] = rating;
    if (approved !== undefined && approved !== null)
      body['approved'] = approved;
    if (date) body['date'] = date;
    if (url) body['url'] = url;
    if (video_url) body['video_url'] = video_url;
    if (customer_email) body['customer_email'] = customer_email;
    if (customer_company) body['customer_company'] = customer_company;
    if (customer_tagline) body['customer_tagline'] = customer_tagline;
    if (customer_username) body['customer_username'] = customer_username;
    if (customer_url) body['customer_url'] = customer_url;
    if (customer_avatar) body['customer_avatar'] = customer_avatar;
    if (customer_company_logo)
      body['customer_company_logo'] = customer_company_logo;
    if (integration) body['integration'] = integration;
    if (tags && Array.isArray(tags) && tags.length > 0) body['tags'] = tags;
    if (thumbnail_url) body['thumbnail_url'] = thumbnail_url;
    if (form_id) body['form_id'] = form_id;
    if (media && Array.isArray(media) && media.length > 0) {
      body['media'] = media.map((item) => {
        if (typeof item === 'string') {
          try { return JSON.parse(item); } catch { return { url: item }; }
        }
        return item;
      });
    }

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

    return mapTestimonial({ testimonial: response.body });
  },
});
