"""Diagnostyka: jak filtrować sceny TPDB po performerze.

Próbuje 3 warianty:
  A) GET /scenes?performers[]=<uuid>  (aktualnie używany)
  B) GET /scenes?performer_id=<uuid>  (czasem REST tak chce)
  C) GET /performers/<slug-or-id>/scenes  (osobny endpoint)

Plus: pobiera /performers?q=<name> (top 5) — sprawdzić czy nie wybieramy złego performera.
"""
from __future__ import annotations

import sys

import httpx

from app.config import get_settings


def main(name: str) -> None:
    settings = get_settings()
    token = settings.tpdb_api_token
    base = settings.tpdb_base_url.rstrip("/")
    headers = {
        "Authorization": f"Bearer {token}",
        "Accept": "application/json",
        "User-Agent": "goon/0.1",
    }

    with httpx.Client(base_url=base, headers=headers, timeout=30.0) as c:
        # 1. lookup
        r = c.get("/performers", params={"q": name, "per_page": 10})
        r.raise_for_status()
        data = r.json().get("data") or []
        print(f"=== /performers?q={name!r}  → {len(data)} matches ===")
        for p in data:
            print(f"  id={p.get('id')}  name={p.get('name')!r}  slug={p.get('slug')!r}  posts={p.get('posts_count')}")
        if not data:
            return
        first = data[0]
        pid = first.get("id")
        slug = first.get("slug")

        # 2. variant A: performers[]
        print(f"\n=== A) /scenes?performers[]={pid} ===")
        r = c.get("/scenes", params={"performers[]": pid, "per_page": 5})
        print(f"  status={r.status_code}")
        if r.status_code == 200:
            j = r.json()
            print(f"  meta={j.get('meta')}  data_len={len(j.get('data') or [])}")

        # 3. variant B: performer_id
        print(f"\n=== B) /scenes?performer_id={pid} ===")
        r = c.get("/scenes", params={"performer_id": pid, "per_page": 5})
        print(f"  status={r.status_code}")
        if r.status_code == 200:
            j = r.json()
            print(f"  meta={j.get('meta')}  data_len={len(j.get('data') or [])}")

        # 4. variant C: /performers/<slug>/scenes
        print(f"\n=== C) /performers/{slug}/scenes ===")
        r = c.get(f"/performers/{slug}/scenes", params={"per_page": 5})
        print(f"  status={r.status_code}")
        if r.status_code == 200:
            j = r.json()
            print(f"  meta={j.get('meta')}  data_len={len(j.get('data') or [])}")
            for s in (j.get("data") or [])[:3]:
                print(f"    [{s.get('date')}] {s.get('title')!r}")

        # 5. variant D: /performers/<id>/scenes
        print(f"\n=== D) /performers/{pid}/scenes ===")
        r = c.get(f"/performers/{pid}/scenes", params={"per_page": 5})
        print(f"  status={r.status_code}")
        if r.status_code == 200:
            j = r.json()
            print(f"  meta={j.get('meta')}  data_len={len(j.get('data') or [])}")
            for s in (j.get("data") or [])[:3]:
                print(f"    [{s.get('date')}] {s.get('title')!r}")


if __name__ == "__main__":
    main(sys.argv[1] if len(sys.argv) > 1 else "Kitana Montana")
