# -*- coding: utf-8 -*-
"""TMDB environment probe of the genre listing and CZ services (docs/plan/genre-browser/060, 070).

    python3 -I scripts/genre_harness/tmdb_probe.py            # every test
    python3 -I scripts/genre_harness/tmdb_probe.py votes rent # chosen tests

Live TMDB, no cache (the point is today's data); the key comes from the addon settings and is never printed.
Every test returns plain numbers and samples; the run is saved to docs/plan/genre-browser/data/tmdb-probe.json
(merged by test name, each with its date) so later agents can compare their numbers with these.
"""
import datetime
import json
import os
import re
import sys
import urllib.parse
import urllib.request

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', '..', 'docs', 'plan', 'genre-browser', 'data', 'tmdb-probe.json')
SETTINGS = os.path.expanduser('~/Library/Application Support/Kodi/userdata/addon_data/plugin.video.milionar/settings.xml')

TODAY = datetime.date.today().isoformat()
WESTERN = 'en|fr|de|es|it|pl|sv|da|no|nl|fi|pt|hu|is'
HOME = 'cs|sk'
NON_WESTERN = 'ja|ko|zh|cn|ru|hi|ta|te|th|tr|id|fa|ar|ml|bn|kn|tl|vi|uk|he|ms'
BIG = '8|1899|337|119|350|1773|2536|1928|381|1939'   # Netflix, HBO Max, Disney+, Prime, Apple TV, SkyShowtime,
                                                     # Oneplay, Prima Plus, Canal+, Lepší TV
RENT = '2|10|35'                                     # Apple TV Store, Amazon Video, Rakuten
ANIME_KEYWORD = 210024
BANDS = ((0, 0), (1, 9), (10, 49), (50, 10 ** 6))


def _key():
    return re.search(r'id="tmdb_api_key"[^>]*>([^<]+)<', open(SETTINGS, encoding='utf-8').read()).group(1)


KEY = _key()


def get(path, **query):
    query.setdefault('language', 'cs-CZ')
    query.update(api_key=KEY, include_adult='false')
    url = 'https://api.themoviedb.org/3/{}?{}'.format(path, urllib.parse.urlencode(query, safe='|'))
    with urllib.request.urlopen(url, timeout=20) as response:
        return json.load(response)


def discover(media='movie', **query):
    return get('discover/' + media, **query)


def votes(lo, hi):
    return {'vote_count.gte': lo, 'vote_count.lte': hi}


def titles(result, count=6):
    return ['{} ({}, {}, {})'.format(i.get('title') or i.get('name'), i.get('original_language'),
                                     (i.get('release_date') or i.get('first_air_date') or '')[:4], i.get('vote_count'))
            for i in result['results'][:count]]


def band_table(media, **query):
    """{band: {'total', 'sample'}} of one query over the vote bands, the most popular first."""
    out = {}
    for lo, hi in BANDS:
        result = discover(media, sort_by='popularity.desc', **query, **votes(lo, hi))
        out['{}-{}'.format(lo, hi if hi < 10 ** 6 else '')] = {'total': result['total_results'],
                                                              'sample': titles(result)}
    return out


def providers_cz(media, tmdb_id):
    result = get('{}/{}/watch/providers'.format(media, tmdb_id))['results'].get('CZ', {})
    return {kind: [p['provider_name'] for p in names] for kind, names in result.items() if kind != 'link'}


def test_listing():
    """Today's junk: the newest Sci-Fi / Action films without any filter."""
    out = {}
    for name, genre in (('scifi', 878), ('action', 28)):
        result = discover(with_genres=genre, sort_by='primary_release_date.desc', **{'primary_release_date.lte': TODAY})
        out[name] = {'newest': titles(result, 12),
                     'languages': _count(i['original_language'] for i in result['results'])}
    return out


def test_votes():
    """Does a known western film ever have under 10 votes? Western, Sci-Fi + Action 2021-2025, by vote band."""
    query = {'with_genres': '878|28', 'with_original_language': WESTERN,
             'primary_release_date.gte': '2021-01-01', 'primary_release_date.lte': '2025-12-31'}
    return {'western': band_table('movie', **query),
            'home_all_years': band_table('movie', with_original_language=HOME)}


def test_non_western():
    """Non-western live action (no anime) Sci-Fi + Action 2000-2025 by vote band."""
    return band_table('movie', with_genres='878|28', without_genres=16, without_keywords=ANIME_KEYWORD,
                      with_original_language=NON_WESTERN, **{'primary_release_date.gte': '2000-01-01',
                                                              'primary_release_date.lte': '2025-12-31'})


