import { gristAuth } from '../..';
import {
  createAction,
  DropdownOption,
  PiecePropValueSchema,
  Property,
} from '@activepieces/pieces-framework';
import { commonProps } from '../common/props';
import { GristAPIClient } from '../common/helpers';

import { HttpMethod } from '@activepieces/pieces-common';

export const gristSearchRecordAction = createAction({
  auth: gristAuth,
  name: 'grist-search-record',
  displayName: 'Search Record',
  description: 'Search record by matching criteria.',
  audience: 'both',
  aiMetadata: { description: 'Looks up records in a Grist table by filtering one chosen column against a value. Use it to find rows before reading or updating them; the match is case-sensitive, exact, and only works on Text-type columns. Read-only and idempotent.', idempotent: true },
  props: {
    workspace_id: commonProps.workspace_id,
    document_id: commonProps.document_id,
    table_id: commonProps.table_id,
    column: Property.Dropdown({
      displayName: 'Column',
      auth: gristAuth,
      refreshers: ['document_id', 'table_id'],
      required: true,
      options: async ({ auth, document_id, table_id }) => {
        if (!auth || !document_id || !table_id) {
          return {
            disabled: true,
            placeholder: 'Please connect account and select document.',
            options: [],
          };
        }

        const authValue = auth.props;

        const client = new GristAPIClient({
          domainUrl: authValue.domain,
          apiKey: authValue.apiKey,
        });

        const response = await client.listTableColumns(
          document_id as string,
          table_id as string
        );

        const options: DropdownOption<string>[] = [];
        for (const column of response.columns) {
          options.push({ value: column.id, label: column.fields.label });
        }
        return {
          disabled: false,
          options,
        };
      },
    }),
    value: Property.ShortText({
      displayName: 'Value',
      required: true,
      description:
        'Enter the search text. The search operation is case sensitive, exact text match, and supports column-type Text only.',
    }),
  },
  async run(context) {
    const documentId = context.propsValue.document_id;
    const tableId = context.propsValue.table_id;
    const columnName = context.propsValue.column;
    const columnValue = context.propsValue.value;

    const client = new GristAPIClient({
      domainUrl: context.auth.props.domain,
      apiKey: context.auth.props.apiKey,
    });

    const encodedQuery = encodeURIComponent(
      JSON.stringify({
        [columnName]: [columnValue],
      })
    );

    return await client.makeRequest(
      HttpMethod.GET,
      `/docs/${documentId}/tables/${tableId}/records?filter=${encodedQuery}`
    );
  },
});
