from rhodecode.lib.dbmigrate.versions import _reset_base
from rhodecode.model import meta, init_model_encryption
from rhodecode.model.db import User


def upgrade(migrate_engine):
    """
    Upgrade operations go here.
    Don't create your own engine; bind migrate_engine to your metadata
    """
    _reset_base(migrate_engine)

    from rhodecode.lib.dbmigrate.schema import db_4_20_0_1 as db

    init_model_encryption(db)

    UserModel: User = db.User

    username = "ai_rhodecode"
    if not User.get_by_username(username):
        ghost_user = UserModel()
        ghost_user.name = "RhodeCode Helper"
        ghost_user.username = username
        ghost_user.lastname = "User"
        ghost_user.strict_creation_check = False
        ghost_user.email = "ai@rhodecode.org"
        ghost_user.active = False
        ghost_user.description = "System user"
        ghost_user.is_new_user = False

        session = meta.Session()
        session.add(ghost_user)
        session.commit()


def downgrade(migrate_engine):
    pass
