from peconvert import convert, ImageTypes
from tqdm import tqdm
from sqlalchemy.orm import Session
import os
import sys
from db.session import SessionLocal
from models.entry import Entry
from models.images import Image

def main(folder_path: str, export: str, session: Session):
    files = os.listdir(folder_path)
    for file in tqdm(files):
        entry = session.query(Entry).filter(Entry.sha256 == file).first()
        image = Image(
            entry_id = entry.id,
            filename = file
        )
        file_path = f"{folder_path}/{file}"
        export_path = f"{export}/{file}.png"
        convert(file_path, export_path)
        session.add(image)
        session.commit()


if __name__ == "__main__":
    session = SessionLocal()
    if len(sys.argv) == 3:
        main(sys.argv[1], sys.argv[2], session)
    else:
        raise ValueError()