"""
Google Tasks MCP Tools

This module provides MCP tools for interacting with Google Tasks API.
"""

import asyncio
import logging
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional

from googleapiclient.errors import HttpError  # type: ignore
from mcp import Resource

from auth.oauth_config import is_oauth21_enabled, is_external_oauth21_provider
from auth.permissions import is_action_denied
from auth.service_decorator import require_google_service
from core.server import server
from core.utils import UserInputError, handle_http_errors

logger = logging.getLogger(__name__)

LIST_TASKS_MAX_RESULTS_DEFAULT = 20
LIST_TASKS_MAX_RESULTS_MAX = 10_000
LIST_TASKS_MAX_POSITION = "99999999999999999999"


def _format_reauth_message(error: Exception, user_google_email: str) -> str:
    base = f"API error: {error}"

    # Only suggest re-authentication for auth-related errors (401, 403)
    if isinstance(error, HttpError) and error.resp.status in (401, 403):
        base += ". You might need to re-authenticate."
        if is_oauth21_enabled():
            if is_external_oauth21_provider():
                hint = (
                    "LLM: Ask the user to provide a valid OAuth 2.1 bearer token in the "
                    "Authorization header and retry."
                )
            else:
                hint = (
                    "LLM: Ask the user to authenticate via their MCP client's OAuth 2.1 "
                    "flow and retry."
                )
        else:
            hint = (
                "LLM: Try 'start_google_auth' with the user's email "
                f"({user_google_email}) and service_name='Google Tasks'."
            )
        return f"{base} {hint}"

    return base


class StructuredTask:
    def __init__(self, task: Dict[str, str], is_placeholder_parent: bool) -> None:
        self.id = task["id"]
        self.title = task.get("title", None)
        self.status = task.get("status", None)
        self.due = task.get("due", None)
        self.notes = task.get("notes", None)
        self.updated = task.get("updated", None)
        self.completed = task.get("completed", None)
        self.is_placeholder_parent = is_placeholder_parent
        self.subtasks: List["StructuredTask"] = []

    def add_subtask(self, subtask: "StructuredTask") -> None:
        self.subtasks.append(subtask)

    def __repr__(self) -> str:
        return f"StructuredTask(title={self.title}, {len(self.subtasks)} subtasks)"


def _adjust_due_max_for_tasks_api(due_max: str) -> str:
    """
    Compensate for the Google Tasks API treating dueMax as an exclusive bound.

    The API stores due dates at day resolution and compares using < dueMax, so to
    include tasks due on the requested date we bump the bound by one day.
    """
    try:
        parsed = datetime.fromisoformat(due_max.replace("Z", "+00:00"))
    except ValueError:
        logger.warning(
            "[list_tasks] Unable to parse due_max '%s'; sending unmodified value",
            due_max,
        )
        return due_max

    if parsed.tzinfo is None:
        parsed = parsed.replace(tzinfo=timezone.utc)

    adjusted = parsed + timedelta(days=1)
    if adjusted.tzinfo == timezone.utc:
        return adjusted.isoformat().replace("+00:00", "Z")
    return adjusted.isoformat()


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks_read")  # type: ignore
@handle_http_errors("list_task_lists", service_type="tasks")  # type: ignore
async def list_task_lists(
    service: Resource,
    user_google_email: str,
    max_results: int = 1000,
    page_token: Optional[str] = None,
) -> str:
    """
    List all task lists for the user.

    Args:
        user_google_email (str): The user's Google email address. Required.
        max_results (int): Maximum number of task lists to return (default: 1000, max: 1000).
        page_token (Optional[str]): Token for pagination.

    Returns:
        str: List of task lists with their IDs, titles, and details.
    """
    logger.info(f"[list_task_lists] Invoked. Email: '{user_google_email}'")

    try:
        params: Dict[str, Any] = {}
        if max_results is not None:
            params["maxResults"] = max_results
        if page_token:
            params["pageToken"] = page_token

        result = await asyncio.to_thread(service.tasklists().list(**params).execute)

        task_lists = result.get("items", [])
        next_page_token = result.get("nextPageToken")

        if not task_lists:
            return f"No task lists found for {user_google_email}."

        response = f"Task Lists for {user_google_email}:\n"
        for task_list in task_lists:
            response += f"- {task_list['title']} (ID: {task_list['id']})\n"
            response += f"  Updated: {task_list.get('updated', 'N/A')}\n"

        if next_page_token:
            response += f"\nNext page token: {next_page_token}"

        logger.info(f"Found {len(task_lists)} task lists for {user_google_email}")
        return response

    except HttpError as error:
        message = _format_reauth_message(error, user_google_email)
        logger.error(message, exc_info=True)
        raise Exception(message)
    except Exception as e:
        message = f"Unexpected error: {e}."
        logger.exception(message)
        raise Exception(message)


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks_read")  # type: ignore
@handle_http_errors("get_task_list", service_type="tasks")  # type: ignore
async def get_task_list(
    service: Resource, user_google_email: str, task_list_id: str
) -> str:
    """
    Get details of a specific task list.

    Args:
        user_google_email (str): The user's Google email address. Required.
        task_list_id (str): The ID of the task list to retrieve.

    Returns:
        str: Task list details including title, ID, and last updated time.
    """
    logger.info(
        f"[get_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}"
    )

    try:
        task_list = await asyncio.to_thread(
            service.tasklists().get(tasklist=task_list_id).execute
        )

        response = f"""Task List Details for {user_google_email}:
- Title: {task_list["title"]}
- ID: {task_list["id"]}
- Updated: {task_list.get("updated", "N/A")}
- Self Link: {task_list.get("selfLink", "N/A")}"""

        logger.info(
            f"Retrieved task list '{task_list['title']}' for {user_google_email}"
        )
        return response

    except HttpError as error:
        message = _format_reauth_message(error, user_google_email)
        logger.error(message, exc_info=True)
        raise Exception(message)
    except Exception as e:
        message = f"Unexpected error: {e}."
        logger.exception(message)
        raise Exception(message)


