import os
from typing import List

from sqlmodel.sql.expression import select
from utilities.filepath import get_home_dir
from sqlmodel import create_engine, SQLModel, Session

# these are imported so that the initialization of the database can be done
from schemas.common.event import Event
from schemas.common.extension import Extension


class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]


class DB(metaclass=Singleton):
    def __init__(self) -> None:
        self.engine = create_engine("sqlite:///{}".format(os.path.join(get_home_dir(), "extensions.db")))

    def initialize(self) -> None:
        SQLModel.metadata.create_all(self.engine)

    def save(self, obj: SQLModel) -> None:
        session = Session(self.engine)
        session.add(obj)
        session.commit()
        session.refresh(obj)
        return obj

    def fetch_extensions(self)-> List[Extension]:
        with Session(self.engine) as session:
            results = session.exec(select(Extension)).all()
        return results

if __name__=="__main__":
    DB().initialize()
sqlmodel.SQLModel.metadata.create_all sqlmodel.Session sqlmodel.sql.expression.select utilities.filepath.get_home_dir
[((793, 834), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['self.engine'], {}), '(self.engine)\n', (821, 834), False, 'from sqlmodel import create_engine, SQLModel, Session\n'), ((897, 917), 'sqlmodel.Session', 'Session', (['self.engine'], {}), '(self.engine)\n', (904, 917), False, 'from sqlmodel import create_engine, SQLModel, Session\n'), ((1080, 1100), 'sqlmodel.Session', 'Session', (['self.engine'], {}), '(self.engine)\n', (1087, 1100), False, 'from sqlmodel import create_engine, SQLModel, Session\n'), ((715, 729), 'utilities.filepath.get_home_dir', 'get_home_dir', ([], {}), '()\n', (727, 729), False, 'from utilities.filepath import get_home_dir\n'), ((1148, 1165), 'sqlmodel.sql.expression.select', 'select', (['Extension'], {}), '(Extension)\n', (1154, 1165), False, 'from sqlmodel.sql.expression import select\n')]