import { clarifaiAuth } from '../auth';
import { Property, createAction } from '@activepieces/pieces-framework';
import {
  CommonClarifaiProps,
  callClarifaiModel,
  cleanMultiOutputResponse,
  fileToInput,
} from '../common';
import { Data } from 'clarifai-nodejs-grpc/proto/clarifai/api/resources_pb';

export const visualClassifierModelPredictAction = createAction({
  auth: clarifaiAuth,
  name: 'visual_classifier_model',
  description: 'Call an visual classifier AI model to recognize concepts',
  audience: 'both',
  aiMetadata: {
    description:
      'Runs an image or video through a Clarifai visual classification model and returns the recognized concepts with confidence scores. Use to tag, categorize, or detect content in visual media; supply the model URL and the file as a URL or base64 bytes. Read-only inference, safe to repeat.',
    idempotent: true,
  },
  displayName: 'Classify Images or Videos',
  props: {
    modelUrl: CommonClarifaiProps.modelUrl,
    file: Property.File({
      description: 'URL or base64 bytes of the image or video to classify',
      displayName: 'Input URL or bytes',
      required: true,
    }),
  },
  async run(ctx) {
    const { auth } = ctx;
    const { modelUrl, file } = ctx.propsValue;

    const input = fileToInput(file);

    const outputs = await callClarifaiModel({
      auth: auth.secret_text,
      modelUrl,
      input,
    });
    return cleanMultiOutputResponse(outputs);
  },
});

export const imageToTextModelPredictAction = createAction({
  auth: clarifaiAuth,
  name: 'image_text_model',
  description: 'Call an image to text AI model',
  audience: 'both',
  aiMetadata: {
    description:
      'Runs an image through a Clarifai image-to-text model and returns the generated text (e.g. a caption or description). Use to describe or extract text from an image; supply the model URL and the file as a URL or base64 bytes. Read-only inference, safe to repeat.',
    idempotent: true,
  },
  displayName: 'Image to Text',
  props: {
    modelUrl: CommonClarifaiProps.modelUrl,
    file: Property.File({
      description: 'URL or base64 bytes of the image to classify',
      displayName: 'Input URL or bytes',
      required: true,
    }),
  },
  async run(ctx) {
    const { auth } = ctx;
    const { modelUrl, file } = ctx.propsValue;

    const input = fileToInput(file);

    const outputs = await callClarifaiModel({
      auth: auth.secret_text  ,
      modelUrl,
      input,
    });
    return cleanMultiOutputResponse(outputs);
  },
});
