import os

from api_controllers.postgres.engine import get_db
from init_app_defaults import create_application_defaults
from init_enums import init_api_enums_build_types
from init_alembic import generate_alembic
from init_occupant_types import create_occupant_types_defaults
from init_services import create_modules_and_services_and_actions
from init_address import create_one_address
from init_occ_defaults import create_occupant_defaults

set_alembic = bool(int(os.getenv("SET_ALEMBIC"), 0))

if __name__ == "__main__":
    print(f"Set alembic: {set_alembic}")

    with get_db() as db_session:
        if set_alembic:
            generate_alembic(session=db_session)
        try:
            create_one_address(db_session=db_session)
        except Exception as e:
            print(f"Error creating address: {e}")
        try:
            init_api_enums_build_types(db_session=db_session)
        except Exception as e:
            print(f"Error creating enums: {e}")
        try:
            create_application_defaults(db_session=db_session)
        except Exception as e:
            print(f"Error creating application defaults: {e}")
        try:
            create_occupant_types_defaults(db_session=db_session)
        except Exception as e:
            print(f"Error creating occupant types defaults: {e}")
        try:
            create_modules_and_services_and_actions(db_session=db_session)
        except Exception as e:
            print(f"Error creating modules and services and actions: {e}")
        try:
            create_occupant_defaults(db_session=db_session)
        except Exception as e:
            print(f"Error creating occupant defaults: {e}")
