# -*- coding: utf-8 -*-
import csv
from collections import defaultdict

typen = []
jahre = []
ratings = []
dauern = []
seasons = []
titles = []

count_types = defaultdict(int)
count_ratings = defaultdict(int)

with open("netflix.csv", encoding="utf-8") as f:
    reader = csv.reader(f, delimiter=';')

    header = next(reader)

    for row in reader:
        show_id, typ, title, director, country, release_year, rating, duration, season, listed_in = row

        typen.append(typ)
        titles.append(title)
        count_types[typ] += 1
        count_ratings[rating] += 1

        jahre.append(int(release_year) if release_year.strip() else 0)
        ratings.append(rating)
        dauern.append(int(duration) if duration.strip() else 0)
        seasons.append(int(season) if season.strip() else 0)

anz_movies = count_types["Movie"]
anz_series = count_types["TV Show"]

movies_vor_2020 = sum(
    1 for t, j in zip(typen, jahre) if t == "Movie" and j != 0 and j < 2020
)

anz_pg13 = count_ratings["PG-13"]

movie_dauern = [d for t, d in zip(typen, dauern) if t == "Movie" and d > 0]
durchschnitt_dauer = (sum(movie_dauern) / len(movie_dauern)) if movie_dauern else 0

shows_eine_season = [
    title for t, title, s in zip(typen, titles, seasons)
    if t == "TV Show" and s == 1
]

if seasons:
    max_season = max(seasons)
    stelle = seasons.index(max_season)
    show_mit_meisten_seasons = titles[stelle]
else:
    max_season = 0
    show_mit_meisten_seasons = None

print(f"Anzahl Movies: {anz_movies}")
print(f"Anzahl Serien: {anz_series}")
print(f"Movies vor 2020: {movies_vor_2020}")
print(f"Anzahl PG-13 Inhalte: {anz_pg13}")
print(f"Durchschnittliche Movie-Dauer: {durchschnitt_dauer:.2f} Minuten")
print(f"{'-'*80}\nTV-Shows mit nur 1 Season:", *shows_eine_season, sep="\n")
print(f"{'-'*80}\nTV-Show mit den meisten Seasons: {show_mit_meisten_seasons} mit {max_season} Seasons")
