from pathlib import Path
from database import Session, Comic, Movie, TvShow, Game, TvEpisode, DupComic

OLD_COMICS_DIRECTORY = Path("/usb/storage/media/Comics/")
OLD_MOVIES_DIRECTORY = Path("/usb/storage/media/Videos/Movies/")
OLD_TV_SHOWS_DIRECTORY = Path("/usb/storage/media/Videos/TV/")
OLD_GAMES_DIRECTORY = Path("/usb/storage/Games/")

NEW_COMICS_DIRECTORY = Path("/srv/comics/")
NEW_MOVIES_DIRECTORY = Path("/srv/movies/")
NEW_TV_SHOWS_DIRECTORY = Path("/srv/tv/")
NEW_GAMES_DIRECTORY = Path("/srv/games/")


def update_db_paths(table, old_root, new_root):
    session = Session()

    rows = session.query(table).all()
    for row in rows:
        absolute = Path(row.path)
        relative = absolute.relative_to(old_root)
        print(absolute)
        print(new_root / relative)
        print()
        row.path = str(new_root / relative)
    session.commit()


update_db_paths(Comic, OLD_COMICS_DIRECTORY, NEW_COMICS_DIRECTORY)
update_db_paths(DupComic, OLD_COMICS_DIRECTORY, NEW_COMICS_DIRECTORY)
update_db_paths(Movie, OLD_MOVIES_DIRECTORY, NEW_MOVIES_DIRECTORY)
update_db_paths(TvShow, OLD_TV_SHOWS_DIRECTORY, NEW_TV_SHOWS_DIRECTORY)
update_db_paths(TvEpisode, OLD_TV_SHOWS_DIRECTORY, NEW_TV_SHOWS_DIRECTORY)
update_db_paths(Game, OLD_GAMES_DIRECTORY, NEW_GAMES_DIRECTORY)
