"""PV Power Forecasting with Akkudoktor.

This module provides classes and methods to retrieve, process, and display photovoltaic (PV) power forecast data. It includes features for working with environmental data such as temperature, wind speed, DC power, and AC power. Data retrieval is designed to work with Akkudoktor.net, and caching is implemented to reduce redundant network requests. Additionally, the module supports management of historical data for analysis over time.

Classes:
    AkkudoktorForecastHorizon: Represents details about the orientation of PV system horizons.
    AkkudoktorForecastMeta: Metadata configuration for the forecast, including location, system settings, and timezone.
    AkkudoktorForecastValue: Represents a single forecast data entry with information on temperature, wind speed, and solar orientation.
    AkkudoktorForecast: The main container for forecast data, holding both metadata and individual forecast entries.
    PVForecastAkkudoktorDataRecord: A specialized data record format for PV forecast data, including forecasted and actual AC power measurements.
    PVForecastAkkudoktorSettings: Contains configuration settings for constructing the Akkudoktor forecast API URL.
    PVForecastAkkudoktor: Primary class to manage PV power forecasts, handle data retrieval, caching, and integration with Akkudoktor.net.

Example:
    .. code-block:: python

        # Set up the configuration with necessary fields for URL generation
        settings_data = {
            "general": {
                "latitude": 52.52,
                "longitude": 13.405,
            },
            "prediction": {
                "hours": 48,
                "historic_hours": 24,
            },
            "pvforecast": {
                "provider": "PVForecastAkkudoktor",
                "planes": [
                    {
                        "peakpower": 5.0,
                        "surface_azimuth": 170,
                        "surface_tilt": 7,
                        "userhorizon": [20, 27, 22, 20],
                        "inverter_paco": 10000,
                    },
                    {
                        "peakpower": 4.8,
                        "surface_azimuth": 90,
                        "surface_tilt": 7,
                        "userhorizon": [30, 30, 30, 50],
                        "inverter_paco": 10000,
                    }
                ]
            }
        }

        # Create the config instance from the provided data
        config = PVForecastAkkudoktorSettings(**settings_data)

        # Initialize the forecast object with the generated configuration
        forecast = PVForecastAkkudoktor(settings=config)

        # Get an actual forecast
        await forecast.update_data()

        # Update the AC power measurement for a specific date and time
        await forecast.update_value(to_datetime(None, to_maxtime=False), "pvforecastakkudoktor_ac_power_measured", 1000.0)

        # Report the DC and AC power forecast along with AC measurements
        print(forecast.report_ac_power_and_measurement())

Attributes:
    hours (int): Number of hours into the future to forecast. Default is 48.
    historic_hours (int): Number of past hours to retain for analysis. Default is 24.
    latitude (float): Latitude for the forecast location.
    longitude (float): Longitude for the forecast location.
    start_datetime (datetime): Start time for the forecast, defaulting to current datetime.
    end_datetime (datetime): Computed end datetime based on `start_datetime` and `hours`.
    keep_datetime (datetime): Computed threshold datetime for retaining historical data.

Methods:
    provider_id(): Returns the unique identifier for the Akkudoktor provider.
    _request_forecast(): Retrieves forecast data from the Akkudoktor API.
    _update_data(): Updates forecast data within the PVForecastAkkudoktorDataRecord structure.
    report_ac_power_and_measurement(): Generates a report on AC and DC power forecasts and actual measurements.

"""

import random
from typing import Any, List, Optional, Union

import requests
from loguru import logger
from pydantic import Field, ValidationError, computed_field, field_validator

from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.pvforecast import PVForecastPlaneSetting
from akkudoktoreos.prediction.pvforecastabc import (
    PVForecastDataRecord,
    PVForecastProvider,
)
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime


class AkkudoktorForecastHorizon(PydanticBaseModel):
    altitude: int
    azimuthFrom: float
    azimuthTo: float