# --- Task list _impl functions ---


async def _create_task_list_impl(
    service: Resource, user_google_email: str, title: str
) -> str:
    """Implementation for creating a new task list."""
    logger.info(
        f"[create_task_list] Invoked. Email: '{user_google_email}', Title: '{title}'"
    )

    body = {"title": title}

    result = await asyncio.to_thread(service.tasklists().insert(body=body).execute)

    response = f"""Task List Created for {user_google_email}:
- Title: {result["title"]}
- ID: {result["id"]}
- Created: {result.get("updated", "N/A")}
- Self Link: {result.get("selfLink", "N/A")}"""

    logger.info(
        f"Created task list '{title}' with ID {result['id']} for {user_google_email}"
    )
    return response


async def _update_task_list_impl(
    service: Resource, user_google_email: str, task_list_id: str, title: str
) -> str:
    """Implementation for updating an existing task list."""
    logger.info(
        f"[update_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, New Title: '{title}'"
    )

    body = {"id": task_list_id, "title": title}

    result = await asyncio.to_thread(
        service.tasklists().update(tasklist=task_list_id, body=body).execute
    )

    response = f"""Task List Updated for {user_google_email}:
- Title: {result["title"]}
- ID: {result["id"]}
- Updated: {result.get("updated", "N/A")}"""

    logger.info(
        f"Updated task list {task_list_id} with new title '{title}' for {user_google_email}"
    )
    return response


async def _delete_task_list_impl(
    service: Resource, user_google_email: str, task_list_id: str
) -> str:
    """Implementation for deleting a task list."""
    logger.info(
        f"[delete_task_list] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}"
    )

    await asyncio.to_thread(service.tasklists().delete(tasklist=task_list_id).execute)

    response = f"Task list {task_list_id} has been deleted for {user_google_email}. All tasks in this list have also been deleted."

    logger.info(f"Deleted task list {task_list_id} for {user_google_email}")
    return response


async def _clear_completed_tasks_impl(
    service: Resource, user_google_email: str, task_list_id: str
) -> str:
    """Implementation for clearing completed tasks from a task list."""
    logger.info(
        f"[clear_completed_tasks] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}"
    )

    await asyncio.to_thread(service.tasks().clear(tasklist=task_list_id).execute)

    response = f"All completed tasks have been cleared from task list {task_list_id} for {user_google_email}. The tasks are now hidden and won't appear in default task list views."

    logger.info(
        f"Cleared completed tasks from list {task_list_id} for {user_google_email}"
    )
    return response


