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 toggleSchedulingLinkAction = createAction({
  auth: savvyCalAuth,
  name: 'toggle_scheduling_link',
  displayName: 'Toggle Scheduling Link',
  description: 'Switches a scheduling link between active and disabled states.',
  audience: 'both',
  aiMetadata: { description: 'Flips a scheduling link between active and disabled, identified by its link id. Use to enable or pause a link; note it toggles relative to the link\'s current state, so repeating the call alternates the state rather than converging — not idempotent.', 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 toggle.',
      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}/toggle`,
    });
    return flattenLink(response.body);
  },
});
