<?php

namespace QUI\MasterLogin;

use QUI;
use QUI\System\Console\Tools\MigrationV2;

use function date;

/**
 * Class Events
 *
 * Main event handler for quiqqer/masterlogin
 */
class Events
{
    protected static array $userSaveSnapshots = [];

    /**
     * quiqqer/core: onTemplateGetHeader
     *
     * Load MasterLogin control in template
     *
     * @param QUI\Template $Template
     * @return void
     */
    public static function onTemplateGetHeader(QUI\Template $Template): void
    {
        if (!QUI::getUsers()->isAuth(QUI::getUserBySession()) || !Manager::canUse()) {
            return;
        }

        $masterUser = '';
        $slaveUser = '';

        $masterUserData = [];
        $slaveUserData = [];

        if (Manager::isLoggedInAsSlave()) {
            // Fetch master user info
            $masterUserId = QUI::getSession()->get(Manager::SESSION_MASTER_USER_ID);

            try {
                $MasterUser = QUI::getUsers()->get($masterUserId);

                $masterUser = $MasterUser->getName() . ' (#' . $MasterUser->getId() . ')';
                $masterUserData = [
                    'id' => $MasterUser->getId(),
                    'name' => $MasterUser->getName(),
                    'email' => $MasterUser->getAttribute('email')
                ];
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }

            // Fetch slave user info (session user)
            /** @var QUI\Users\User $SlaveUser */
            $SlaveUser = QUI::getUserBySession();
            $slaveName = $SlaveUser->getName();
            $slaveCompany = '';

            try {
                $SlaveUserAddress = $SlaveUser->getStandardAddress();
                $slaveCompany = $SlaveUserAddress->getAttribute('company');

                if (!empty($SlaveUserAddress->getName())) {
                    $slaveName = $SlaveUserAddress->getName();
                }
            } catch (\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }

            $slaveUser = $slaveName . ' - ' . $slaveCompany . ' (#' . $SlaveUser->getId() . ')';

            $slaveUserData = [
                'id' => $SlaveUser->getId(),
                'name' => $slaveName,
                'email' => $SlaveUser->getAttribute('email'),
                'company' => $slaveCompany
            ];
        } else {
            // no slave user, get admin user data
            $MasterUser = QUI::getUserBySession();
            $masterUser = $MasterUser->getName() . ' (#' . $MasterUser->getId() . ')';
            $masterUserData = [
                'id' => $MasterUser->getId(),
                'name' => $MasterUser->getName(),
                'email' => $MasterUser->getAttribute('email')
            ];
        }

        $masterUserData = json_encode($masterUserData);
        $slaveUserData = json_encode($slaveUserData);

        $Template->extendFooter(
            '<div data-qui="package/quiqqer/masterlogin/bin/js/frontend/controls/MasterLogin"
                  data-qui-options-slave="' . (Manager::isLoggedInAsSlave() ? 1 : 0) . '"
                  data-qui-options-masteruser="' . $masterUser . '"
                  data-qui-options-slaveuser="' . $slaveUser . '"
                  data-qui-options-masteruserdata="' . htmlspecialchars($masterUserData, ENT_QUOTES, 'UTF-8') . '"
                  data-qui-options-slaveuserdata="' . htmlspecialchars($slaveUserData, ENT_QUOTES, 'UTF-8') . '"
             ></div>'
        );
    }

    /**
     * quiqqer/core: onAdminLoad
     *
     * If the user accesses the backend -> log out of any possible slave user
     *
     * @return void
     */
    public static function onAdminLoad(): void
    {
        Manager::log('masterlogin.admin_load.log_back_requested', [
            'session' => Manager::getSessionLogContext()
        ]);

        Manager::logBack();
    }

    public static function onUserLoginStart(int | string $userId): void
    {
        Manager::log('user.login.start', [
            'userId' => $userId,
            'session' => Manager::getSessionLogContext()
        ]);
    }

    public static function onUserLogin(QUI\Users\User $User): void
    {
        Manager::log('user.login.success', [
            'user' => Manager::getUserLogContext($User),
            'dbGroups' => Manager::getUserGroupsFromDatabase($User),
            'session' => Manager::getSessionLogContext()
        ]);
    }

    public static function onUserLoginError(int | string $userId, \Exception $Exception): void
    {
        $reason = null;

        if (method_exists($Exception, 'getAttribute')) {
            $reason = $Exception->getAttribute('reason');
        }

        Manager::log('user.login.error', [
            'userId' => $userId,
            'reason' => $reason,
            'exception' => $Exception->getMessage(),
            'session' => Manager::getSessionLogContext()
        ], QUI\System\Log::LEVEL_WARNING);
    }

    public static function onUserSaveBegin(QUI\Users\User $User): void
    {
        $snapshot = [
            'dbGroups' => Manager::getUserGroupsFromDatabase($User),
            'objectGroups' => $User->getGroups(false),
            'session' => Manager::getSessionLogContext()
        ];

        self::$userSaveSnapshots[$User->getUUID()] = $snapshot;

        Manager::log('user.save.begin', [
            'user' => Manager::getUserLogContext($User),
            'dbGroupsBefore' => $snapshot['dbGroups'],
            'objectGroupsBefore' => $snapshot['objectGroups'],
            'session' => $snapshot['session']
        ]);
    }

