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 uploadMedia = createAction({
  name: 'uploadMedia',
  displayName: 'Upload Media',
  description:
    'Generate a signed URL for file upload. Use the returned sign_url to upload your file with a PUT request.',
  audience: 'both',
  aiMetadata: {
    description:
      'Requests a signed upload URL from JoggAI for a given filename (must include a file extension and no special characters); the caller then PUTs the actual file bytes to the returned sign_url. Use as the first step to get media into JoggAI before referencing it in product or video actions. Not idempotent: each call mints a new signed upload target.',
    idempotent: false,
  },
  auth: joggAiAuth,
  props: {
    filename: Property.ShortText({
      displayName: 'Filename',
      description:
        'Name of the file to upload with extension (e.g., "image.jpg", "video.mp4")',
      required: true,
    }),
  },

  async run({ auth, propsValue }) {
    const { filename } = propsValue;

    await propsValidation.validateZod(propsValue, {
      filename: z.string().check(z.minLength(1, 'Filename cannot be empty'), z.regex(/^[^/\\:*?"<>|]+\.[a-zA-Z0-9]+$/, 'Filename must include a file extension and contain no special characters')),
    });

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

    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;
  },
});