class AkkudoktorForecastMeta(PydanticBaseModel):
    lat: float
    lon: float
    power: List[int]
    azimuth: List[int]
    tilt: List[int]
    timezone: str
    albedo: float
    past_days: int
    inverterEfficiency: float
    powerInverter: List[int]
    cellCoEff: float
    range: bool
    horizont: List[List[AkkudoktorForecastHorizon]]
    horizontString: List[str]

    @field_validator("power", "azimuth", "tilt", "powerInverter", mode="before")
    @classmethod
    def ensure_list(cls, v: Any) -> List[int]:
        return v if isinstance(v, list) else [v]

    @field_validator("horizont", mode="before")
    @classmethod
    def normalize_horizont(cls, v: Any) -> List[List[AkkudoktorForecastHorizon]]:
        if isinstance(v, list):
            # Case: flat list of dicts
            if v and isinstance(v[0], dict):
                return [v]
            # Already in correct nested form
            if v and isinstance(v[0], list):
                return v
        return v

    @field_validator("horizontString", mode="before")
    @classmethod
    def parse_horizont_string(cls, v: Any) -> List[str]:
        if isinstance(v, str):
            return [s.strip() for s in v.split(",")]
        return v


class AkkudoktorForecastValue(PydanticBaseModel):
    datetime: str
    dcPower: float
    power: float
    sunTilt: Optional[float]
    sunAzimuth: Optional[float]
    temperature: Optional[float]
    relativehumidity_2m: Optional[float]
    windspeed_10m: Optional[float]


class AkkudoktorForecast(PydanticBaseModel):
    meta: AkkudoktorForecastMeta
    values: List[List[AkkudoktorForecastValue]]


class PVForecastAkkudoktorDataRecord(PVForecastDataRecord):
    """Represents a Akkudoktor specific pvforecast data record containing various pvforecast attributes at a specific datetime."""

    pvforecastakkudoktor_ac_power_measured: Optional[float] = Field(
        default=None, json_schema_extra={"description": "Total AC power measured (W)"}
    )
    pvforecastakkudoktor_wind_speed_10m: Optional[float] = Field(
        default=None, json_schema_extra={"description": "Wind Speed 10m (kmph)"}
    )
    pvforecastakkudoktor_temp_air: Optional[float] = Field(
        default=None, json_schema_extra={"description": "Temperature (°C)"}
    )

    # Computed fields
    @computed_field  # type: ignore[prop-decorator]
    @property
    def pvforecastakkudoktor_ac_power_any(self) -> Optional[float]:
        """Returns the AC power.

        If a measured value is available, it returns the measured AC power;
        otherwise, it returns the forecasted AC power.

        Returns:
            float: AC power in watts or None if no forecast data is available.
        """
        if self.pvforecastakkudoktor_ac_power_measured is not None:
            return self.pvforecastakkudoktor_ac_power_measured
        else:
            return self.pvforecast_ac_power


