"""Jellyfin + Arr suite media endpoints."""

import json
import subprocess
from fastapi import APIRouter
import httpx

router = APIRouter(tags=["media"])

JELLYFIN_API_KEY = "REDACTED_API_KEY"  # pragma: allowlist secret
JELLYFIN_USER_ID = "308e0dab19ce4a2180a2933d73694514"
SONARR_URL = "http://192.168.0.200:8989"
SONARR_KEY = "REDACTED_SONARR_API_KEY"  # pragma: allowlist secret
RADARR_URL = "http://192.168.0.200:7878"
RADARR_KEY = "REDACTED_RADARR_API_KEY"  # pragma: allowlist secret
SABNZBD_URL = "http://192.168.0.200:8080"
SABNZBD_KEY = "6ae289de5a4f45f7a0124b43ba9c3dea"  # pragma: allowlist secret


def _jellyfin(path: str) -> dict:
    """Call Jellyfin API via SSH+kubectl to bypass Olares auth sidecar."""
    sep = "&" if "?" in path else "?"
    url = f"http://localhost:8096{path}{sep}api_key={JELLYFIN_API_KEY}"
    try:
        result = subprocess.run(
            ["ssh", "-o", "ConnectTimeout=3", "olares",
             f"kubectl exec -n jellyfin-vishinator deploy/jellyfin -c jellyfin -- curl -s '{url}'"],
            capture_output=True, text=True, timeout=15,
        )
        return json.loads(result.stdout) if result.returncode == 0 else {}
    except Exception:
        return {}


@router.get("/jellyfin/latest")
def jellyfin_latest():
    """Get recently added items from Jellyfin."""
    try:
        items = _jellyfin(f"/Users/{JELLYFIN_USER_ID}/Items/Latest?Limit=10&Fields=Overview,DateCreated")
        return [{"name": i.get("Name", "?"), "type": i.get("Type", "?"),
                 "series": i.get("SeriesName"), "date": i.get("DateCreated", "?")[:10],
                 "year": i.get("ProductionYear")} for i in (items if isinstance(items, list) else [])]
    except Exception as e:
        return {"error": str(e)}


@router.get("/sonarr/history")
def sonarr_history():
    """Recent Sonarr grabs/imports."""
    try:
        with httpx.Client(timeout=10) as client:
            r = client.get(f"{SONARR_URL}/api/v3/history",
                           headers={"X-Api-Key": SONARR_KEY},
                           params={"pageSize": 10, "sortKey": "date", "sortDirection": "descending"})
            r.raise_for_status()
            records = r.json().get("records", [])
            return [{"title": rec.get("sourceTitle", "?"), "event": rec.get("eventType", "?"),
                     "date": rec.get("date", "?")[:10],
                     "quality": rec.get("quality", {}).get("quality", {}).get("name", "?")}
                    for rec in records]
    except Exception as e:
        return {"error": str(e)}


@router.get("/radarr/history")
def radarr_history():
    """Recent Radarr grabs/imports."""
    try:
        with httpx.Client(timeout=10) as client:
            r = client.get(f"{RADARR_URL}/api/v3/history",
                           headers={"X-Api-Key": RADARR_KEY},
                           params={"pageSize": 10, "sortKey": "date", "sortDirection": "descending"})
            r.raise_for_status()
            records = r.json().get("records", [])
            return [{"title": rec.get("sourceTitle", "?"), "event": rec.get("eventType", "?"),
                     "date": rec.get("date", "?")[:10],
                     "quality": rec.get("quality", {}).get("quality", {}).get("name", "?")}
                    for rec in records]
    except Exception as e:
        return {"error": str(e)}


