from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


def create_db():
    import models

    models.Base.metadata.create_all(bind=engine)


def _json_quotes(s):
    return s.replace("'", '"')


def adapt_movie_data(data_in: dict):
    import copy, json

    data_out = copy.deepcopy(data_in)

    # adapt genresd to stringlist
    breakpoint()

    data_out["genres"] = [
        genre["name"] for genre in json.loads(_json_quotes(data_in["genres"]))
    ]

    return data_out


def fill_db(
    db=SessionLocal(), movie_input_file: str = "input_data/movies_metadata_short.csv"
):
    create_db()
    import crud
    import csv

    with open(movie_input_file) as csvfile:
        for movie_data in csv.DictReader(csvfile):
            adapted_data = adapt_movie_data(movie_data)
            crud.create_movie(db, **adapted_data)


if __name__ == "__main__":
    fill_db()
