"""Legacy SDK compatibility adapters.

These adapters preserve historical query controls (`block_results`,
`block_results_index`) on top of the modern submission-only `DalyClient`
implementation.
"""

from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from dalysdk.client import DalyClient


class LegacyEnergyModel:
    """Minimal legacy-compatible energy model wrapper.

    This class preserves the historical pattern where block query controls are
    passed via `run_query(..., block_results, block_results_index)` and the SDK
    injects `blockQuery`/`blockQueryIndex` in the request payload.

    The current API interprets those deprecated controls as block-results
    filtering on the full model definition, so this wrapper no longer trims the
    `blocks` list down to a single selected block before submission. The
    `run_async` compatibility input is accepted but ignored.
    """

    def __init__(self, daly_api_client: DalyClient, epm_input_json: dict | None = None, name: str = "New Energy Model"):
        self.daly_api_client = daly_api_client
        self.name = name
        self.epm_input_json = epm_input_json or {}

        self.epm_results: dict = {}
        self.epm_run_success = False
        self.epm_error_messages: dict = {}

        self.epm_query_input_json: dict = {}
        self.epm_query_results: dict = {}
        self.epm_query_run_success = False
        self.completed_epm_queries: list[dict] = []

    def run(
        self,
        *,
        run_async: bool = False,
        block_results: bool = False,
        block_results_index: int = -1,
    ) -> dict:
        """Submit an energy model with legacy tuple-style client behavior."""
        del run_async
        success, result = self.daly_api_client.run_energy_model(
            self.epm_input_json,
            block_results=block_results,
            block_results_index=block_results_index,
        )
        if success:
            self.epm_run_success = True
            self.epm_results = result
            return self.epm_results

        self.epm_run_success = False
        self.epm_results = {}
        self.epm_error_messages = result
        return self.epm_results

    def run_query(
        self,
        start_date,
        end_date,
        time_step_format: str = "local",
        block_results: bool = False,
        block_results_index: int = -1,
        run_async: bool = False,
        silence_print_stmts: bool = False,
    ) -> dict:
        """Legacy-compatible query runner.

        The date/time parameters are kept for backward signature compatibility.
        Query-shaping behavior mirrors the historical SDK for block controls.
        ``run_async`` is accepted for compatibility only and is ignored.
        """
        del start_date, end_date, time_step_format, run_async, silence_print_stmts

        payload = deepcopy(self.epm_input_json)
        self.epm_query_input_json = payload

        success, result = self.daly_api_client.run_energy_model(
            payload,
            block_results=block_results,
            block_results_index=block_results_index,
        )

        if success:
            self.epm_query_run_success = True
            self.epm_query_results = result
            self.completed_epm_queries.append({"resultsJSON": result})
            return result

        self.epm_query_run_success = False
        self.epm_query_results = result
        return result