@router.get("/jellyfin/status")
def jellyfin_status():
    """Jellyfin server status: version, libraries, sessions."""
    info = _jellyfin("/System/Info")
    libraries = _jellyfin("/Library/VirtualFolders")
    sessions = _jellyfin("/Sessions")

    active = []
    idle_count = 0
    if isinstance(sessions, list):
        for s in sessions:
            if s.get("NowPlayingItem"):
                active.append({
                    "user": s.get("UserName", ""),
                    "client": s.get("Client", ""),
                    "device": s.get("DeviceName", ""),
                    "now_playing": s["NowPlayingItem"].get("Name", ""),
                    "type": s["NowPlayingItem"].get("Type", ""),
                })
            else:
                idle_count += 1

    return {
        "version": info.get("Version", "unknown"),
        "server_name": info.get("ServerName", "unknown"),
        "libraries": [{"name": lib.get("Name"), "type": lib.get("CollectionType", "")}
                      for lib in libraries] if isinstance(libraries, list) else [],
        "active_sessions": active,
        "idle_sessions": idle_count,
    }


@router.get("/sonarr/queue")
async def sonarr_queue():
    """Sonarr download queue."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            resp = await client.get(
                f"{SONARR_URL}/api/v3/queue",
                headers={"X-Api-Key": SONARR_KEY},
            )
            return resp.json()
    except Exception as e:
        return {"error": str(e)}


@router.get("/radarr/queue")
async def radarr_queue():
    """Radarr download queue."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            resp = await client.get(
                f"{RADARR_URL}/api/v3/queue",
                headers={"X-Api-Key": RADARR_KEY},
            )
            return resp.json()
    except Exception as e:
        return {"error": str(e)}


@router.get("/sabnzbd/queue")
async def sabnzbd_queue():
    """SABnzbd download queue."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            resp = await client.get(
                f"{SABNZBD_URL}/api",
                params={"apikey": SABNZBD_KEY, "output": "json", "mode": "queue"},
            )
            return resp.json()
    except Exception as e:
        return {"error": str(e)}


# ---------------------------------------------------------------------------
# Prowlarr (indexer manager)
# ---------------------------------------------------------------------------
PROWLARR_URL = "http://192.168.0.200:9696"
PROWLARR_KEY = "58b5963e008243cf8cc4fae5276e68af"  # pragma: allowlist secret


@router.get("/prowlarr/stats")
async def prowlarr_stats():
    """Prowlarr indexer status."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            r = await client.get(
                f"{PROWLARR_URL}/api/v1/indexer",
                headers={"X-Api-Key": PROWLARR_KEY},
            )
            r.raise_for_status()
            indexers = r.json()
            enabled = [i for i in indexers if i.get("enable")]
            return {
                "total": len(indexers),
                "enabled": len(enabled),
                "indexers": [
                    {"name": i["name"], "protocol": i.get("protocol", "?")}
                    for i in enabled[:10]
                ],
            }
    except Exception as e:
        return {"total": 0, "enabled": 0, "error": str(e)}


# ---------------------------------------------------------------------------
# Bazarr (subtitles)
# ---------------------------------------------------------------------------
BAZARR_URL = "http://192.168.0.200:6767"
BAZARR_KEY = "REDACTED_BAZARR_API_KEY"  # pragma: allowlist secret


@router.get("/bazarr/status")
async def bazarr_status():
    """Bazarr subtitle status."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            r = await client.get(
                f"{BAZARR_URL}/api/system/status",
                headers={"X-Api-Key": BAZARR_KEY},
            )
            r.raise_for_status()
            status = r.json().get("data", r.json())
            w = await client.get(
                f"{BAZARR_URL}/api/badges",
                headers={"X-Api-Key": BAZARR_KEY},
            )
            badges = w.json() if w.status_code == 200 else {}
            return {
                "version": status.get("bazarr_version", "?"),
                "sonarr_signalr": badges.get("sonarr_signalr", "?"),
                "radarr_signalr": badges.get("radarr_signalr", "?"),
                "wanted_episodes": badges.get("episodes", 0),
                "wanted_movies": badges.get("movies", 0),
            }
    except Exception as e:
        return {"error": str(e)}


# ---------------------------------------------------------------------------
# Audiobookshelf
# ---------------------------------------------------------------------------
ABS_URL = "http://192.168.0.200:13378"
ABS_TOKEN = "REDACTED_TOKEN"  # pragma: allowlist secret


@router.get("/audiobookshelf/stats")
async def audiobookshelf_stats():
    """Audiobookshelf library stats."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            libs = await client.get(
                f"{ABS_URL}/api/libraries",
                headers={"Authorization": f"Bearer {ABS_TOKEN}"},
            )
            libs.raise_for_status()
            libraries = libs.json().get("libraries", [])
            result = []
            for lib in libraries:
                result.append({
                    "name": lib.get("name", "?"),
                    "type": lib.get("mediaType", "?"),
                    "items": lib.get("stats", {}).get("totalItems", 0),
                })
            return {"libraries": result, "total": sum(l["items"] for l in result)}
    except Exception as e:
        return {"error": str(e)}


