import {
  DynamicPropsValue,
  createAction,
} from '@activepieces/pieces-framework';
import { BikaCommon, createNewFields, makeClient } from '../common';
import { BikaAuth } from '../auth';

export const createRecordAction = createAction({
  auth: BikaAuth,
  name: 'bika_create_record',
  displayName: 'Create Record',
  description: 'Creates a new record in database.',
  audience: 'both',
  aiMetadata: { description: 'Creates a new record in a Bika.ai database table, populating its fields. Requires a space and database (table) to target; field values must match the database schema (read-only field types like formulas and autonumber are ignored). Not idempotent: each call appends a new record.', idempotent: false },
  props: {
    space_id: BikaCommon.space_id,
    database_id: BikaCommon.database_id,
    fields: BikaCommon.fields,
  },
  async run(context) {
    const auth = context.auth;
    const databaseId = context.propsValue.database_id;
    const spaceId = context.propsValue.space_id;
    const dynamicFields: DynamicPropsValue = context.propsValue.fields;
    const fields: {
      [n: string]: any;
    } = {};

    const props = Object.entries(dynamicFields);
    for (const [propertyKey, propertyValue] of props) {
      if (propertyValue !== undefined && propertyValue !== '') {
        fields[propertyKey] = propertyValue;
      }
    }

    const newFields: Record<string, unknown> = await createNewFields(
      auth,
      spaceId,
      databaseId,
      fields
    );

    const client = makeClient(
      context.auth.props,
    );
    const response: any = await client.createRecord(spaceId, databaseId , {
      records: [
        {
          fields: {
            ...newFields,
          },
        },
      ],
    });

    if (!response.success) {
      throw new Error(JSON.stringify(response, undefined, 2));
    }
    return response;
  },
});
