import { createAction, Property } from '@activepieces/pieces-framework';
import { googleDriveAuth, createGoogleClient } from '../auth';
import { common } from '../common';
import { drive as googleDrive } from '@googleapis/drive';

export const googleDriveCreateNewFolder = createAction({
  auth: googleDriveAuth,
  name: 'create_new_gdrive_folder',
  description: 'Create a new empty folder in your Google Drive',
  audience: 'both',
  aiMetadata: { description: 'Creates a new empty folder in Google Drive, optionally nested under a parent folder. Use to set up a destination before uploading or moving files. Not idempotent: each call creates a distinct folder even with the same name (Drive permits duplicate folder names).', idempotent: false },
  displayName: 'Create new folder',
  props: {
    folderName: Property.ShortText({
      displayName: 'Folder name',
      description: 'The name of the new folder',
      required: true,
    }),
    parentFolder: common.properties.parentFolder,
    include_team_drives: common.properties.include_team_drives,
  },
  async run(context) {
    const authClient = await createGoogleClient(context.auth);

    const drive = googleDrive({ version: 'v3', auth: authClient });

    const response = await drive.files.create({
      requestBody: {
        mimeType: 'application/vnd.google-apps.folder',
        name: context.propsValue.folderName,
        ...(context.propsValue.parentFolder
          ? { parents: [context.propsValue.parentFolder] }
          : {}),
      },
      supportsAllDrives: context.propsValue.include_team_drives,
    });

    return response.data;
  },
});
