import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import {
  savvyCalApiCall,
  buildTeamOptions,
  buildLinkOptions,
  flattenLink,
  SavvyCalSchedulingLink,
} from '../common';
import { savvyCalAuth, getToken } from '../auth';

export const duplicateSchedulingLinkAction = createAction({
  auth: savvyCalAuth,
  name: 'duplicate_scheduling_link',
  displayName: 'Duplicate Scheduling Link',
  description: 'Creates a copy of an existing scheduling link.',
  audience: 'both',
  aiMetadata: { description: 'Creates a new scheduling link as a copy of an existing one, identified by the source link id. Use to clone a link\'s configuration as a starting point. Not idempotent — each call produces another distinct copy.', idempotent: false },
  props: {
    team_id: Property.Dropdown({
      auth: savvyCalAuth,
      displayName: 'Team',
      description: 'Filter scheduling links by team. Leave empty to show all teams.',
      refreshers: [],
      required: false,
      options: async ({ auth }) => {
        if (!auth) return { disabled: true, options: [], placeholder: 'Please connect your account first' };
        try {
          const options = await buildTeamOptions(getToken(auth));
          return { disabled: false, options };
        } catch {
          return { disabled: true, options: [], placeholder: 'Failed to load teams.' };
        }
      },
    }),
    link_id: Property.Dropdown({
      auth: savvyCalAuth,
      displayName: 'Scheduling Link',
      description: 'Select the scheduling link to duplicate.',
      refreshers: ['team_id'],
      required: true,
      options: async ({ auth, team_id }) => {
        if (!auth) return { disabled: true, options: [], placeholder: 'Please connect your account first' };
        try {
          const options = await buildLinkOptions(getToken(auth), team_id as string | null);
          return { disabled: false, options };
        } catch {
          return { disabled: true, options: [], placeholder: 'Failed to load scheduling links.' };
        }
      },
    }),
  },
  async run(context) {
    const response = await savvyCalApiCall<SavvyCalSchedulingLink>({
      token: getToken(context.auth),
      method: HttpMethod.POST,
      path: `/links/${context.propsValue.link_id}/duplicate`,
    });
    return flattenLink(response.body);
  },
});
