import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { plausibleAuth } from '../..';
import { plausibleApiCall, siteIdDropdown } from '../common';

export const createSharedLink = createAction({
  auth: plausibleAuth,
  name: 'create_shared_link',
  displayName: 'Create Shared Link',
  description: 'Find or create a shared link for a site',
  audience: 'both',
  aiMetadata: { description: 'Returns a public shared dashboard link for a site, creating one with the given name if it does not already exist (find-or-create). Use to obtain a shareable analytics URL for a specific site and link name. Idempotent, as repeating with the same site and name returns the existing link rather than duplicating it.', idempotent: true },
  props: {
    site_id: siteIdDropdown,
    name: Property.ShortText({
      displayName: 'Link Name',
      description: 'Name of the shared link (e.g., Wordpress)',
      required: true,
    }),
  },
  async run(context) {
    const response = await plausibleApiCall({
      apiKey: context.auth.secret_text,
      method: HttpMethod.PUT,
      endpoint: '/sites/shared-links',
      body: {
        site_id: context.propsValue['site_id'],
        name: context.propsValue['name'],
      },
    });
    return response;
  },
});
