import {
  PiecePropValueSchema,
  Property,
  createAction,
} from '@activepieces/pieces-framework';
import { firstSeenAtFromField, firstSeenAtUntilField, lastSeenAtFromField, lastSeenAtUntilField, limitField, makeClient, pageField } from '../common';
import { PredictLeadsAuth } from '../auth';
import { prepareQuery } from '../common/client';

export const findConnectionsByDomainAction = createAction({
  auth: PredictLeadsAuth,
  name: 'predict-leads_find_connections_by_domain',
  displayName: 'Get Company Connections',
  description: 'Retrieves company connections by domain.',
  audience: 'both',
  aiMetadata: { description: "Lists a company's business connections (related/linked companies) in PredictLeads, keyed by its domain. Use to map a known company's relationship graph; requires the company domain. Read-only and idempotent; supports pagination.", idempotent: true },
  props: {
    domain: Property.ShortText({
      displayName: 'Domain',
      description: 'The domain to find.',
      required: true,
    }),
    // first_seen_at_from: firstSeenAtFromField,
    // first_seen_at_until: firstSeenAtUntilField,
    page: pageField,
    limit: limitField
  },
  async run(context) {
    const domain = context.propsValue.domain;
    const page = context.propsValue.page ?? 1;
    const limit = context.propsValue.limit ?? 1000;

    const client = makeClient(
      context.auth
    );

    try {
      const response = await client.findConnectionsByDomain(domain, prepareQuery({
        page,
        limit,
      }));
      return response;
    } catch (error) {
      throw new Error(JSON.stringify(error, undefined, 2));
    }
  },
});


export const findConnectionsAction = createAction({
  auth: PredictLeadsAuth,
  name: 'predict-leads_find_connections',
  displayName: 'List Connections',
  description: 'Finds all portfolio companies.',
  audience: 'both',
  aiMetadata: { description: 'Lists all portfolio-company connections available to the authenticated PredictLeads account, across companies (not scoped to a single domain). Use to retrieve the full connection set rather than one company\'s connections. Read-only and idempotent; supports pagination.', idempotent: true },
  props: {
    // first_seen_at_from: firstSeenAtFromField,
    // first_seen_at_until: firstSeenAtUntilField,
    // last_seen_at_from: lastSeenAtFromField,
    // last_seen_at_until: lastSeenAtUntilField,
    page: pageField,
    limit: limitField
  },
  async run(context) {
    const client = makeClient(
      context.auth
    );
    const page = context.propsValue.page ?? 1;
    const limit = context.propsValue.limit ?? 1000;

    try {
      const response = await client.findConnections(prepareQuery({
        page,
        limit,
      }));
      return response;
    } catch (error) {
      throw new Error(JSON.stringify(error, undefined, 2));
    }
  },
});