import {
  httpClient,
  HttpMethod,
  propsValidation,
} from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import * as z from 'zod/mini'
import { joggAiAuth } from '../..';

export const createAiAvatarPhoto = createAction({
  name: 'createAiAvatarPhoto',
  displayName: 'Create AI Avatar Photo',
  description: 'Creates an AI avatar photo using JoggAI API',
  audience: 'both',
  aiMetadata: {
    description:
      'Generates a brand-new AI avatar photo on JoggAI from specified traits (age, gender, style, ethnicity, aspect ratio, model) plus optional appearance/background text or a reference image URL. Use to produce an avatar still image for downstream avatar-video creation. Not idempotent: each call generates and is billed for a new photo.',
    idempotent: false,
  },
  auth: joggAiAuth,
  props: {
    age: Property.StaticDropdown({
      displayName: 'Age',
      description: 'The age group for the avatar',
      required: true,
      options: {
        options: [
          { label: 'Teenager', value: 'Teenager' },
          { label: 'Young adult', value: 'Young adult' },
          { label: 'Adult', value: 'Adult' },
          { label: 'Elderly', value: 'Elderly' },
        ],
      },
    }),
    appearance: Property.LongText({
      displayName: 'Appearance',
      description: 'Description of the appearance',
      required: false,
    }),
    aspect_ratio: Property.StaticDropdown({
      displayName: 'Aspect Ratio',
      description: 'Photo aspect ratio',
      required: true,
      options: {
        options: [
          { label: 'Portrait [9:16]', value: 0 },
          { label: 'Landscape [16:9]', value: 1 },
        ],
      },
    }),
    avatar_style: Property.StaticDropdown({
      displayName: 'Avatar Style',
      description: 'Style of the avatar',
      required: true,
      options: {
        options: [
          { label: 'Professional', value: 'Professional' },
          { label: 'Social', value: 'Social' },
        ],
      },
    }),
    background: Property.LongText({
      displayName: 'Background',
      description: 'Description of the background',
      required: false,
    }),
    ethnicity: Property.StaticDropdown({
      displayName: 'Ethnicity',
      description: 'The ethnicity for the avatar',
      required: false,
      options: {
        options: [
          { label: 'European', value: 'European' },
          { label: 'African', value: 'African' },
          { label: 'South Asian', value: 'South Asian' },
          { label: 'East Asian', value: 'East Asian' },
          { label: 'Middle Eastern', value: 'Middle Eastern' },
          { label: 'South American', value: 'South American' },
          { label: 'North American', value: 'North American' },
        ],
      },
    }),
    gender: Property.StaticDropdown({
      displayName: 'Gender',
      description: 'The gender for the avatar',
      required: true,
      options: {
        options: [
          { label: 'Female', value: 'Female' },
          { label: 'Male', value: 'Male' },
        ],
      },
    }),
    image_url: Property.ShortText({
      displayName: 'Image URL',
      description: 'URL of an existing image to use as reference',
      required: false,
    }),
    model: Property.StaticDropdown({
      displayName: 'Model',
      description: 'The AI model to use',
      required: true,
      options: {
        options: [
          { label: 'Classic', value: 'classic' },
          { label: 'Modern', value: 'modern' },
        ],
      },
    }),
  },

  async run({ auth, propsValue }) {
    const {
      age,
      appearance,
      aspect_ratio,
      avatar_style,
      background,
      ethnicity,
      gender,
      image_url,
      model,
    } = propsValue;

    await propsValidation.validateZod(propsValue, {
      image_url: z.optional(z.string().check(z.url('Image URL must be a valid URL'))),
      appearance: z.optional(z.string().check(z.minLength(1, 'Appearance description cannot be empty'))),
      background: z.optional(z.string().check(z.minLength(1, 'Background description cannot be empty'))),
    });

    const requestBody: {
      age: string;
      aspect_ratio: number;
      avatar_style: string;
      gender: string;
      model: string;
      appearance?: string;
      background?: string;
      ethnicity?: string;
      image_url?: string;
    } = {
      age,
      aspect_ratio,
      avatar_style,
      gender,
      model,
    };

    if (appearance) {
      requestBody.appearance = appearance;
    }
    if (background) {
      requestBody.background = background;
    }
    if (ethnicity) {
      requestBody.ethnicity = ethnicity;
    }
    if (image_url) {
      requestBody.image_url = image_url;
    }

    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: 'https://api.jogg.ai/v1/photo_avatar/photo/generate',
      headers: {
        'x-api-key': auth.secret_text,
        'Content-Type': 'application/json',
      },
      body: requestBody,
    });

    if (response.body.code !== 0) {
      const errorMessages: Record<number, string> = {
        10104: 'Record not found',
        10105: 'Invalid API key',
        18020: 'Insufficient credit',
        18025: 'No permission to call APIs',
        40000: 'Parameter error',
        50000: 'System error',
      };

      const message =
        errorMessages[response.body.code] || `API Error: ${response.body.msg}`;
      throw new Error(message);
    }

    return response.body;
  },
});
