from pathlib import Path

import os
import shutil

from models import *

SrcRoot = Path('/media/nfs/SRS/storage/0/')

ShownRoot = Path('/media/nfs/SRS/export/shown/')
HiddenRoot = Path('/media/nfs/SRS/export/hidden/')

session = Session()

for entry in os.scandir(SrcRoot):
    if entry.is_dir():
        for study in session.query(Study).filter_by(run=entry.name):
            print(entry.name, study.naming())

            src = Path(entry.path) / study.patient_id / study.naming()
            src_shown = src/'shown'
            src_hidden = src/'hidden'
            # print(src_shown, src_hidden)

            dst = f'{study.patient_id}/{study.naming()}'
            dst_shown  = ShownRoot /dst
            dst_hidden = HiddenRoot/dst
            # print(dst)

            print(src_shown, dst_shown)
            if dst_shown.is_dir():
                shutil.rmtree(dst_shown)
            shutil.move(src_shown , dst_shown)

            print(src_hidden, dst_hidden)     
            if dst_hidden.is_dir():
                shutil.rmtree(dst_hidden)
            shutil.move(src_hidden, dst_hidden)     

        shutil.rmtree(entry.path)