"""Vehicle status: telemetry, location, remote/lock status, health."""

from __future__ import annotations

from typing import Any

from tyta.core.client import ToyotaClient

# ---------------------------------------------------------------------------
# Endpoint constants
# ---------------------------------------------------------------------------

VEHICLE_LOCATION_ENDPOINT = "/v1/location"
VEHICLE_HEALTH_STATUS_ENDPOINT = "/v1/vehiclehealth/status"
VEHICLE_GLOBAL_REMOTE_STATUS_ENDPOINT = "/v1/global/remote/status"
VEHICLE_TELEMETRY_ENDPOINT = "/v3/telemetry"


# ---------------------------------------------------------------------------
# Individual endpoint functions
# ---------------------------------------------------------------------------


async def get_telemetry(client: ToyotaClient, vin: str) -> dict[str, Any] | None:
    """Fetch raw telemetry data for a vehicle.

    Args:
        client: Authenticated ToyotaClient instance.
        vin: Vehicle Identification Number.

    Returns:
        The ``payload`` sub-dict from the API response, or ``None`` if absent.

    Raises:
        ToyotaApiError: If the API call returns a non-2xx response.

    """
    raw = await client.get(VEHICLE_TELEMETRY_ENDPOINT, vin=vin)
    return raw.get("payload")


async def get_location(client: ToyotaClient, vin: str) -> dict[str, Any] | None:
    """Fetch raw location data for a vehicle.

    Args:
        client: Authenticated ToyotaClient instance.
        vin: Vehicle Identification Number.

    Returns:
        The ``payload`` sub-dict from the API response, or ``None`` if absent.

    Raises:
        ToyotaApiError: If the API call returns a non-2xx response.

    """
    raw = await client.get(VEHICLE_LOCATION_ENDPOINT, vin=vin)
    return raw.get("payload")


async def get_remote_status(client: ToyotaClient, vin: str) -> dict[str, Any] | None:
    """Fetch raw remote/lock status for a vehicle.

    Args:
        client: Authenticated ToyotaClient instance.
        vin: Vehicle Identification Number.

    Returns:
        The ``payload`` sub-dict from the API response, or ``None`` if absent.

    Raises:
        ToyotaApiError: If the API call returns a non-2xx response.

    """
    raw = await client.get(VEHICLE_GLOBAL_REMOTE_STATUS_ENDPOINT, vin=vin)
    return raw.get("payload")


async def get_health(client: ToyotaClient, vin: str) -> dict[str, Any] | None:
    """Fetch raw vehicle health data for a vehicle.

    Args:
        client: Authenticated ToyotaClient instance.
        vin: Vehicle Identification Number.

    Returns:
        The ``payload`` sub-dict from the API response, or ``None`` if absent.

    Raises:
        ToyotaApiError: If the API call returns a non-2xx response.

    """
    raw = await client.get(VEHICLE_HEALTH_STATUS_ENDPOINT, vin=vin)
    return raw.get("payload")


# ---------------------------------------------------------------------------
# Aggregate: all status sections combined
# ---------------------------------------------------------------------------


async def get_status(client: ToyotaClient, vin: str) -> dict[str, Any]:
    """Fetch and merge all status data for a vehicle.

    Calls telemetry, location, remote status, and vehicle health endpoints
    concurrently and combines the results into a single dict.

    Args:
        client: Authenticated ToyotaClient instance.
        vin: Vehicle Identification Number.

    Returns:
        Dict containing keys: telemetry, location, remote_status, health.
        Each value is the ``payload`` sub-dict from the respective API
        response, or ``None`` if the endpoint returned no payload.

    Raises:
        ToyotaApiError: If any API call returns a non-2xx response.

    """
    import asyncio

    telemetry_task = asyncio.create_task(get_telemetry(client, vin))
    location_task = asyncio.create_task(get_location(client, vin))
    remote_status_task = asyncio.create_task(get_remote_status(client, vin))
    health_task = asyncio.create_task(get_health(client, vin))

    (
        telemetry_payload,
        location_payload,
        remote_status_payload,
        health_payload,
    ) = await asyncio.gather(
        telemetry_task,
        location_task,
        remote_status_task,
        health_task,
    )

    return {
        "telemetry": telemetry_payload,
        "location": location_payload,
        "remote_status": remote_status_payload,
        "health": health_payload,
    }
