import requests
from typing import Any, Dict

class ReconstructorClient:
    def __init__(self, base_url: str = "http://localhost:8081"):
        self.base_url = base_url.rstrip("/")
        self.timeout = 60

    def reconstruct(self, body: Dict[str, Any], raw_format: str = "h5") -> Dict[str, Any]:
        """
        POST /h5/reconstruct или /reconstruct
        body: {
          "file_raw_id": "string",
          "file_json_id": "string",
          "file_order_id": "string",
          "sequence_name": "string",
          "digit": "string",
          "phase_shift": bool
        }
        """
        endpoint = "/h5/reconstruct" if raw_format == "h5" else "/reconstruct"
        url = f"{self.base_url}{endpoint}"
        r = requests.post(url, json=body, timeout=self.timeout)
        r.raise_for_status()
        return r.json()

    def get_session(self, session_id: str) -> Dict[str, Any]:
        """
        GET /sessions/{session_id}
        """
        url = f"{self.base_url}/sessions/{session_id}"
        r = requests.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.json()

    def list_files(self, session_id: str) -> Dict[str, Any]:
        """
        GET /sessions/{session_id}/files
        """
        url = f"{self.base_url}/sessions/{session_id}/files"
        r = requests.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.json()

    def download_file(self, session_id: str, name: str) -> bytes:
        """
        GET /sessions/{session_id}/files/{name}
        """
        url = f"{self.base_url}/sessions/{session_id}/files/{name}"
        r = requests.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.content

    def download_archive(self, session_id: str) -> bytes:
        """
        GET /sessions/{session_id}/archive.zip
        """
        url = f"{self.base_url}/sessions/{session_id}/archive.zip"
        r = requests.get(url, timeout=self.timeout)
        r.raise_for_status()
        return r.content
