"""Google Calendar MCP tools."""

from __future__ import annotations

from typing import Optional

from fastmcp import FastMCP

from ..services import calendar as cal_svc

mcp = FastMCP("Calendar")


@mcp.tool(tags={"calendar", "read"})
def calendar_list_events(
    profile: Optional[str] = None,
    calendar_id: str = "primary",
    time_min: Optional[str] = None,
    time_max: Optional[str] = None,
    query: Optional[str] = None,
    max_results: int = 25,
    page_token: Optional[str] = None,
) -> dict:
    """List calendar events within a time range. Supports search and pagination.

    Args:
        profile: Account profile name
        calendar_id: Calendar ID (default: 'primary')
        time_min: Start of time range (ISO 8601, e.g. '2025-04-01T00:00:00+05:30')
        time_max: End of time range (ISO 8601)
        query: Free text search within events
        max_results: Max events to return (1-250, default 25)
        page_token: Pagination token
    """
    result = cal_svc.list_events(
        profile=profile, calendar_id=calendar_id, time_min=time_min,
        time_max=time_max, query=query, max_results=max_results, page_token=page_token,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"calendar", "write"})
def calendar_create_event(
    summary: str,
    start: str,
    end: str,
    profile: Optional[str] = None,
    calendar_id: str = "primary",
    description: Optional[str] = None,
    location: Optional[str] = None,
    attendees: Optional[list[str]] = None,
    send_updates: str = "none",
) -> dict:
    """Create a new calendar event with optional attendees and location.

    Args:
        summary: Event title
        start: Start time (ISO 8601, e.g. '2025-04-15T10:00:00+05:30')
        end: End time (ISO 8601)
        profile: Account profile name
        calendar_id: Calendar ID (default: 'primary')
        description: Event description
        location: Event location
        attendees: List of attendee email addresses
        send_updates: Send notifications: 'all', 'externalOnly', or 'none'
    """
    result = cal_svc.create_event(
        summary=summary, start=start, end=end, profile=profile,
        calendar_id=calendar_id, description=description, location=location,
        attendees=attendees, send_updates=send_updates,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"calendar", "write"})
def calendar_update_event(
    event_id: str,
    profile: Optional[str] = None,
    calendar_id: str = "primary",
    summary: Optional[str] = None,
    description: Optional[str] = None,
    location: Optional[str] = None,
    start: Optional[str] = None,
    end: Optional[str] = None,
) -> dict:
    """Update an existing calendar event.

    Args:
        event_id: Event ID to update
        profile: Account profile name
        calendar_id: Calendar ID (default: 'primary')
        summary: Updated title
        description: Updated description
        location: Updated location
        start: Updated start time (ISO 8601)
        end: Updated end time (ISO 8601)
    """
    result = cal_svc.update_event(
        event_id=event_id, profile=profile, calendar_id=calendar_id,
        summary=summary, description=description, location=location,
        start=start, end=end,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"calendar", "write"})
def calendar_delete_event(
    event_id: str,
    profile: Optional[str] = None,
    calendar_id: str = "primary",
) -> dict:
    """Delete a calendar event by ID.

    Args:
        event_id: Event ID to delete
        profile: Account profile name
        calendar_id: Calendar ID (default: 'primary')
    """
    result = cal_svc.delete_event(event_id=event_id, profile=profile, calendar_id=calendar_id)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"calendar", "read"})
def calendar_free_busy(
    time_min: str,
    time_max: str,
    profile: Optional[str] = None,
    emails: Optional[list[str]] = None,
) -> dict:
    """Check free/busy status for one or more calendars in a time range.

    Args:
        time_min: Start of range (ISO 8601)
        time_max: End of range (ISO 8601)
        profile: Account profile name
        emails: Email addresses to check (default: your own calendar)
    """
    result = cal_svc.free_busy(
        time_min=time_min, time_max=time_max, profile=profile, emails=emails,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"calendar", "read"})
def calendar_list_calendars(profile: Optional[str] = None) -> dict:
    """List all calendars accessible by this account.

    Args:
        profile: Account profile name
    """
    calendars = cal_svc.list_calendars(profile=profile)
    return {"profile": profile or "default", "calendars": calendars}
