#!/usr/bin/env python3
from pathlib import Path
import argparse
import shutil

ROOT = Path('/mnt/nas/media/tvshows')


def move(src: Path, dst: Path, apply: bool):
    print(f"MV  {src} -> {dst}")
    if not apply:
        return
    dst.parent.mkdir(parents=True, exist_ok=True)
    if dst.exists():
        print(f"SKIP target exists: {dst}")
        return
    shutil.move(str(src), str(dst))


def main():
    ap = argparse.ArgumentParser(description='Normalize TV show names for Jellyfin')
    ap.add_argument('--apply', action='store_true', help='actually perform changes')
    args = ap.parse_args()
    apply = args.apply

    # 1) House of Cards season dirs
    show = ROOT / '纸牌屋'
    if show.exists():
        for p in sorted(show.iterdir()):
            if p.is_dir() and p.name.startswith('Season') and p.name not in [f'Season {i:02d}' for i in range(100)]:
                suffix = p.name[len('Season'):]
                if suffix.isdigit():
                    move(p, show / f'Season {int(suffix):02d}', apply)

    # 2) Hi My Sweetheart junk files to _junk
    show = ROOT / '海派甜心'
    if show.exists():
        for p in sorted(show.iterdir()):
            if p.is_file() and (p.suffix.lower() == '.webp' or p.name.endswith('.aria2')):
                move(p, show / '_junk' / p.name, apply)

    # 3) Love Apartment movie-like folder move aside
    show = ROOT / '爱情公寓'
    movie_dir = show / '爱情公寓 (2018)'
    if movie_dir.exists():
        move(movie_dir, ROOT / '爱情公寓（电影特别篇待整理）', apply)

    print('\nDone.' if apply else '\nDry-run done. Re-run with --apply to execute.')
    print('Note: 黑镜 Season 00 was intentionally not auto-changed; it needs manual confirmation.')


if __name__ == '__main__':
    main()