# --- Consolidated manage_task_list tool ---


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks")  # type: ignore
@handle_http_errors("manage_task_list", service_type="tasks")  # type: ignore
async def manage_task_list(
    service: Resource,
    user_google_email: str,
    action: str,
    task_list_id: Optional[str] = None,
    title: Optional[str] = None,
) -> str:
    """
    Manage task lists: create, update, delete, or clear completed tasks.

    Args:
        user_google_email (str): The user's Google email address. Required.
        action (str): The action to perform. Must be one of: "create", "update", "delete", "clear_completed".
        task_list_id (Optional[str]): The ID of the task list. Required for "update", "delete", and "clear_completed" actions.
        title (Optional[str]): The title for the task list. Required for "create" and "update" actions.

    Returns:
        str: Result of the requested action.
    """
    logger.info(
        f"[manage_task_list] Invoked. Email: '{user_google_email}', Action: '{action}'"
    )

    valid_actions = ("create", "update", "delete", "clear_completed")
    if action not in valid_actions:
        raise UserInputError(
            f"Invalid action '{action}'. Must be one of: {', '.join(valid_actions)}"
        )

    if is_action_denied("tasks", action):
        raise UserInputError(
            f"The '{action}' action is not allowed under the current permission level."
        )

    if action == "create":
        if not title:
            raise UserInputError("'title' is required for the 'create' action.")
        return await _create_task_list_impl(service, user_google_email, title)

    if action == "update":
        if not task_list_id:
            raise UserInputError("'task_list_id' is required for the 'update' action.")
        if not title:
            raise UserInputError("'title' is required for the 'update' action.")
        return await _update_task_list_impl(
            service, user_google_email, task_list_id, title
        )

    if action == "delete":
        if not task_list_id:
            raise UserInputError("'task_list_id' is required for the 'delete' action.")
        return await _delete_task_list_impl(service, user_google_email, task_list_id)

    # action == "clear_completed"
    if not task_list_id:
        raise UserInputError(
            "'task_list_id' is required for the 'clear_completed' action."
        )
    return await _clear_completed_tasks_impl(service, user_google_email, task_list_id)


