import { createAction, Property } from '@activepieces/pieces-framework';
import {
	httpClient,
	HttpMethod,
	AuthenticationType,
	HttpRequest,
} from '@activepieces/pieces-common';
import { googleSheetsAuth } from '../common/common';
import { includeTeamDrivesProp } from '../common/props';
import { getAccessToken } from '../common/common';

export const findSpreadsheets = createAction({
	name: 'find_spreadsheets',
	displayName: 'Find Spreadsheet(s)',
	description: 'Find spreadsheet(s) by name.',
	audience: 'both',
	aiMetadata: {
		description:
			'Searches Google Drive for spreadsheets whose name matches a query (exact or contains) and returns the matches. Use to resolve a spreadsheet id from a human-readable name before acting on it. Read-only and idempotent.',
		idempotent: true,
	},
	auth: googleSheetsAuth,
	props: {
		includeTeamDrives: includeTeamDrivesProp(),
		spreadsheet_name: Property.ShortText({
			displayName: 'Spreadsheet Name',
			description: 'Enter the name of the spreadsheet to search for',
			required: true,
		}),
		exact_match: Property.Checkbox({
			displayName: 'Exact Match',
			description:
				'If true, only return spreadsheets that exactly match the name. If false, return spreadsheets that contain the name.',
			required: false,
			defaultValue: false,
		}),
	},
	async run({ propsValue, auth }) {
		const searchValue = propsValue.spreadsheet_name;
		const queries = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed=false'];

		if (propsValue.exact_match) {
			queries.push(`name = '${searchValue}'`);
		} else {
			queries.push(`name contains '${searchValue}'`);
		}

		const files = [];
		let pageToken = null;

		do {
			const request: HttpRequest = {
				method: HttpMethod.GET,
				url: 'https://www.googleapis.com/drive/v3/files',
				queryParams: {
					q: queries.join(' and '),
					includeItemsFromAllDrives: propsValue.includeTeamDrives ? 'true' : 'false',
					supportsAllDrives: 'true',
					corpora: propsValue.includeTeamDrives ? 'allDrives' : 'user',
					fields: 'files(id,name,webViewLink,createdTime,modifiedTime),nextPageToken',
				},
				authentication: {
					type: AuthenticationType.BEARER_TOKEN,
					token: await getAccessToken(auth),
				},
			};
			if (pageToken) {
				if (request.queryParams !== undefined) {
					request.queryParams['pageToken'] = pageToken;
				}
			}
			try {
				const response = await httpClient.sendRequest<{
					files: { id: string; name: string }[];
					nextPageToken: string;
				}>(request);

				files.push(...response.body.files);
				pageToken = response.body.nextPageToken;
			} catch (e) {
				throw new Error(`Failed to get folders\nError:${e}`);
			}
		} while (pageToken);

		return {
			found: files.length > 0,
			spreadsheets: files,
		};
	},
});
