from ._base import BaseClient


class WeatherClient(BaseClient):
    def __init__(self, token, default_days=None, **kwargs):
        super(WeatherClient, self).__init__(token, **kwargs)
        self._default_days = self._normalize_default_days(default_days)

    def current(self, location):
        params = self._build_params(location, options=None, supports_days=False, supports_lang=False)
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/current/", params=params, headers=headers)

    def forecast_hourly(self, location, options=None):
        params = self._build_params(
            location,
            options,
            default_days=self._default_days["forecast_hourly"],
            supports_days=True,
        )
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/forecast/by-hours/", params=params, headers=headers)

    def forecast_3hourly(self, location, options=None):
        params = self._build_params(
            location,
            options,
            default_days=self._default_days["forecast_3hourly"],
            supports_days=True,
        )
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/forecast/by-3hours/", params=params, headers=headers)

    def forecast_daily(self, location, options=None):
        params = self._build_params(
            location,
            options,
            default_days=self._default_days["forecast_daily"],
            supports_days=True,
        )
        params, headers = self._with_auth(params)
        return self._transport.get("/v2/forecast/by-days/", params=params, headers=headers)

    # Aliases for camelCase compatibility
    def forecastHourly(self, location, options=None):
        return self.forecast_hourly(location, options)

    def forecast3hourly(self, location, options=None):
        return self.forecast_3hourly(location, options)

    def forecastDaily(self, location, options=None):
        return self.forecast_daily(location, options)

    def _normalize_default_days(self, default_days):
        defaults = {
            "forecast_hourly": 1,
            "forecast_3hourly": 2,
            "forecast_daily": 3,
        }
        if default_days is None:
            return defaults
        if isinstance(default_days, dict):
            merged = defaults.copy()
            merged.update(default_days)
            return merged
        try:
            days_val = int(default_days)
        except (TypeError, ValueError):
            return defaults
        return {
            "forecast_hourly": days_val,
            "forecast_3hourly": days_val,
            "forecast_daily": days_val,
        }