class PVForecastAkkudoktor(PVForecastProvider):
    """Fetch and process PV forecast data from akkudoktor.net.

    PVForecastAkkudoktor is a singleton-based class that retrieves weather forecast data
    from the PVForecastAkkudoktor API and maps it to `PVForecastDataRecord` fields, applying
    any necessary scaling or unit corrections. It manages the forecast over a range
    of hours into the future and retains historical data.
    """

    # overload
    records: List[PVForecastAkkudoktorDataRecord] = Field(
        default_factory=list,
        json_schema_extra={"description": "List of PVForecastAkkudoktorDataRecord records"},
    )

    @classmethod
    def provider_id(cls) -> str:
        """Return the unique identifier for the Akkudoktor provider."""
        return "PVForecastAkkudoktor"

    @classmethod
    def _validate_data(cls, json_str: Union[bytes, Any]) -> AkkudoktorForecast:
        """Validate Akkudoktor PV forecast data."""
        try:
            akkudoktor_data = AkkudoktorForecast.model_validate_json(json_str)
        except ValidationError as e:
            error_msg = ""
            for error in e.errors():
                field = " -> ".join(str(x) for x in error["loc"])
                message = error["msg"]
                error_type = error["type"]
                error_msg += f"Field: {field}\nError: {message}\nType: {error_type}\n"
            logger.error(f"Akkudoktor schema change: {error_msg}")
            raise ValueError(error_msg)
        return akkudoktor_data

    def _url(self, plane: PVForecastPlaneSetting) -> str:
        """Build akkudoktor.net API request URL.

        One request per plane.
        """
        base_url = "https://api.akkudoktor.net/forecast"
        query_params: dict[str, Any] = {
            # Randomize lat a little bit to circumvent caching by api.akkudoktor.
            # Caching used to provide very old data
            "lat": round(float(self.config.general.latitude) + random.uniform(0.000, 0.001), 6),  # noqa: S311
            "lon": self.config.general.longitude,
        }

        # power pv modules in watts ($float)
        if plane.peakpower is None:
            error_msg = "PVForecastAkkudoktor is missing plane peakpower configuration"
            logger.error(error_msg)
            raise ValueError(error_msg)
        query_params["power"] = float(plane.peakpower) * 1000

        # EOS orientation of of pv modules in azimuth in degree ($float):
        #   north=0, east=90, south=180, west=270
        # Akkudoktor orientation of pv modules in azimuth in degree:
        #   north=+-180, east=-90, south=0, west=90
        if plane.surface_azimuth is None:
            error_msg = "PVForecastAkkudoktor is missing plane surface_azimuth configuration"
            logger.error(error_msg)
            raise ValueError(error_msg)
        query_params["azimuth"] = float(plane.surface_azimuth) - 180

        # tilt of pv modules degree ($float)
        # 0 = flat / horizontal 30 = 30° tilt (on a roof) 90 = vertical (at a wall)
        if plane.surface_tilt is None:
            error_msg = "PVForecastAkkudoktor is missing plane surface_tilt configuration"
            logger.error(error_msg)
            raise ValueError(error_msg)
        query_params["tilt"] = float(plane.surface_tilt)

        # days with calculated data up to 92 Days (3 months)
        # query_params["past_days] = past_days

        # Change the used time cycles between "minutely_15" and "hourly" ($string).
        query_params["timecycle"] = "hourly"  # @TODO switch to 15 minutes if really supported

        # cellcoefficient of your pv modules. It is used for calculate losses with the
        # temperature of the modules. Value in percent ($float).
        query_params["cellCoEff"] = -0.36

        # albedo value ($float)
        query_params["albedo"] = 0.25

        # timezone $string
        query_params["timezone"] = self.config.general.timezone

        # max power of the inverter in watts. it will used, if the power of the inverter
        # is lower than the power generation of the modules. default: no limit. ($float)
        if plane.inverter_paco is None:
            error_msg = "PVForecastAkkudoktor is missing plane inverter_paco configuration"
            logger.error(error_msg)
            raise ValueError(error_msg)
        query_params["powerInverter"] = float(plane.inverter_paco)

        # returns max- and min generation from differnt weathermodels $int.
        # query_params["range"] = 0

        # horizon to calculate shading up to 360 values to describe shading situation for your pv.
        # For this, you use can a comma separated string with obsticals hight in angle
        # (-180 = north, -90 = east, 0 = south, 90 = east). Also transparency is optional supported
        # with a "t" after angle-
        # e.g.
        # 10,20,10,15 => -180..-90° azimuth + 10° tilt | -90°..0 azimuth + 20° tilt | ...
        # 10t0.3,15t0.8 => -180..0° azimuth + 10° tilt + 30% transparency |
        #                   0°..180 azimuth + 15° tilt + 80% transarency
        # 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10 (18 x "10")
        #               => every 20° azimuth (360/18) with 10° tilt
        if plane.userhorizon:
            query_params["horizont"] = ",".join(str(round(h)) for h in plane.userhorizon)

        # additional hourly data from open-meteo with "," splittet values.
        # Check https://open-meteo.com/en/docs/dwd-api
        query_params["hourly"] = "relativehumidity_2m,windspeed_10m"

        # Use requests to build the URL with proper encoding
        req = requests.Request("GET", base_url, params=query_params)
        prepared = req.prepare()
        url = prepared.url
        logger.debug(f"Akkudoktor URL: {url}")

        if url is None:
            error_msg = f"Cannot prepare URL for {req}"
            logger.error(error_msg)
            raise RuntimeError(error_msg)

        return url

    @cache_in_file(with_ttl="1 hour")
    def _request_forecast(self) -> AkkudoktorForecast:
        """Fetch PV forecast data from Akkudoktor API.

        This method sends a request to Akkudoktor API to retrieve forecast data
        for a specified date range and location. The response data is parsed and
        returned as JSON for further processing.

        Returns:
            dict: The parsed JSON response from Akkudoktor API containing forecast data.

        Raises:
            ValueError: If the API response does not include expected `meta` data.
        """
        akkudoktor_data: Optional[AkkudoktorForecast] = None

        # Hopefully skip internediate caches
        headers = {
            "Accept": "application/json",
            "Cache-Control": "no-cache",
            "Pragma": "no-cache",
        }

        for plane in self.config.pvforecast.planes:
            plane_url = self._url(plane)
            response = requests.get(plane_url, headers=headers, timeout=10)
            logger.info("[PVForecastAkkudoktor] URL: {}", response.request.url)
            logger.info("[PVForecastAkkudoktor] Headers: {}", response.request.headers)
            logger.info("[PVForecastAkkudoktor] Status: {}", response.status_code)
            logger.info("[PVForecastAkkudoktor] Response headers: {}", response.headers)

            response.raise_for_status()  # Raise an error for bad responses
            plane_data = self._validate_data(response.content)

            if akkudoktor_data is None:
                akkudoktor_data = plane_data
            else:
                # Add plane data to total akkudoktor data
                akkudoktor_data.values.append(plane_data.values[0])

        if akkudoktor_data is None:
            error_msg = "PVForecastAkkudoktor is missing plane configuration"
            logger.error(error_msg)
            raise ValueError(error_msg)

        # We are working on fresh data (no cache), report update time
        self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)

        return akkudoktor_data

    async def _update_data(self, force_update: Optional[bool] = False) -> None:
        """Update forecast data in the PVForecastAkkudoktorDataRecord format.

        Retrieves data from Akkudoktor. The processed data is inserted into the sequence as
        `PVForecastAkkudoktorDataRecord`.
        """
        # Assure we have something to request PV power for.
        if not self.config.pvforecast.planes:
            # No planes for PV
            error_msg = "Requested PV forecast, but no planes configured."
            logger.error(f"Configuration error: {error_msg}")
            raise ValueError(error_msg)

        # Get Akkudoktor PV Forecast data for the given configuration.
        if force_update:
            logger.info("[PVForecastAkkudoktor] force update.")
        akkudoktor_data = self._request_forecast(force_update=force_update)  # type: ignore

        # Timezone of the PV system
        if self.config.general.timezone != akkudoktor_data.meta.timezone:
            error_msg = f"Configured timezone '{self.config.general.timezone}' does not match Akkudoktor timezone '{akkudoktor_data.meta.timezone}'."
            logger.error(f"Akkudoktor schema change: {error_msg}")
            raise ValueError(error_msg)

        prediction_horizon = self.ems_start_datetime.start_of("day")
        prediction_horizon = prediction_horizon.add(hours=self.config.prediction.hours)

        # Assumption that all lists are the same length and are ordered chronologically
        # in ascending order and have the same timestamps.
        for plane_idx, plane_values in enumerate(akkudoktor_data.values):
            last_datetime = plane_values[-1].datetime
            dt = to_datetime(last_datetime, in_timezone=self.config.general.timezone)

            prediction_horizon = self.ems_start_datetime.start_of("day")
            prediction_horizon = prediction_horizon.add(hours=self.config.prediction.hours - 1)
            if compare_datetimes(dt, prediction_horizon).lt:
                error_msg = (
                    f"The forecast must cover at least the `{prediction_horizon}` prediction horizon, "
                    f"but only data up to `{dt}` is given in "
                    f"forecast data for plane `{plane_idx}`."
                )
                logger.error(f"Akkudoktor schema change: {error_msg}")
                raise ValueError(error_msg)

            if len(plane_values) < self.config.prediction.hours:
                # Expect one value set per prediction hour
                error_msg = (
                    f"The forecast must cover at least `{self.config.prediction.hours}` hours, "
                    f"but only `{len(plane_values)}` data sets are given in "
                    f"forecast data for plane `{plane_idx}`."
                )
                logger.error(f"Akkudoktor schema change: {error_msg}")
                raise ValueError(error_msg)

        if not self.ems_start_datetime:
            raise ValueError(f"Start DateTime not set: {self.ems_start_datetime}")

        # Iterate over forecast data points
        for forecast_values in zip(*akkudoktor_data.values):
            original_datetime = forecast_values[0].datetime
            dt = to_datetime(original_datetime, in_timezone=self.config.general.timezone)

            # Skip outdated forecast data
            if compare_datetimes(dt, self.ems_start_datetime.start_of("day")).lt:
                continue

            sum_dc_power = sum(values.dcPower for values in forecast_values)
            sum_ac_power = sum(values.power for values in forecast_values)

            data = {
                "pvforecast_dc_power": sum_dc_power,
                "pvforecast_ac_power": sum_ac_power,
                "pvforecastakkudoktor_wind_speed_10m": forecast_values[0].windspeed_10m,
                "pvforecastakkudoktor_temp_air": forecast_values[0].temperature,
            }

            await self.update_value(dt, data)

        if len(self) < self.config.prediction.hours:
            raise ValueError(
                f"The forecast must cover at least {self.config.prediction.hours} hours, "
                f"but only {len(self)} hours starting from {self.ems_start_datetime} "
                f"were predicted.\n"
                f"{akkudoktor_data}"
            )

    def report_ac_power_and_measurement(self) -> str:
        """Generate a report of DC power, forecasted AC power, measured AC power, and other AC power values.

        For each forecast entry, the following details are included:
            - Time of the forecast
            - DC power
            - Forecasted AC power
            - Measured AC power (if available)
            - Value returned by `get_ac_power` (if available)

        Returns:
            str: A formatted report containing details for each forecast entry.
        """

        def format_value(value: float | None) -> str:
            """Helper to format values as rounded strings or 'N/A' if None."""
            return f"{round(value, 2)}" if value is not None else "N/A"

        report_lines = []
        for record in self.records:
            date_time = record.date_time
            dc_power = format_value(record.pvforecast_dc_power)
            ac_power = format_value(record.pvforecast_ac_power)
            ac_power_measured = format_value(record.pvforecastakkudoktor_ac_power_measured)
            ac_power_any = format_value(record.pvforecastakkudoktor_ac_power_any)

            report_lines.append(
                f"Date&Time: {date_time}, DC: {dc_power}, AC: {ac_power}, "
                f"AC sampled: {ac_power_measured}, AC any: {ac_power_any}"
            )

        return "\n".join(report_lines)


