import { segmentAuth } from '../../.';
import { Property, createAction } from '@activepieces/pieces-framework';
import { Analytics } from '@segment/analytics-node'

export const identifyUser = createAction({
  name: 'identifyUser',
  displayName: 'Identify User',
  description: '',
  audience: 'both',
  aiMetadata: { description: 'Sends a Segment Identify call that associates a user (by required userId) with a set of traits, registering or updating that profile in Segment. Use when an agent needs to record who a user is and their attributes for downstream analytics/tooling. Not idempotent: each call emits a new Identify event.', idempotent: false },
  props: {
    userId: Property.ShortText({
      displayName: 'User ID',
      required: true,
    }),
    traits: Property.Object({
      displayName: 'Traits',
      description: 'The traits to associate with the user',
      required: true,
    }),
  },
  auth: segmentAuth,
  async run(context) {
    const analytics = new Analytics({ writeKey: context.auth.secret_text })
    analytics.identify({
      userId: context.propsValue.userId,
      traits: context.propsValue.traits,
    })
    return {
      success: true,

    }
  },
});