# --- Task tools ---


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks_read")  # type: ignore
@handle_http_errors("list_tasks", service_type="tasks")  # type: ignore
async def list_tasks(
    service: Resource,
    user_google_email: str,
    task_list_id: str,
    max_results: int = LIST_TASKS_MAX_RESULTS_DEFAULT,
    page_token: Optional[str] = None,
    show_completed: bool = True,
    show_deleted: bool = False,
    show_hidden: bool = False,
    show_assigned: bool = False,
    completed_max: Optional[str] = None,
    completed_min: Optional[str] = None,
    due_max: Optional[str] = None,
    due_min: Optional[str] = None,
    updated_min: Optional[str] = None,
) -> str:
    """
    List all tasks in a specific task list.

    Args:
        user_google_email (str): The user's Google email address. Required.
        task_list_id (str): The ID of the task list to retrieve tasks from.
        max_results (int): Maximum number of tasks to return. (default: 20, max: 10000).
        page_token (Optional[str]): Token for pagination.
        show_completed (bool): Whether to include completed tasks (default: True). Note that show_hidden must also be true to show tasks completed in first party clients, such as the web UI and Google's mobile apps.
        show_deleted (bool): Whether to include deleted tasks (default: False).
        show_hidden (bool): Whether to include hidden tasks (default: False).
        show_assigned (bool): Whether to include assigned tasks (default: False).
        completed_max (Optional[str]): Upper bound for completion date (RFC 3339 timestamp).
        completed_min (Optional[str]): Lower bound for completion date (RFC 3339 timestamp).
        due_max (Optional[str]): Upper bound for due date (RFC 3339 timestamp).
        due_min (Optional[str]): Lower bound for due date (RFC 3339 timestamp).
        updated_min (Optional[str]): Lower bound for last modification time (RFC 3339 timestamp).

    Returns:
        str: List of tasks with their details.
    """
    logger.info(
        f"[list_tasks] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}"
    )

    try:
        params: Dict[str, Any] = {"tasklist": task_list_id}
        if max_results is not None:
            params["maxResults"] = max_results
        if page_token:
            params["pageToken"] = page_token
        if show_completed is not None:
            params["showCompleted"] = show_completed
        if show_deleted is not None:
            params["showDeleted"] = show_deleted
        if show_hidden is not None:
            params["showHidden"] = show_hidden
        if show_assigned is not None:
            params["showAssigned"] = show_assigned
        if completed_max:
            params["completedMax"] = completed_max
        if completed_min:
            params["completedMin"] = completed_min
        if due_max:
            adjusted_due_max = _adjust_due_max_for_tasks_api(due_max)
            if adjusted_due_max != due_max:
                logger.info(
                    "[list_tasks] Adjusted due_max from '%s' to '%s' to include due date boundary",
                    due_max,
                    adjusted_due_max,
                )
            params["dueMax"] = adjusted_due_max
        if due_min:
            params["dueMin"] = due_min
        if updated_min:
            params["updatedMin"] = updated_min

        result = await asyncio.to_thread(service.tasks().list(**params).execute)

        tasks = result.get("items", [])
        next_page_token = result.get("nextPageToken")

        # In order to return a sorted and organized list of tasks all at once, we support retrieving more than a single
        # page from the Google tasks API.
        results_remaining = (
            min(max_results, LIST_TASKS_MAX_RESULTS_MAX)
            if max_results
            else LIST_TASKS_MAX_RESULTS_DEFAULT
        )
        results_remaining -= len(tasks)
        while results_remaining > 0 and next_page_token:
            params["pageToken"] = next_page_token
            params["maxResults"] = str(results_remaining)
            result = await asyncio.to_thread(service.tasks().list(**params).execute)
            more_tasks = result.get("items", [])
            next_page_token = result.get("nextPageToken")
            if len(more_tasks) == 0:
                # For some unexpected reason, no more tasks were returned. Break to avoid an infinite loop.
                break
            tasks.extend(more_tasks)
            results_remaining -= len(more_tasks)

        if not tasks:
            return (
                f"No tasks found in task list {task_list_id} for {user_google_email}."
            )

        structured_tasks = get_structured_tasks(tasks)

        response = f"Tasks in list {task_list_id} for {user_google_email}:\n"
        response += serialize_tasks(structured_tasks, 0)

        if next_page_token:
            response += f"Next page token: {next_page_token}\n"

        logger.info(
            f"Found {len(tasks)} tasks in list {task_list_id} for {user_google_email}"
        )
        return response

    except HttpError as error:
        message = _format_reauth_message(error, user_google_email)
        logger.error(message, exc_info=True)
        raise Exception(message)
    except Exception as e:
        message = f"Unexpected error: {e}."
        logger.exception(message)
        raise Exception(message)


def get_structured_tasks(tasks: List[Dict[str, str]]) -> List[StructuredTask]:
    """
    Convert a flat list of task dictionaries into StructuredTask objects based on parent-child relationships sorted by position.

    Args:
        tasks: List of task dictionaries.

    Returns:
        list: Sorted list of top-level StructuredTask objects with nested subtasks.
    """
    tasks_by_id = {
        task["id"]: StructuredTask(task, is_placeholder_parent=False) for task in tasks
    }
    positions_by_id = {
        task["id"]: int(task["position"]) for task in tasks if "position" in task
    }

    # Placeholder virtual root as parent for top-level tasks
    root_task = StructuredTask(
        {"id": "root", "title": "Root"}, is_placeholder_parent=False
    )

    for task in tasks:
        structured_task = tasks_by_id[task["id"]]
        parent_id = task.get("parent")
        parent = None

        if not parent_id:
            # Task without parent: parent to the virtual root
            parent = root_task
        elif parent_id in tasks_by_id:
            # Subtask: parent to its actual parent
            parent = tasks_by_id[parent_id]
        else:
            # Orphaned subtask: create placeholder parent
            # Due to paging or filtering, a subtask may have a parent that is not present in the list of tasks.
            # We will create placeholder StructuredTask objects for these missing parents to maintain the hierarchy.
            parent = StructuredTask({"id": parent_id}, is_placeholder_parent=True)
            tasks_by_id[parent_id] = parent
            root_task.add_subtask(parent)

        parent.add_subtask(structured_task)

    sort_structured_tasks(root_task, positions_by_id)
    return root_task.subtasks


