"""Google Drive MCP tools."""

from __future__ import annotations

from typing import Optional

from fastmcp import FastMCP

from ..services import drive as drive_svc

mcp = FastMCP("Drive")


@mcp.tool(tags={"drive", "read"})
def drive_list(
    profile: Optional[str] = None,
    folder_id: Optional[str] = None,
    query: Optional[str] = None,
    max_results: int = 30,
    page_token: Optional[str] = None,
) -> dict:
    """List or search files in Google Drive.

    Args:
        profile: Account profile name
        folder_id: Folder ID to list contents of (default: root)
        query: Drive search query (e.g. "name contains 'invoice'", "mimeType='application/pdf'")
        max_results: Max files to return (1-100, default 30)
        page_token: Pagination token from previous result
    """
    result = drive_svc.list_files(
        profile=profile, folder_id=folder_id, query=query,
        max_results=max_results, page_token=page_token,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "write"})
def drive_upload(
    local_path: str,
    profile: Optional[str] = None,
    folder_id: Optional[str] = None,
    name: Optional[str] = None,
    mime_type: Optional[str] = None,
) -> dict:
    """Upload a local file to Google Drive. MIME type is auto-detected from extension.

    Args:
        local_path: Path to the local file to upload
        profile: Account profile name
        folder_id: Destination folder ID (default: root)
        name: File name on Drive (default: local filename)
        mime_type: Override MIME type (auto-detected if omitted)
    """
    result = drive_svc.upload_file(
        local_path=local_path, profile=profile, folder_id=folder_id,
        name=name, mime_type=mime_type,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "write"})
def drive_upload_base64(
    base64_data: str,
    name: str,
    mime_type: str,
    profile: Optional[str] = None,
    folder_id: Optional[str] = None,
) -> dict:
    """Upload file to Google Drive from base64-encoded data. No local file needed.

    Args:
        base64_data: Base64-encoded file content
        name: File name on Drive
        mime_type: MIME type of the file
        profile: Account profile name
        folder_id: Destination folder ID
    """
    result = drive_svc.upload_base64(
        base64_data=base64_data, name=name, mime_type=mime_type,
        profile=profile, folder_id=folder_id,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "read"})
def drive_download(file_id: str, save_path: str, profile: Optional[str] = None) -> dict:
    """Download a file from Google Drive to a local path.

    Args:
        file_id: Drive file ID to download
        save_path: Local path to save the file
        profile: Account profile name
    """
    result = drive_svc.download_file(file_id=file_id, save_path=save_path, profile=profile)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "write"})
def drive_create_folder(
    name: str,
    profile: Optional[str] = None,
    parent_id: Optional[str] = None,
) -> dict:
    """Create a new folder in Google Drive.

    Args:
        name: Folder name
        profile: Account profile name
        parent_id: Parent folder ID (default: root)
    """
    result = drive_svc.create_folder(name=name, profile=profile, parent_id=parent_id)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "write"})
def drive_move(file_id: str, new_parent_id: str, profile: Optional[str] = None) -> dict:
    """Move a file to a different folder in Google Drive.

    Args:
        file_id: File ID to move
        new_parent_id: Destination folder ID
        profile: Account profile name
    """
    result = drive_svc.move_file(file_id=file_id, new_parent_id=new_parent_id, profile=profile)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"drive", "write"})
def drive_trash(file_id: str, profile: Optional[str] = None) -> dict:
    """Move a file to trash in Google Drive.

    Args:
        file_id: File ID to trash
        profile: Account profile name
    """
    result = drive_svc.trash_file(file_id=file_id, profile=profile)
    result["profile"] = profile or "default"
    return result
