from ...Logging.Logger import Severity, log
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ..Playbooks.Playbook import FileCleanupTask, PlaybookCredentialCollection
from ...SetupSteps.SetupStep import SetupStep


class SetupSso(SetupStep):
    def run(self, file_cleanup_stack: list[FileCleanupTask], credentials: PlaybookCredentialCollection) -> bool:
        log('Setting up SSO on server...')
        log('Setting up SSO certificates...', Severity.DEBUG)
        server = MACHINE_COLLECTION.server
        # pem = server.read_file(f'/etc/univention/ssl/ucs-sso.{LMZ_COMMON_CREDENTIALS.get_domainname()}/cert.pem')
        # key = server.read_file(f'/etc/univention/ssl/ucs-sso.{LMZ_COMMON_CREDENTIALS.get_domainname()}/private.key')
        # server.write_file(f'/etc/simplesamlphp/ucs-sso.{LMZ_COMMON_CREDENTIALS.get_domainname()}-idp-certificate.crt', pem)
        # server.write_file(f'/etc/simplesamlphp/ucs-sso.{LMZ_COMMON_CREDENTIALS.get_domainname()}-idp-certificate.key', key)

        # stdout, stderr, retval = server.execute_shell_command('systemctl', 'restart', 'univention-saml.service')
        #
        # if retval != 0:
        #     log(f'Failed to restart univention-saml.service: {stderr}', Severity.ERROR)
        #     return False

        # log('Activating SAML...', Severity.DEBUG)
        # server.delete(f'/usr/share/univention-management-console/saml/idp/login.{LMZ_COMMON_CREDENTIALS.get_domainname()}.xml')

        # stdout, stderr, retval = server.execute_shell_command('ucr', 'get', 'ucs/server/sso/fqdn')
        # if retval != 0:
        #     log(f'Failed to get SSO FQDN: {stderr}', Severity.ERROR)
        #     return False
        #
        # sso_fqdn = stdout.strip()
        #
        # stdout, stderr, retval = server.execute_shell_command('ucr', 'set', f'umc/saml/idp-server="https://{sso_fqdn}/simplesamlphp/saml2/idp/metadata.php"')
        # if retval != 0:
        #     log(f'Failed to set IDP server', Severity.WARN)

        # stdout, stderr, retval = server.execute_shell_command('systemctl', 'restart', 'univention-management-console-web-server.service')
        # if retval != 0:
        #     log(f'Failed to restart web console web server: {stderr}', Severity.ERROR)
        #     return False

        stdout, stderr, retval = server.execute_shell_command('univention-run-join-scripts',
                                                              '--force',
                                                              '--run-scripts',
                                                              '92univention-management-console-web-server.inst')
        if retval != 0:
            log(f'Failed to install web console server: {stderr if len(stderr) > 0 else stdout}', Severity.ERROR)
            return False

        stdout, stderr, retval = server.execute_shell_command('systemctl',
                                                              'restart',
                                                              'univention-management-console-server.service')
        if retval != 0:
            log(f'Failed to restart web console server: {stderr}', Severity.ERROR)
            return False

        stdout, stderr, retval = server.execute_shell_command('systemctl',
                                                              'restart',
                                                              'univention-management-console-web-server.service')
        if retval != 0:
            log(f'Failed to restart web console web server: {stderr}', Severity.ERROR)
            return False

        log('SSO setup complete.', Severity.DEBUG)
        return True