#!/usr/bin/env python3
"""YouTube Data API v3 — search and video info."""

import os
import sys
import json
import urllib.request
import urllib.parse

API_KEY = os.environ.get("YOUTUBE_API_KEY") or ""
BASE = "https://www.googleapis.com/youtube/v3"


def search(query: str, max_results: int = 5, lang: str = "ru") -> list[dict]:
    params = urllib.parse.urlencode({
        "part": "snippet",
        "q": query,
        "maxResults": max_results,
        "key": API_KEY,
        "relevanceLanguage": lang,
        "type": "video",
    })
    url = f"{BASE}/search?{params}"
    with urllib.request.urlopen(url) as r:
        data = json.loads(r.read())
    results = []
    for item in data.get("items", []):
        s = item["snippet"]
        vid_id = item["id"]["videoId"]
        results.append({
            "title": s["title"],
            "channel": s["channelTitle"],
            "published": s["publishedAt"][:10],
            "description": s["description"][:200],
            "url": f"https://www.youtube.com/watch?v={vid_id}",
            "video_id": vid_id,
        })
    return results


def video_info(video_id: str) -> dict:
    params = urllib.parse.urlencode({
        "part": "snippet,statistics,contentDetails",
        "id": video_id,
        "key": API_KEY,
    })
    url = f"{BASE}/videos?{params}"
    with urllib.request.urlopen(url) as r:
        data = json.loads(r.read())
    items = data.get("items", [])
    if not items:
        return {}
    item = items[0]
    s = item["snippet"]
    stats = item.get("statistics", {})
    return {
        "title": s["title"],
        "channel": s["channelTitle"],
        "published": s["publishedAt"][:10],
        "description": s["description"],
        "views": stats.get("viewCount", "?"),
        "likes": stats.get("likeCount", "?"),
        "duration": item["contentDetails"]["duration"],
        "url": f"https://www.youtube.com/watch?v={video_id}",
    }


if __name__ == "__main__":
    cmd = sys.argv[1] if len(sys.argv) > 1 else "search"
    if cmd == "search":
        query = sys.argv[2] if len(sys.argv) > 2 else "OpenClaw AI"
        results = search(query)
        print(json.dumps(results, ensure_ascii=False, indent=2))
    elif cmd == "info":
        vid_id = sys.argv[2]
        info = video_info(vid_id)
        print(json.dumps(info, ensure_ascii=False, indent=2))
