import { googleSheetsAuth } from '../common/common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { sheets as googleSheets } from '@googleapis/sheets';
import { includeTeamDrivesProp, spreadsheetIdProp } from '../common/props';
import { createGoogleClient } from '../common/common';

export const findWorksheetAction = createAction({
	auth: googleSheetsAuth,
	name: 'find-worksheet',
	displayName: 'Find Worksheet(s)',
	description: 'Finds a worksheet(s) by title.',
	audience: 'both',
	aiMetadata: {
		description:
			'Searches the worksheets (tabs) of a given spreadsheet for ones whose title matches a query (exact or contains). Use to resolve a worksheet/sheet id from its tab name before acting on it. Read-only and idempotent.',
		idempotent: true,
	},
	props: {
		includeTeamDrives: includeTeamDrivesProp(),
		spreadsheetId: spreadsheetIdProp('Spreadsheet', ''),
		title: Property.ShortText({
			displayName: 'Title',
			required: true,
		}),
		exact_match: Property.Checkbox({
			displayName: 'Exact Match',
			description:
				'If true, only return worksheets that exactly match the name. If false, return worksheets that contain the name.',
			required: false,
			defaultValue: false,
		}),
	},
	async run(context) {
		const spreadsheetId = context.propsValue.spreadsheetId;
		const title = context.propsValue.title;
		const exactMatch = context.propsValue.exact_match ?? false;

		const authClient = await createGoogleClient(context.auth);

		const sheets = googleSheets({ version: 'v4', auth: authClient });

		const response = await sheets.spreadsheets.get({
			spreadsheetId,
		});

		const sheetsData = response.data.sheets ?? [];

		const matchedSheets = sheetsData.filter((sheet) => {
			const sheetTitle = sheet.properties?.title ?? '';
			return exactMatch ? sheetTitle === title : sheetTitle.includes(title);
		});

		return {
			found: matchedSheets.length > 0,
			worksheets: matchedSheets,
		};
	},
});
