import { PieceAuth } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';

const markdownDescription = `
Follow these instructions to get your Clarifai (Personal Access Token) PAT Key:
1. Go to the [security tab](https://clarifai.com/settings/security) in your Clarifai account and generate a new PAT token.
2. Copy the PAT token and paste it in the PAT Key field.
`;

export const clarifaiAuth = PieceAuth.SecretText({
  displayName: 'PAT Key',
  description: markdownDescription,
  required: true,
  validate: async ({ auth }) => {
    try {
      await httpClient.sendRequest({
        method: HttpMethod.GET,
        url: 'https://api.clarifai.com/v2/models',
        headers: {
          Authorization: 'Key ' + auth,
        },
      });
      return {
        valid: true,
      };
    } catch (e) {
      return {
        valid: false,
        error: `Invalid PAT token\nerror:\n${e}`,
      };
    }
  },
});
