#!/usr/bin/env python3
from __future__ import annotations

import argparse
import os
import sys

try:
    from source.kodi_addon.tmdb import lookup_tv_show
except Exception:
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "source", "kodi_addon"))
    from tmdb import lookup_tv_show  # type: ignore[import-not-found]


def main() -> int:
    parser = argparse.ArgumentParser(description="Manueller Test fuer tmdb.py (Plot + Poster).")
    parser.add_argument("title", nargs="?", default="Dark", help="Serientitel (Default: Dark)")
    parser.add_argument("--key", default=os.environ.get("TMDB_API_KEY", ""), help="TMDB API Key (oder env TMDB_API_KEY)")
    parser.add_argument("--lang", default=os.environ.get("TMDB_LANGUAGE", "de-DE"), help="Sprache, z.B. de-DE")
    parser.add_argument("--log-responses", action="store_true", help="Antwort-JSON (gekürzt) loggen")
    args = parser.parse_args()

    if not args.key:
        print(
            "Fehlt: --key oder env TMDB_API_KEY\n"
            "Beispiel: TMDB_API_KEY=DEIN_KEY ./scripts/test_tmdb.py\n"
            "Oder:     ./scripts/test_tmdb.py \"Dark\" --key DEIN_KEY",
            file=sys.stderr,
        )
        return 2

    meta = lookup_tv_show(
        title=args.title,
        api_key=args.key,
        language=args.lang,
        log=print,
        log_responses=args.log_responses,
    )
    if not meta:
        print("Kein Treffer / keine Meta-Daten.")
        return 1

    print("\nRESULT")
    print("plot:", (meta.plot or "")[:500])
    print("poster:", meta.poster)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
