#!/usr/bin/env python3
"""Generate fixtures/tmdb_fixture.json: TMDb-shaped data for the offline tests and the live fixture provider.

Frozen "today" is 2026-06-15. Shows: 900001 "Fixture Show" (returning; season 0 with one
special, season 1 with 10 episodes, season 2 with 8 episodes of which the last two are unaired),
900002 "Fixture Ended Show" (ended, 1 season with 3 episodes). Movies 900101 / 900102 are
parts of collection 900900.
"""
import json
import os
from datetime import date, timedelta

HERE = os.path.dirname(os.path.abspath(__file__))
TODAY = date(2026, 6, 15)


def episodes(season, count, first_air, unaired=0):
    items = []
    for n in range(1, count + 1):
        air = first_air + timedelta(days=7 * (n - 1))
        if n > count - unaired:
            air = TODAY + timedelta(days=7 * (n - (count - unaired)))
        items.append({'episode_number': n, 'season_number': season, 'name': 'S{:02d}E{:02d}'.format(season, n),
                      'air_date': air.isoformat(), 'runtime': 42, 'overview': 'Fixture episode', 'still_path': None})
    return items


def build():
    data = {'today': TODAY.isoformat(), 'tv': {}, 'season': {}, 'movie': {}, 'collection': {}}
    show = {'id': 900001, 'name': 'Fixture Show', 'original_name': 'Fixture Show', 'status': 'Returning Series',
            'first_air_date': '2025-09-01', 'overview': 'Fixture series', 'poster_path': None, 'backdrop_path': None,
            'seasons': [{'season_number': 0, 'episode_count': 1, 'air_date': '2025-08-20'},
                        {'season_number': 1, 'episode_count': 10, 'air_date': '2025-09-01'},
                        {'season_number': 2, 'episode_count': 8, 'air_date': '2026-04-20'}]}
    data['tv']['900001'] = show
    data['season']['900001:0'] = {'season_number': 0, 'episodes': episodes(0, 1, date(2025, 8, 20))}
    data['season']['900001:1'] = {'season_number': 1, 'episodes': episodes(1, 10, date(2025, 9, 1))}
    data['season']['900001:2'] = {'season_number': 2, 'episodes': episodes(2, 8, date(2026, 4, 20), unaired=2)}
    ended = {'id': 900002, 'name': 'Fixture Ended Show', 'original_name': 'Fixture Ended Show', 'status': 'Ended',
             'first_air_date': '2020-01-10', 'overview': 'Ended fixture series', 'poster_path': None,
             'seasons': [{'season_number': 1, 'episode_count': 3, 'air_date': '2020-01-10'}]}
    data['tv']['900002'] = ended
    data['season']['900002:1'] = {'season_number': 1, 'episodes': episodes(1, 3, date(2020, 1, 10))}
    data['movie']['900101'] = {'id': 900101, 'title': 'Fixture Film 1', 'release_date': '2020-05-01', 'runtime': 100,
                               'overview': 'First of a collection', 'genres': [], 'belongs_to_collection': {'id': 900900, 'name': 'Fixture Collection'}}
    data['movie']['900102'] = {'id': 900102, 'title': 'Fixture Film 2', 'release_date': '2022-05-01', 'runtime': 110,
                               'overview': 'Second of a collection', 'genres': [], 'belongs_to_collection': {'id': 900900, 'name': 'Fixture Collection'}}
    data['movie']['900103'] = {'id': 900103, 'title': 'Fixture Film 3 (unreleased)', 'release_date': '2026-12-01', 'runtime': 0,
                               'overview': 'Not released yet', 'genres': [], 'belongs_to_collection': {'id': 900900, 'name': 'Fixture Collection'}}
    data['collection']['900900'] = {'id': 900900, 'name': 'Fixture Collection', 'parts': [
        {'id': 900102, 'title': 'Fixture Film 2', 'release_date': '2022-05-01'},
        {'id': 900101, 'title': 'Fixture Film 1', 'release_date': '2020-05-01'},
        {'id': 900103, 'title': 'Fixture Film 3 (unreleased)', 'release_date': '2026-12-01'}]}
    return data


if __name__ == '__main__':
    os.makedirs(os.path.join(HERE, 'fixtures'), exist_ok=True)
    path = os.path.join(HERE, 'fixtures', 'tmdb_fixture.json')
    with open(path, 'w', encoding='utf-8') as handle:
        json.dump(build(), handle, indent=1, ensure_ascii=False)
    print('wrote', path)
