import { createAction } from '@activepieces/pieces-framework';
import { NinoxAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import { HttpMethod } from '@activepieces/pieces-common';
import { teamidDropdown, databaseIdDropdown, tableIdDropdown, recordIdDropdown } from '../common/props';

export const listFilesFromRecord = createAction({
  auth: NinoxAuth,
  name: 'listFilesFromRecord',
  displayName: 'List Files from Record',
  description: 'List files attached to a specific record (without downloading) for management.',
  audience: 'both',
  aiMetadata: {
    description: 'Lists the file attachments on one Ninox record, addressed by team, database, table, and record id, returning their metadata without downloading content. Use to enumerate or check which files are attached before downloading. Idempotent — a read-only listing with no side effect.',
    idempotent: true,
  },
  props: {
    teamid: teamidDropdown,
    dbid: databaseIdDropdown,
    tid: tableIdDropdown,
    rid: recordIdDropdown,
  },
  async run({ auth, propsValue }) {
    const { teamid, dbid, tid, rid } = propsValue;

    const path = `/teams/${teamid}/databases/${dbid}/tables/${tid}/records/${rid}/files`;

    try {
      const response = await makeRequest<any>(
        auth.secret_text,
        HttpMethod.GET,
        path
      );

      // Process the file metadata
      const files = Array.isArray(response) ? response : [];

      return {
        success: true,
        message: `Found ${files.length} file(s) attached to the record`,
        files: files,
        totalFiles: files.length,
        recordId: rid,
      };
    } catch (error) {
      throw new Error(`Failed to list files from record: ${error}`);
    }
  },
});

