<?php

namespace WPQT\Capability;

if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('WPQT\Capability\CapabilityService')) {
    class CapabilityService
    {
        /**
         * Adds QuickTasker admin capabilities to the administrator role.
         *
         * This function checks if the WordPress administrator role has specific QuickTasker capabilities.
         * If the capabilities are not present, they are added to the administrator role.
         *
         * Capabilities added:
         * - WP_QUICKTASKER_ADMIN_ROLE
         * - WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE
         * - WP_QUICKTASKER_ACCESS_USER_PAGE_APP
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS
         *
         * @return void
         */
        public function addQuickTaskerAdminCapabilityToAdminRole()
        {
            $adminRole = get_role('administrator');

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ACCESS_USER_PAGE_APP)) {
                $adminRole->add_cap(WP_QUICKTASKER_ACCESS_USER_PAGE_APP);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS)) {
                $adminRole->add_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS);
            }

            if (!$adminRole->has_cap(WP_QUICKTASKER_VIEW_MY_TASKS)) {
                $adminRole->add_cap(WP_QUICKTASKER_VIEW_MY_TASKS);
            }
        }

        /**
         * Removes QuickTasker capabilities from the administrator role.
         *
         * This function checks if the administrator role has the following capabilities:
         * - WP_QUICKTASKER_ADMIN_ROLE
         * - WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE
         * - WP_QUICKTASKER_ACCESS_USER_PAGE_APP
         * - WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS
         *
         * If any of these capabilities are found, they are removed from the administrator role.
         *
         * @return void
         */
        public function removeQuickTaskerAdminCapabilityFromAdminRole()
        {
            $adminRole = get_role('administrator');

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE_ALLOW_DELETE);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_USERS);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_SETTINGS);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_ARCHIVE);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ACCESS_USER_PAGE_APP)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ACCESS_USER_PAGE_APP);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS)) {
                $adminRole->remove_cap(WP_QUICKTASKER_ADMIN_ROLE_MANAGE_QUICKTASKER_SESSIONS);
            }

            if ($adminRole->has_cap(WP_QUICKTASKER_VIEW_MY_TASKS)) {
                $adminRole->remove_cap(WP_QUICKTASKER_VIEW_MY_TASKS);
            }
        }

        /**
         * Updates the capabilities of a WordPress user.
         *
         * @param int $userId The ID of the user whose capabilities are to be updated.
         * @param array $capabilities An associative array of capabilities to be updated.
         *                            The key is the capability name and the value is a boolean
         *                            indicating whether to add (true) or remove (false) the capability.
         *
         * @return void
         */
        public function updateWPUserCapabilities($userId, $capabilities)
        {
            $user = get_user_by('id', $userId);

            if ($user) {
                foreach ($capabilities as $capability => $value) {
                    if ($value) {
                        $user->add_cap($capability);
                    } else {
                        $user->remove_cap($capability);
                    }
                }
            }
        }
    }
}
