"""Location resource for the Daly Energy SDK.

Locations represent physical sites with latitude/longitude coordinates.
The API auto-fetches elevation and timezone if not provided.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from dalysdk.client import DalyClient


class LocationResource:
    """Interact with the ``/locations`` endpoints.

    Usage::

        locations = client.locations.list()
        location = client.locations.get(1)
        new_loc = client.locations.create({"name": "Site A", "latitude": 33.4, "longitude": -112.0})
        client.locations.delete(1)
    """

    def __init__(self, client: DalyClient):
        self._client = client

    def list(self) -> list[dict]:
        """List all locations in the workspace.

        Returns:
            List of location dicts.
        """
        return self._client._request("GET", "/locations")

    def get(self, location_id: int) -> dict:
        """Get a single location by its workspace-scoped ID.

        Args:
            location_id: Workspace-scoped location index.

        Returns:
            Location dict.

        Raises:
            NotFoundError: If the location does not exist.
        """
        return self._client._request("GET", f"/locations/{location_id}")

    def create(self, data: dict) -> dict:
        """Create a new location.

        The API auto-fetches elevation and timezone from coordinates when
        those fields are not explicitly provided.

        Args:
            data: Location data. Must include ``latitude`` and ``longitude``.
                  Optional: ``name``, ``elevation``, ``timezone``, etc.

        Returns:
            Created location dict (includes assigned IDs).
        """
        return self._client._request("POST", "/locations", json=data)

    def delete(self, location_id: int) -> dict:
        """Delete a location.

        Args:
            location_id: Workspace-scoped location index.

        Returns:
            Confirmation dict (``{"deleted": True}``).

        Raises:
            NotFoundError: If the location does not exist.
        """
        return self._client._request("DELETE", f"/locations/{location_id}")