# ---------------------------------------------------------------------------
# Plex
# ---------------------------------------------------------------------------
PLEX_TOKEN = "REDACTED_TOKEN"  # pragma: allowlist secret
PLEX_SERVERS = {
    "Calypso": "http://192.168.0.250:32400",
    "Atlantis": "http://192.168.0.200:32400",
}


@router.get("/plex/status")
def plex_status():
    """Get Plex server status and active sessions."""
    import xml.etree.ElementTree as ET
    results = []
    for name, url in PLEX_SERVERS.items():
        try:
            with httpx.Client(timeout=5) as client:
                # Get sessions
                r = client.get(f"{url}/status/sessions", headers={"X-Plex-Token": PLEX_TOKEN})
                r.raise_for_status()
                root = ET.fromstring(r.text)
                sessions = []
                for v in root.iter("Video"):
                    session = {
                        "title": v.get("title", "?"),
                        "type": v.get("type", "?"),
                        "year": v.get("year"),
                    }
                    for p in v.iter("Player"):
                        session["player"] = p.get("product", "?")
                        session["device"] = p.get("device", "?")
                        session["state"] = p.get("state", "?")
                        session["local"] = p.get("local") == "1"
                    for s in v.iter("Session"):
                        session["bandwidth"] = s.get("bandwidth")
                        session["location"] = s.get("location")
                    for t in v.iter("REDACTED_APP_PASSWORD"):
                        session["transcode"] = True
                        session["video_decision"] = t.get("videoDecision")
                    sessions.append(session)

                # Get library counts
                lr = client.get(f"{url}/library/sections", headers={"X-Plex-Token": PLEX_TOKEN})
                libraries = []
                if lr.status_code == 200:
                    lroot = ET.fromstring(lr.text)
                    for d in lroot.iter("Directory"):
                        libraries.append({
                            "title": d.get("title", "?"),
                            "type": d.get("type", "?"),
                        })

                results.append({
                    "name": name,
                    "url": url,
                    "online": True,
                    "sessions": sessions,
                    "libraries": libraries,
                })
        except Exception as e:
            results.append({"name": name, "url": url, "online": False, "error": str(e), "sessions": [], "libraries": []})

    return {"servers": results}


# ---------------------------------------------------------------------------
# Deluge (torrent client)
# ---------------------------------------------------------------------------
DELUGE_URL = "http://192.168.0.200:8112"


@router.get("/deluge/status")
async def deluge_status():
    """Deluge torrent client status."""
    try:
        async with httpx.AsyncClient(timeout=10) as client:
            login = await client.post(
                f"{DELUGE_URL}/json",
                json={"method": "auth.login", "params": ["deluge"], "id": 1},
            )
            if login.status_code != 200:
                return {"available": False}
            stats = await client.post(
                f"{DELUGE_URL}/json",
                json={
                    "method": "web.update_ui",
                    "params": [
                        ["name", "state", "progress", "download_payload_rate",
                         "upload_payload_rate"],
                        {},
                    ],
                    "id": 2,
                },
            )
            data = stats.json().get("result", {})
            torrents = data.get("torrents", {})
            active = [
                t for t in torrents.values()
                if t.get("state") in ("Downloading", "Seeding")
            ]
            return {
                "available": True,
                "total": len(torrents),
                "active": len(active),
                "downloading": len(
                    [t for t in torrents.values() if t.get("state") == "Downloading"]
                ),
                "seeding": len(
                    [t for t in torrents.values() if t.get("state") == "Seeding"]
                ),
            }
    except Exception as e:
        return {"available": False, "error": str(e)}