# Example of how to use the PVForecastAkkudoktor class
async def main() -> None:
    """Main execution block to demonstrate the use of the PVForecastAkkudoktor class.

    Sets up the forecast configuration fields, fetches PV power forecast data,
    updates the AC power measurement for the current date/time, and prints
    the DC and AC power information.
    """
    # Set up the configuration with necessary fields for URL generation
    settings_data = {
        "general": {
            "latitude": 52.52,
            "longitude": 13.405,
        },
        "prediction": {
            "hours": 48,
            "historic_hours": 24,
        },
        "pvforecast": {
            "provider": "PVForecastAkkudoktor",
            "planes": [
                {
                    "peakpower": 5.0,
                    "surface_azimuth": 170,
                    "surface_tilt": 7,
                    "userhorizon": [20, 27, 22, 20],
                    "inverter_paco": 10000,
                },
                {
                    "peakpower": 4.8,
                    "surface_azimuth": 90,
                    "surface_tilt": 7,
                    "userhorizon": [30, 30, 30, 50],
                    "inverter_paco": 10000,
                },
                {
                    "peakpower": 1.4,
                    "surface_azimuth": 140,
                    "surface_tilt": 60,
                    "userhorizon": [60, 30, 0, 30],
                    "inverter_paco": 2000,
                },
                {
                    "peakpower": 1.6,
                    "surface_azimuth": 185,
                    "surface_tilt": 45,
                    "userhorizon": [45, 25, 30, 60],
                    "inverter_paco": 1400,
                },
            ],
        },
    }

    # Initialize the forecast object with the generated configuration
    forecast = PVForecastAkkudoktor()

    # Get an actual forecast
    await forecast.update_data()

    # Update the AC power measurement for a specific date and time
    await forecast.update_value(
        to_datetime(None, to_maxtime=False), "pvforecastakkudoktor_ac_power_measured", 1000.0
    )

    # Report the DC and AC power forecast along with AC measurements
    print(forecast.report_ac_power_and_measurement())


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