def sort_structured_tasks(
    root_task: StructuredTask, positions_by_id: Dict[str, int]
) -> None:
    """
    Recursively sort--in place--StructuredTask objects and their subtasks based on position.

    Args:
        root_task: The root StructuredTask object.
        positions_by_id: Dictionary mapping task IDs to their positions.
    """

    def get_position(task: StructuredTask) -> int | float:
        # Tasks without position go to the end (infinity)
        result = positions_by_id.get(task.id, float("inf"))
        return result

    root_task.subtasks.sort(key=get_position)
    for subtask in root_task.subtasks:
        sort_structured_tasks(subtask, positions_by_id)


def serialize_tasks(structured_tasks: List[StructuredTask], subtask_level: int) -> str:
    """
    Serialize a list of StructuredTask objects into a formatted string with indentation for subtasks.
    Args:
        structured_tasks (list): List of StructuredTask objects.
        subtask_level (int): Current level of indentation for subtasks.

    Returns:
        str: Formatted string representation of the tasks.
    """
    response = ""
    placeholder_parent_count = 0
    placeholder_parent_title = "Unknown parent"
    for task in structured_tasks:
        indent = "  " * subtask_level
        bullet = "-" if subtask_level == 0 else "*"
        if task.title is not None:
            title = task.title
        elif task.is_placeholder_parent:
            title = placeholder_parent_title
            placeholder_parent_count += 1
        else:
            title = "Untitled"
        response += f"{indent}{bullet} {title} (ID: {task.id})\n"
        response += f"{indent}  Status: {task.status or 'N/A'}\n"
        response += f"{indent}  Due: {task.due}\n" if task.due else ""
        if task.notes:
            response += f"{indent}  Notes: {task.notes[:100]}{'...' if len(task.notes) > 100 else ''}\n"
        response += f"{indent}  Completed: {task.completed}\n" if task.completed else ""
        response += f"{indent}  Updated: {task.updated or 'N/A'}\n"
        response += "\n"

        response += serialize_tasks(task.subtasks, subtask_level + 1)

    if placeholder_parent_count > 0:
        # Placeholder parents should only appear at the top level
        assert subtask_level == 0
        response += f"""
{placeholder_parent_count} tasks with title {placeholder_parent_title} are included as placeholders.
These placeholders contain subtasks whose parents were not present in the task list.
This can occur due to pagination. Callers can often avoid this problem if max_results is large enough to contain all tasks (subtasks and their parents) without paging.
This can also occur due to filtering that excludes parent tasks while including their subtasks or due to deleted or hidden parent tasks.
"""

    return response


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks_read")  # type: ignore
@handle_http_errors("get_task", service_type="tasks")  # type: ignore
async def get_task(
    service: Resource, user_google_email: str, task_list_id: str, task_id: str
) -> str:
    """
    Get details of a specific task.

    Args:
        user_google_email (str): The user's Google email address. Required.
        task_list_id (str): The ID of the task list containing the task.
        task_id (str): The ID of the task to retrieve.

    Returns:
        str: Task details including title, notes, status, due date, etc.
    """
    logger.info(
        f"[get_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}"
    )

    try:
        task = await asyncio.to_thread(
            service.tasks().get(tasklist=task_list_id, task=task_id).execute
        )

        response = f"""Task Details for {user_google_email}:
- Title: {task.get("title", "Untitled")}
- ID: {task["id"]}
- Status: {task.get("status", "N/A")}
- Updated: {task.get("updated", "N/A")}"""

        if task.get("due"):
            response += f"\n- Due Date: {task['due']}"
        if task.get("completed"):
            response += f"\n- Completed: {task['completed']}"
        if task.get("notes"):
            response += f"\n- Notes: {task['notes']}"
        if task.get("parent"):
            response += f"\n- Parent Task ID: {task['parent']}"
        if task.get("position"):
            response += f"\n- Position: {task['position']}"
        if task.get("selfLink"):
            response += f"\n- Self Link: {task['selfLink']}"
        if task.get("webViewLink"):
            response += f"\n- Web View Link: {task['webViewLink']}"

        logger.info(
            f"Retrieved task '{task.get('title', 'Untitled')}' for {user_google_email}"
        )
        return response

    except HttpError as error:
        message = _format_reauth_message(error, user_google_email)
        logger.error(message, exc_info=True)
        raise Exception(message)
    except Exception as e:
        message = f"Unexpected error: {e}."
        logger.exception(message)
        raise Exception(message)


