"""
Nova (Amazon) model list fetching
"""

import os
import requests
from typing import Any, Dict, List

from .base import FetchBase

NOVA_API_URL = "https://api.nova.amazon.com/v1/models"


class FetchNova(FetchBase):
    """Nova (Amazon) model list fetcher"""

    def __init__(self, api_key: str = None):
        """
        Initialize Nova model fetcher.
        
        Args:
            api_key: Nova API key. If not provided, reads from NOVA_API_KEY env var.
        """
        self._api_key = api_key or os.getenv("NOVA_API_KEY", "")

    @property
    def provider(self) -> str:
        """Returns 'nova'"""
        return "nova"

    def fetch_models(self) -> List[str]:
        """
        Fetch model list in unified format.
        
        Returns:
            List of model ID strings extracted from the API response.
        """
        if not self._api_key:
            return self._handle_missing_env_var("NOVA_API_KEY")

        headers = {"Authorization": f"Bearer {self._api_key}"}

        try:
            response = self._http_get(NOVA_API_URL, headers=headers)
            response.raise_for_status()

            data = response.json()
            models = data.get("data", [])

            if not isinstance(models, list):
                return self._handle_unexpected_format(data)

            return [m.get("id") for m in models if m.get("id")]
        except requests.exceptions.RequestException as e:
            return self._handle_http_error(e)

    def fetch_models_raw(self) -> Dict[str, Any]:
        """
        Fetch raw API response.
        
        Returns:
            Complete JSON response from Nova API as dictionary.
        """
        if not self._api_key:
            self._log_warning(f"[{self.provider}] Missing environment variable: NOVA_API_KEY")
            return {}

        headers = {"Authorization": f"Bearer {self._api_key}"}

        try:
            response = self._http_get(NOVA_API_URL, headers=headers)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            self._log_error(
                f"[{self.provider}] HTTP request failed: {e}",
                exc_info=True
            )
            return {}
