from autosubmit_api.config.basicConfig import APIBasicConfig
from autosubmit_api.database.db_manager import DbManager
from autosubmit_api.database import prepare_db
from autosubmit_api.workers.populate_details.populate import DetailsProcessor

class ExtendedDB:
    def __init__(self, root_path: str, db_name: str, as_times_db_name: str) -> None:
        self.root_path = root_path
        self.db_name = db_name
        self.main_db_manager = DbManager(root_path, db_name)
        self.as_times_db_manager = DbManager(root_path, as_times_db_name)

    def prepare_db(self):
        """
        Create tables and views that are required
        """
        self.prepare_main_db()
        prepare_db()


    def prepare_main_db(self):
        APIBasicConfig.read()
        DetailsProcessor(APIBasicConfig).create_details_table_if_not_exists()
        self.main_db_manager.create_view(
            'listexp',
            'select id,name,user,created,model,branch,hpc,description from experiment left join details on experiment.id = details.exp_id'
            )