# --- Task _impl functions ---


async def _create_task_impl(
    service: Resource,
    user_google_email: str,
    task_list_id: str,
    title: str,
    notes: Optional[str] = None,
    due: Optional[str] = None,
    parent: Optional[str] = None,
    previous: Optional[str] = None,
) -> str:
    """Implementation for creating a new task in a task list."""
    logger.info(
        f"[create_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Title: '{title}'"
    )

    body = {"title": title}
    if notes:
        body["notes"] = notes
    if due:
        body["due"] = due

    params = {"tasklist": task_list_id, "body": body}
    if parent:
        params["parent"] = parent
    if previous:
        params["previous"] = previous

    result = await asyncio.to_thread(service.tasks().insert(**params).execute)

    response = f"""Task Created for {user_google_email}:
- Title: {result["title"]}
- ID: {result["id"]}
- Status: {result.get("status", "N/A")}
- Updated: {result.get("updated", "N/A")}"""

    if result.get("due"):
        response += f"\n- Due Date: {result['due']}"
    if result.get("notes"):
        response += f"\n- Notes: {result['notes']}"
    if result.get("webViewLink"):
        response += f"\n- Web View Link: {result['webViewLink']}"

    logger.info(
        f"Created task '{title}' with ID {result['id']} for {user_google_email}"
    )
    return response


async def _update_task_impl(
    service: Resource,
    user_google_email: str,
    task_list_id: str,
    task_id: str,
    title: Optional[str] = None,
    notes: Optional[str] = None,
    status: Optional[str] = None,
    due: Optional[str] = None,
) -> str:
    """Implementation for updating an existing task."""
    logger.info(
        f"[update_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}"
    )

    # First get the current task to build the update body
    current_task = await asyncio.to_thread(
        service.tasks().get(tasklist=task_list_id, task=task_id).execute
    )

    body = {
        "id": task_id,
        "title": title if title is not None else current_task.get("title", ""),
        "status": status
        if status is not None
        else current_task.get("status", "needsAction"),
    }

    if notes is not None:
        body["notes"] = notes
    elif current_task.get("notes"):
        body["notes"] = current_task["notes"]

    if due is not None:
        body["due"] = due
    elif current_task.get("due"):
        body["due"] = current_task["due"]

    result = await asyncio.to_thread(
        service.tasks().update(tasklist=task_list_id, task=task_id, body=body).execute
    )

    response = f"""Task Updated for {user_google_email}:
- Title: {result["title"]}
- ID: {result["id"]}
- Status: {result.get("status", "N/A")}
- Updated: {result.get("updated", "N/A")}"""

    if result.get("due"):
        response += f"\n- Due Date: {result['due']}"
    if result.get("notes"):
        response += f"\n- Notes: {result['notes']}"
    if result.get("completed"):
        response += f"\n- Completed: {result['completed']}"

    logger.info(f"Updated task {task_id} for {user_google_email}")
    return response


async def _delete_task_impl(
    service: Resource, user_google_email: str, task_list_id: str, task_id: str
) -> str:
    """Implementation for deleting a task from a task list."""
    logger.info(
        f"[delete_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}"
    )

    await asyncio.to_thread(
        service.tasks().delete(tasklist=task_list_id, task=task_id).execute
    )

    response = f"Task {task_id} has been deleted from task list {task_list_id} for {user_google_email}."

    logger.info(f"Deleted task {task_id} for {user_google_email}")
    return response


async def _move_task_impl(
    service: Resource,
    user_google_email: str,
    task_list_id: str,
    task_id: str,
    parent: Optional[str] = None,
    previous: Optional[str] = None,
    destination_task_list: Optional[str] = None,
) -> str:
    """Implementation for moving a task to a different position, parent, or list."""
    logger.info(
        f"[move_task] Invoked. Email: '{user_google_email}', Task List ID: {task_list_id}, Task ID: {task_id}"
    )

    params = {"tasklist": task_list_id, "task": task_id}
    if parent:
        params["parent"] = parent
    if previous:
        params["previous"] = previous
    if destination_task_list:
        params["destinationTasklist"] = destination_task_list

    result = await asyncio.to_thread(service.tasks().move(**params).execute)

    response = f"""Task Moved for {user_google_email}:
- Title: {result["title"]}
- ID: {result["id"]}
- Status: {result.get("status", "N/A")}
- Updated: {result.get("updated", "N/A")}"""

    if result.get("parent"):
        response += f"\n- Parent Task ID: {result['parent']}"
    if result.get("position"):
        response += f"\n- Position: {result['position']}"

    move_details = []
    if destination_task_list:
        move_details.append(f"moved to task list {destination_task_list}")
    if parent:
        move_details.append(f"made a subtask of {parent}")
    if previous:
        move_details.append(f"positioned after {previous}")

    if move_details:
        response += f"\n- Move Details: {', '.join(move_details)}"

    logger.info(f"Moved task {task_id} for {user_google_email}")
    return response


