from ._base import BaseClient
from .exceptions import ValidationError


class GeographyClient(BaseClient):
    def countries(self):
        params, headers = self._with_auth({})
        return self._transport.get("/v2/geography/countries/", params=params, headers=headers)

    def country_by_code(self, country_code):
        if not isinstance(country_code, str) or not country_code:
            raise ValidationError("country_code must be a non-empty string")
        params = {"country_code": country_code}
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/geography/countries/", params=params, headers=headers)

    def cities_by_country(self, country_code):
        if not isinstance(country_code, str) or not country_code:
            raise ValidationError("country_code must be a non-empty string")
        params = {"country_code": country_code}
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/geography/countries/cities/", params=params, headers=headers)

    def search(self, q, limit=None):
        if not isinstance(q, str) or not q:
            raise ValidationError("q must be a non-empty string")
        params = {"q": q}
        if limit is not None:
            try:
                limit_val = int(limit)
            except (TypeError, ValueError):
                raise ValidationError("limit must be an integer")
            if limit_val < 1:
                raise ValidationError("limit must be >= 1")
            params["limit"] = limit_val
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/geography/search/", params=params, headers=headers)