    public static function onUserSave(QUI\Users\User $User): void
    {
        Manager::log('user.save.before_database_write', [
            'user' => Manager::getUserLogContext($User),
            'objectGroups' => $User->getGroups(false),
            'session' => Manager::getSessionLogContext()
        ]);
    }

    public static function onUserSaveEnd(QUI\Users\User $User): void
    {
        $snapshot = self::$userSaveSnapshots[$User->getUUID()] ?? [
            'dbGroups' => [],
            'objectGroups' => [],
            'session' => []
        ];

        unset(self::$userSaveSnapshots[$User->getUUID()]);

        $dbGroupsBefore = $snapshot['dbGroups'];
        $dbGroupsAfter = Manager::getUserGroupsFromDatabase($User);
        $normalizedBefore = Manager::normalizeGroupIds($dbGroupsBefore);
        $normalizedAfter = Manager::normalizeGroupIds($dbGroupsAfter);
        $groupsChanged = $normalizedBefore !== $normalizedAfter;

        $context = [
            'user' => Manager::getUserLogContext($User),
            'dbGroupsBefore' => $dbGroupsBefore,
            'dbGroupsAfter' => $dbGroupsAfter,
            'objectGroupsBefore' => $snapshot['objectGroups'],
            'objectGroupsAfter' => $User->getGroups(false),
            'groupsChanged' => $groupsChanged,
            'sessionBefore' => $snapshot['session'],
            'sessionAfter' => Manager::getSessionLogContext()
        ];

        if (self::groupsWereReducedToEveryone($dbGroupsBefore, $dbGroupsAfter)) {
            Manager::log(
                'user.save.groups_reduced_to_everyone',
                $context,
                QUI\System\Log::LEVEL_WARNING
            );
        }

        Manager::log(
            $groupsChanged ? 'user.save.end.groups_changed' : 'user.save.end',
            $context,
            $groupsChanged ? QUI\System\Log::LEVEL_WARNING : QUI\System\Log::LEVEL_INFO
        );
    }

    public static function onUserLogoutBegin(QUI\Users\User $User): void
    {
        Manager::log('user.logout.begin', [
            'user' => Manager::getUserLogContext($User),
            'dbGroups' => Manager::getUserGroupsFromDatabase($User),
            'session' => Manager::getSessionLogContext()
        ]);
    }

    /**
     * quiqqer/core: onUserLogout
     *
     * @param QUI\Users\User $User
     * @return void
     */
    public static function onUserLogout(QUI\Users\User $User): void
    {
        Manager::log('user.logout', [
            'user' => Manager::getUserLogContext($User),
            'dbGroups' => Manager::getUserGroupsFromDatabase($User),
            'session' => Manager::getSessionLogContext()
        ]);

        // Update log entry
        try {
            QUI::getDataBase()->update(
                Manager::getLogTable(),
                ['end' => date('Y-m-d H:i:s')],
                [
                    'end' => null,
                    'slave_id' => $User->getUUID(),
                    'sec_hash' => QUI::getUsers()->getSecHash()
                ]
            );
        } catch (\Exception $Exception) {
            Manager::log('user.logout.masterlogin_log_update_failed', [
                'user' => Manager::getUserLogContext($User),
                'exception' => $Exception->getMessage(),
                'session' => Manager::getSessionLogContext()
            ], QUI\System\Log::LEVEL_ERROR);

            QUI\System\Log::writeException($Exception);
        }
    }

    public static function onQuiqqerMigrationV2(MigrationV2 $Console): void
    {
        $Console->writeLn('- Migrate master login');
        $table = QUI::getDBTableName('masterlogin_log');

        QUI::getDataBaseConnection()->executeStatement(
            'ALTER TABLE `' . $table . '` CHANGE `master_id` `master_id` VARCHAR(50) NOT NULL;'
        );

        QUI::getDataBaseConnection()->executeStatement(
            'ALTER TABLE `' . $table . '` CHANGE `slave_id` `slave_id` VARCHAR(50) NOT NULL;'
        );

        QUI::getDataBaseConnection()->executeStatement(
            'ALTER TABLE `' . $table . '` CHANGE `id` `id` VARCHAR(50) NOT NULL;'
        );

        QUI\Utils\MigrationV1ToV2::migrateUsers($table, [
            'master_id',
            'slave_id',
        ]);
    }

    protected static function groupsWereReducedToEveryone(array $before, array $after): bool
    {
        if (count(Manager::normalizeGroupIds($before)) <= 1) {
            return false;
        }

        try {
            $Everyone = new QUI\Groups\Everyone();
            $everyoneId = $Everyone->getUUID();
        } catch (\Throwable) {
            return false;
        }

        return Manager::normalizeGroupIds($after) === [$everyoneId];
    }
}
