from .UpdatePfSenseConfiguration import UpdatePfSenseConfiguration
from ..OpsiServer.OpsiChangeMysqlSecret import OpsiChangeMysqlSecret
from ..OpsiServer.SetOpsiDomadminPassword import SetOpsiDomadminPassword
from ..OpsiServer.TriggerPcPatchPasswordChange import TriggerPcPatchPasswordChange
from ..Server.PopulateDomadminSecret import PopulateDomadminSecret
from ..Server.SetKerberosKeys import SetKerberosKeys
from ...SetupSteps.Server.DeleteLdbEntrySecret import DeleteLdbEntrySecret
from ..General.WriteSecretFile import WriteSecretFile
from ..Server.CreateSpnAccount import CreateSpnAccount
from ..Server.PopulateFwradiusSecret import PopulateFwradiusSecret
from ..Server.PopulateLdapAdminSecret import PopulateLdapAdminSecret
from ..Server.SetAdminDiarySecret import SetAdminDiarySecret
from ..Server.SetRadiusSecret import SetRaduisSecret
from ..Server.SetRawLdapPassword import SetRawLdapPassword
from ...CommonFunctions import generate_random_password, generate_simple_password
from ...Machine.PaedMLMachineCollection import LMZ_COMMON_CREDENTIALS, MACHINE_COLLECTION
from ..General.CheckForSshSession import CheckForSSHSession
from ..Server.SetLdapPassword import SetLdapPassword
from ...SetupSteps.Playbooks.Playbook import Playbook


class SetInternalPasswords(Playbook):
    def start(self):
        if not self.is_sub_playbook:
            self.run_steps(CheckForSSHSession())


        ldap_secret = generate_random_password()
        ldap_backup_secret = generate_random_password()
        ldapsuche_secret = generate_random_password()
        join_backup_secret = generate_random_password()
        join_slave_secret = generate_random_password()
        guest_secret = generate_random_password()
        radius_secret = generate_random_password()
        kerberos_secret = generate_random_password()
        opsiconfd_secret = generate_random_password()
        schoolopsiadmin_secret = generate_random_password()

        self.run_steps(
            PopulateLdapAdminSecret(),
            SetRawLdapPassword(f'cn=admin,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', ldap_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'ldap', ldap_secret, permissions='640'),
            PopulateLdapAdminSecret(),
            SetRawLdapPassword(f'cn=backup,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', ldap_backup_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'ldap-backup', ldap_backup_secret),
            DeleteLdbEntrySecret(f'samAccountName=dns-{MACHINE_COLLECTION.server.get_hostname_short()},CN=Principals'),
            CreateSpnAccount(f'dns-{MACHINE_COLLECTION.server.get_hostname_short()}', f'DNS/{MACHINE_COLLECTION.server.get_hostname()}', 'dns.keytab'),
            SetLdapPassword(f'uid=ldapsuche,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', ldapsuche_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'ldapsuche', ldapsuche_secret),
            WriteSecretFile(MACHINE_COLLECTION.backup, 'ldapsuche', ldapsuche_secret),
            SetLdapPassword(f'uid=join-backup,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', join_backup_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'backup-join', join_backup_secret),
            SetLdapPassword(f'uid=join-slave,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', join_slave_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'slave-join', join_slave_secret),
            SetLdapPassword(f'uid=guest,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', guest_secret),
            SetRaduisSecret(radius_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'fwradius', radius_secret),
            PopulateFwradiusSecret()
        )

        if MACHINE_COLLECTION.server.check_if_file_exists('/etc/admin-diary.secret'):
            admin_diary_secret = generate_simple_password()
            self.run_steps(
                SetAdminDiarySecret(admin_diary_secret),
                WriteSecretFile(MACHINE_COLLECTION.server, 'admin-diary', admin_diary_secret)
            )

        self.run_steps(
            SetKerberosKeys(kerberos_secret),
            TriggerPcPatchPasswordChange(),
            SetLdapPassword(f'uid=opsiconfd,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', opsiconfd_secret),
            SetLdapPassword(f'uid=schoolopsiadmin,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', schoolopsiadmin_secret),
            WriteSecretFile(MACHINE_COLLECTION.server, 'schoolopsiadmin', schoolopsiadmin_secret),
            WriteSecretFile(MACHINE_COLLECTION.backup, 'schoolopsiadmin', schoolopsiadmin_secret),
            PopulateDomadminSecret(),
            SetOpsiDomadminPassword(self.credentials.domadmin),
            OpsiChangeMysqlSecret()
        )

        if not self.is_sub_playbook:
            self.run_sub_playbook(UpdatePfSenseConfiguration)