def test_services():
    """CZ services of TMDB (JustWatch): the list, coverage, the big services by vote band and language."""
    cz = {'watch_region': 'CZ'}
    big = dict(cz, with_watch_providers=BIG, with_watch_monetization_types='flatrate|ads')
    lists = {media: ['{}#{}'.format(p['provider_name'], p['provider_id'])
                     for p in sorted(get('watch/providers/' + media, **cz)['results'],
                                     key=lambda p: p.get('display_priorities', {}).get('CZ', 999))]
             for media in ('movie', 'tv')}
    known = {'all': discover(**{'vote_count.gte': 1000})['total_results'],
             'any_service': discover(**cz, with_watch_monetization_types='flatrate|ads|rent|buy|free',
                                     **{'vote_count.gte': 1000})['total_results'],
             'big': discover(**big, **{'vote_count.gte': 1000})['total_results']}
    return {'lists': lists, 'films_1000_votes': known,
            'big_western_home': band_table('movie', **big, with_original_language=WESTERN + '|' + HOME),
            'big_non_western': band_table('movie', **big, with_original_language=NON_WESTERN, without_genres=16,
                                          without_keywords=ANIME_KEYWORD),
            'big_scifi_newest': titles(discover(**big, with_genres=878, sort_by='primary_release_date.desc',
                                                **{'primary_release_date.lte': TODAY}), 12)}


def test_rent():
    """Rental stores (Apple TV Store, Amazon Video, Rakuten): junk with few votes, home titles all real."""
    rent = {'watch_region': 'CZ', 'with_watch_providers': RENT, 'with_watch_monetization_types': 'rent|buy'}
    return {'western': band_table('movie', **rent, with_original_language=WESTERN),
            'home': band_table('movie', **rent, with_original_language=HOME),
            'lepsi_tv': band_table('movie', watch_region='CZ', with_watch_providers='1939')}


def test_known():
    """Where famous titles are in CZ (a missing service never means unknown)."""
    titles_ = (('movie', 'The Shawshank Redemption', 1994), ('movie', 'The Matrix', 1999), ('movie', 'The Green Mile', 1999),
               ('movie', 'Intouchables', 2011), ('movie', 'Pelíšky', 1999), ('movie', 'Kulový blesk', 1979),
               ('movie', 'Dune: Part Two', 2024), ('movie', 'Parasite', 2019), ('tv', 'Okresní přebor', 2010),
               ('tv', 'Squid Game', 2021), ('tv', 'Návštěvníci', 1983))
    out = {}
    for media, title, year in titles_:
        query = {'query': title, ('year' if media == 'movie' else 'first_air_date_year'): year}
        found = get('search/' + media, **query)['results']
        out['{} {}'.format(title, year)] = providers_cz(media, found[0]['id']) or 'NIC' if found else '?'
    return out


def test_anime():
    """Anime and Asia counts (filter Původ)."""
    out = {'anime_keyword': discover(with_keywords=ANIME_KEYWORD)['total_results'],
           'anime_keyword_100_votes': discover(with_keywords=ANIME_KEYWORD, **{'vote_count.gte': 100})['total_results'],
           'animation_ja': discover(with_genres=16, with_original_language='ja')['total_results']}
    for lang in ('ja', 'ko', 'zh|cn', 'hi|ta|te|bn|ml|kn', 'th', 'tr', 'ru'):
        out['lang_{}_100_votes'.format(lang)] = discover(with_original_language=lang,
                                                         **{'vote_count.gte': 100})['total_results']
    return out


def test_texts():
    """Overview coverage cs vs en fallback; non-Latin titles in cs (shown as boxes in Kodi)."""
    cs = en = none = non_latin = 0
    for page in range(1, 6):
        query = {'with_genres': 878, 'with_original_language': WESTERN, 'vote_count.gte': 10,
                 'sort_by': 'primary_release_date.desc', 'primary_release_date.lte': TODAY, 'page': page}
        local = discover(**query)['results']
        english = {i['id']: i for i in discover(**dict(query, language='en-US'))['results']}
        for item in local:
            has_cs, has_en = bool(item.get('overview')), bool(english.get(item['id'], {}).get('overview'))
            cs += has_cs
            en += not has_cs and has_en
            none += not has_cs and not has_en
    for item in discover(with_genres=28, sort_by='primary_release_date.desc',
                         **{'primary_release_date.lte': TODAY})['results']:
        non_latin += any(ord(c) > 0x24F for c in item.get('title') or '')
    return {'overview_cs': cs, 'overview_en_only': en, 'overview_none': none, 'non_latin_titles_of_20': non_latin}


def test_logos():
    """Logos of the big services for the badge, and the sizes TMDB serves."""
    providers = {p['provider_id']: p for p in get('watch/providers/movie', watch_region='CZ')['results']}
    return {'sizes': get('configuration')['images']['logo_sizes'],
            'big': {pid: (providers.get(int(pid)) or {}).get('logo_path') for pid in BIG.split('|')}}


def _count(values):
    out = {}
    for value in values:
        out[value] = out.get(value, 0) + 1
    return dict(sorted(out.items(), key=lambda kv: -kv[1]))


TESTS = {name[5:]: fn for name, fn in globals().items() if name.startswith('test_')}


def main(names):
    try:
        saved = json.load(open(OUT, encoding='utf-8'))
    except (OSError, ValueError):
        saved = {}
    for name in names or TESTS:
        print('== {} — {}'.format(name, TESTS[name].__doc__.strip().splitlines()[0]))
        result = TESTS[name]()
        print(json.dumps(result, ensure_ascii=False, indent=1)[:3000])
        saved[name] = {'date': TODAY, 'result': result}
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    with open(OUT, 'w', encoding='utf-8') as handle:
        json.dump(saved, handle, ensure_ascii=False, indent=1, sort_keys=True)
    print('saved', os.path.normpath(OUT))


if __name__ == '__main__':
    main(sys.argv[1:])