# --- Consolidated manage_task tool ---


@server.tool()  # type: ignore
@require_google_service("tasks", "tasks")  # type: ignore
@handle_http_errors("manage_task", service_type="tasks")  # type: ignore
async def manage_task(
    service: Resource,
    user_google_email: str,
    action: str,
    task_list_id: str,
    task_id: Optional[str] = None,
    title: Optional[str] = None,
    notes: Optional[str] = None,
    status: Optional[str] = None,
    due: Optional[str] = None,
    parent: Optional[str] = None,
    previous: Optional[str] = None,
    destination_task_list: Optional[str] = None,
) -> str:
    """
    Manage tasks: create, update, delete, or move tasks within task lists.

    Args:
        user_google_email (str): The user's Google email address. Required.
        action (str): The action to perform. Must be one of: "create", "update", "delete", "move".
        task_list_id (str): The ID of the task list. Required for all actions.
        task_id (Optional[str]): The ID of the task. Required for "update", "delete", and "move" actions.
        title (Optional[str]): The title of the task. Required for "create", optional for "update".
        notes (Optional[str]): Notes/description for the task. Used by "create" and "update" actions.
        status (Optional[str]): Task status ("needsAction" or "completed"). Used by "update" action.
        due (Optional[str]): Due date in RFC 3339 format (e.g., "2024-12-31T23:59:59Z"). Used by "create" and "update" actions.
        parent (Optional[str]): Parent task ID (for subtasks). Used by "create" and "move" actions.
        previous (Optional[str]): Previous sibling task ID (for positioning). Used by "create" and "move" actions.
        destination_task_list (Optional[str]): Destination task list ID (for moving between lists). Used by "move" action.

    Returns:
        str: Result of the requested action.
    """
    logger.info(
        f"[manage_task] Invoked. Email: '{user_google_email}', Action: '{action}', Task List ID: {task_list_id}"
    )

    allowed_statuses = {"needsAction", "completed"}
    if status is not None and status not in allowed_statuses:
        raise UserInputError("invalid status: must be 'needsAction' or 'completed'")

    valid_actions = ("create", "update", "delete", "move")
    if action not in valid_actions:
        raise UserInputError(
            f"Invalid action '{action}'. Must be one of: {', '.join(valid_actions)}"
        )

    if is_action_denied("tasks", action):
        raise UserInputError(
            f"The '{action}' action is not allowed under the current permission level."
        )

    if action == "create":
        if status is not None:
            raise UserInputError("'status' is only supported for the 'update' action.")
        if not title:
            raise UserInputError("'title' is required for the 'create' action.")
        return await _create_task_impl(
            service,
            user_google_email,
            task_list_id,
            title,
            notes=notes,
            due=due,
            parent=parent,
            previous=previous,
        )

    if action == "update":
        if status is not None and status not in allowed_statuses:
            raise UserInputError("invalid status: must be 'needsAction' or 'completed'")
        if not task_id:
            raise UserInputError("'task_id' is required for the 'update' action.")
        return await _update_task_impl(
            service,
            user_google_email,
            task_list_id,
            task_id,
            title=title,
            notes=notes,
            status=status,
            due=due,
        )

    if action == "delete":
        if not task_id:
            raise UserInputError("'task_id' is required for the 'delete' action.")
        return await _delete_task_impl(
            service, user_google_email, task_list_id, task_id
        )

    # action == "move"
    if not task_id:
        raise UserInputError("'task_id' is required for the 'move' action.")
    return await _move_task_impl(
        service,
        user_google_email,
        task_list_id,
        task_id,
        parent=parent,
        previous=previous,
        destination_task_list=destination_task_list,
    )
