from .RebootServers import RebootServers
from .SetInternalPasswords import SetInternalPasswords
from .UpdatePfSenseConfiguration import UpdatePfSenseConfiguration
from ..General.CheckForSshSession import CheckForSSHSession
from ..General.EnsureSshKeysExist import EnsureSshKeysExist
from ..General.SetPamPassword import SetPamPassword
from ..General.SetupSshTrust import SetupSshTrust
from ..General.TestConnection import TestConnection
from ..General.TestInternetConnection import TestInternetConnection
from ..General.TriggerUcsPasswordChange import TriggerUcsPasswordChange
from ..General.WriteRootSecret import WriteRootSecret
from ..General.WriteSecretFile import WriteSecretFile
from ..OpsiServer.CopyVeyonKeys import CopyVeyonKeys
from ..OpsiServer.SetOpsiProductDefaultValues import SetOpsiProductDefaultValues
from ..Server.MigrateToKeycloak import MigrateToKeycloak
from ..Server.PopulateLdapBase import PopulateLdapBase
from ..Server.PopulateRootSecret import PopulateRootSecret
from ..Server.RegenerateVeyonKeys import RegenerateVeyonKeys
from ..Server.SetLdapPassword import SetLdapPassword
from ..General.SetSystemUuid import SetSystemUuid
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ...Logging.Logger import Severity, log
from ...SetupSteps.Playbooks.Playbook import Playbook


class ReinitializeAll(Playbook):
    def start(self):
        self.run_steps(
            TestInternetConnection(),
            CheckForSSHSession(),
            TestConnection(MACHINE_COLLECTION.firewall, 22),
            TestConnection(MACHINE_COLLECTION.backup, 22),
            PopulateRootSecret(),
            EnsureSshKeysExist()
        )

        if not MACHINE_COLLECTION.initialized:
            if self.credentials.root is not None:
                log("Found existing credentials.", Severity.DEBUG)
                LMZ_COMMON_CREDENTIALS.set_password(self.credentials.root)
            log("Initializing machine connections...", Severity.INFO)
            MACHINE_COLLECTION.initialize()

        password = self.ui.get_password("Please enter the desired root password:", verify=True)
        while len(password) < 8:
            self.ui.print("Password is too short. Please enter at least 8 characters.")
            password = self.ui.get_password("Please enter the desired root password:", verify=True)
        self.credentials.root = password
        self.credentials.domadmin = password

        self.run_steps(
            SetPamPassword(MACHINE_COLLECTION.server, "root", password, "sha512crypt"),
            WriteRootSecret(MACHINE_COLLECTION.server),
            SetPamPassword(MACHINE_COLLECTION.backup, "root", password, "sha512crypt"),
            WriteRootSecret(MACHINE_COLLECTION.backup),
            WriteSecretFile(MACHINE_COLLECTION.server, "domadmin", password),
            PopulateLdapBase(),
            SetLdapPassword(f'uid=Administrator,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', password),
            SetLdapPassword(f'uid=netzwerkberater,cn=admins,cn=users,ou=schule,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', password),
            SetupSshTrust()
        )

        self.run_sub_playbook(SetInternalPasswords)

        self.run_steps(
            TriggerUcsPasswordChange(MACHINE_COLLECTION.server),
            TriggerUcsPasswordChange(MACHINE_COLLECTION.backup),
            SetSystemUuid(),
            RegenerateVeyonKeys(),
            CopyVeyonKeys(),
            SetOpsiProductDefaultValues()
        )

        self.run_sub_playbook(UpdatePfSenseConfiguration)

        self.run_steps(MigrateToKeycloak())

        log('!!SUCCESS!! Reinitialization completed successfully.')
        self.run_sub_playbook(RebootServers)
