import { z } from "zod";
import { tool } from "@anthropic-ai/claude-agent-sdk";
import { GoogleDriveClient } from "../drive/google-drive.js";
import { getAgentState } from "../agent/state.js";
export const manageDriveTool = tool("manage_drive", `Manage Google Drive files and folders. Use this to list, upload, download, share, move, and delete files in Google Drive.

Available actions:
- list_files: List files in Drive with optional search query and folder filter
- get_file: Get metadata for a specific file by ID
- upload_file: Upload a local file to Google Drive
- download_file: Download a file from Google Drive to a local path
- create_folder: Create a new folder in Google Drive
- share_file: Share a file or folder with another user
- move_file: Move a file to a different folder
- delete_file: Move a file to trash`, {
    action: z
        .enum([
        "list_files",
        "get_file",
        "upload_file",
        "download_file",
        "create_folder",
        "share_file",
        "move_file",
        "delete_file",
    ])
        .describe("Drive action to perform"),
    file_id: z
        .string()
        .optional()
        .describe("Google Drive file or folder ID (for get_file, download_file, share_file, move_file, delete_file)"),
    name: z
        .string()
        .optional()
        .describe("File or folder name (for create_folder; optional override for upload_file)"),
    local_path: z
        .string()
        .optional()
        .describe("Absolute path to a local file (for upload_file as source, download_file as destination)"),
    mime_type: z
        .string()
        .optional()
        .describe("MIME type of the file being uploaded (optional, e.g. 'image/png', 'application/pdf')"),
    folder_id: z
        .string()
        .optional()
        .describe("Parent folder ID in Drive (for list_files to filter by folder, upload_file or create_folder to set parent)"),
    query: z
        .string()
        .optional()
        .describe("Google Drive search query (for list_files, e.g. \"name contains 'report'\" or \"mimeType = 'application/pdf'\")"),
    max_results: z
        .number()
        .int()
        .min(1)
        .max(100)
        .optional()
        .describe("Maximum number of files to return for list_files (default: 20)"),
    email: z
        .string()
        .optional()
        .describe("Email address to share the file with (for share_file)"),
    role: z
        .enum(["reader", "writer", "commenter"])
        .optional()
        .describe("Permission role to grant when sharing (for share_file): reader, writer, or commenter"),
    send_notification: z
        .boolean()
        .optional()
        .describe("Whether to send an email notification when sharing (for share_file, default: false)"),
    new_folder_id: z
        .string()
        .optional()
        .describe("ID of the destination folder (for move_file)"),
}, async (args) => {
    try {
        const state = getAgentState();
        const client = new GoogleDriveClient(state.config);
        switch (args.action) {
            case "list_files": {
                const files = await client.listFiles({
                    query: args.query,
                    maxResults: args.max_results,
                    folderId: args.folder_id,
                });
                if (files.length === 0) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "No files found matching the criteria.",
                            },
                        ],
                    };
                }
                const lines = files.map((f) => {
                    const size = f.size ? ` (${Math.round(Number(f.size) / 1024)} KB)` : "";
                    const modified = f.modifiedTime
                        ? ` — modified ${new Date(f.modifiedTime).toLocaleDateString()}`
                        : "";
                    const link = f.webViewLink ? `\n    Link: ${f.webViewLink}` : "";
                    return `- [${f.id}] ${f.name}${size}${modified}${link}`;
                });
                return {
                    content: [
                        {
                            type: "text",
                            text: `Found ${files.length} file(s):\n${lines.join("\n")}`,
                        },
                    ],
                };
            }
            case "get_file": {
                if (!args.file_id) {
                    return {
                        content: [
                            { type: "text", text: "Error: file_id is required for get_file" },
                        ],
                        isError: true,
                    };
                }
                const file = await client.getFile(args.file_id);
                const size = file.size
                    ? `\nSize: ${Math.round(Number(file.size) / 1024)} KB`
                    : "";
                const modified = file.modifiedTime
                    ? `\nModified: ${new Date(file.modifiedTime).toLocaleString()}`
                    : "";
                const link = file.webViewLink ? `\nLink: ${file.webViewLink}` : "";
                const parents = file.parents?.length
                    ? `\nParent folder(s): ${file.parents.join(", ")}`
                    : "";
                return {
                    content: [
                        {
                            type: "text",
                            text: `File: ${file.name}\nID: ${file.id}\nType: ${file.mimeType}${size}${modified}${link}${parents}`,
                        },
                    ],
                };
            }
            case "upload_file": {
                if (!args.local_path) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: local_path is required for upload_file",
                            },
                        ],
                        isError: true,
                    };
                }
                const result = await client.uploadFile({
                    localPath: args.local_path,
                    name: args.name,
                    mimeType: args.mime_type,
                    folderId: args.folder_id,
                });
                const link = result.webViewLink ? `\nView: ${result.webViewLink}` : "";
                const size = result.size
                    ? ` (${Math.round(Number(result.size) / 1024)} KB)`
                    : "";
                return {
                    content: [
                        {
                            type: "text",
                            text: `File uploaded: "${result.name}"${size}\nID: ${result.id}${link}`,
                        },
                    ],
                };
            }
            case "download_file": {
                if (!args.file_id) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: file_id is required for download_file",
                            },
                        ],
                        isError: true,
                    };
                }
                if (!args.local_path) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: local_path is required for download_file",
                            },
                        ],
                        isError: true,
                    };
                }
                await client.downloadFile({
                    fileId: args.file_id,
                    localPath: args.local_path,
                });
                return {
                    content: [
                        {
                            type: "text",
                            text: `File downloaded to: ${args.local_path}`,
                        },
                    ],
                };
            }
            case "create_folder": {
                if (!args.name) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: name is required for create_folder",
                            },
                        ],
                        isError: true,
                    };
                }
                const folder = await client.createFolder({
                    name: args.name,
                    parentId: args.folder_id,
                });
                const link = folder.webViewLink ? `\nView: ${folder.webViewLink}` : "";
                return {
                    content: [
                        {
                            type: "text",
                            text: `Folder created: "${folder.name}"\nID: ${folder.id}${link}`,
                        },
                    ],
                };
            }
            case "share_file": {
                if (!args.file_id) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: file_id is required for share_file",
                            },
                        ],
                        isError: true,
                    };
                }
                if (!args.email) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: email is required for share_file",
                            },
                        ],
                        isError: true,
                    };
                }
                if (!args.role) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: role is required for share_file (reader, writer, or commenter)",
                            },
                        ],
                        isError: true,
                    };
                }
                await client.shareFile({
                    fileId: args.file_id,
                    email: args.email,
                    role: args.role,
                    sendNotification: args.send_notification,
                });
                return {
                    content: [
                        {
                            type: "text",
                            text: `File ${args.file_id} shared with ${args.email} as ${args.role}.`,
                        },
                    ],
                };
            }
            case "move_file": {
                if (!args.file_id) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: file_id is required for move_file",
                            },
                        ],
                        isError: true,
                    };
                }
                if (!args.new_folder_id) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: new_folder_id is required for move_file",
                            },
                        ],
                        isError: true,
                    };
                }
                const moved = await client.moveFile({
                    fileId: args.file_id,
                    newFolderId: args.new_folder_id,
                });
                return {
                    content: [
                        {
                            type: "text",
                            text: `File "${moved.name}" moved to folder ${args.new_folder_id}.`,
                        },
                    ],
                };
            }
            case "delete_file": {
                if (!args.file_id) {
                    return {
                        content: [
                            {
                                type: "text",
                                text: "Error: file_id is required for delete_file",
                            },
                        ],
                        isError: true,
                    };
                }
                await client.deleteFile(args.file_id);
                return {
                    content: [
                        {
                            type: "text",
                            text: `File ${args.file_id} moved to trash.`,
                        },
                    ],
                };
            }
            default: {
                const action = args.action;
                return {
                    content: [{ type: "text", text: `Unknown action: ${action}` }],
                    isError: true,
                };
            }
        }
    }
    catch (err) {
        return {
            content: [
                {
                    type: "text",
                    text: `Drive operation failed: ${err instanceof Error ? err.message : String(err)}`,
                },
            ],
            isError: true,
        };
    }
});
//# sourceMappingURL=drive.js.map