<?php
/**
 * Multisite Events Observer
 *
 * Monitors site lifecycle (create, delete, archive, spam, etc.)
 * and multisite user management (add/remove from blog, super admin grants).
 *
 * Only registered when is_multisite() returns true (handled by the registry).
 *
 * 22 event types across 22 hooks.
 *
 * @package VMPFence
 * @since 2.2.5
 */

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

class VMPFence_Audit_Log_Observer_Multisite extends VMPFence_Audit_Log_Observer {

    /**
     * Register all multisite-related WordPress hooks.
     *
     * @return void
     */
    public function register_hooks() {

        // ----- Site lifecycle (13 hooks) -----
        add_action('wp_initialize_site', array($this, 'on_site_created'));
        add_action('wp_delete_site',     array($this, 'on_site_deleted'));
        add_action('wp_update_site',     array($this, 'on_site_updated'));
        add_action('activate_blog',      array($this, 'on_site_activated'));
        add_action('deactivate_blog',    array($this, 'on_site_deactivated'));
        add_action('archive_blog',       array($this, 'on_site_archived'));
        add_action('unarchive_blog',     array($this, 'on_site_unarchived'));
        add_action('make_delete_blog',   array($this, 'on_site_trashed'));
        add_action('make_undelete_blog', array($this, 'on_site_restored'));
        add_action('update_blog_public', array($this, 'on_site_visibility'));
        add_action('make_spam_blog',     array($this, 'on_site_spam'));
        add_action('make_ham_blog',      array($this, 'on_site_unspam'));
        add_action('after_signup_site',  array($this, 'on_blog_signup'), 10, 7);

        // ----- User management (9 hooks) -----
        add_action('add_user_to_blog',    array($this, 'on_user_added'), 10, 3);
        add_action('remove_user_from_blog', array($this, 'on_user_removed'), 10, 2);
        add_action('wpmu_new_user',       array($this, 'on_multisite_user_created'));
        add_action('wpmu_delete_user',    array($this, 'on_multisite_user_deleted'));
        add_action('wpmu_activate_user',  array($this, 'on_multisite_user_activated'), 10, 3);
        add_action('invite_user',         array($this, 'on_multisite_user_invited'), 10, 3);
        add_action('after_signup_user',   array($this, 'on_multisite_user_signed_up'), 10, 4);
        add_action('granted_super_admin', array($this, 'on_super_admin_granted'));
        add_action('revoked_super_admin', array($this, 'on_super_admin_revoked'));
    }

    // =========================================================================
    // SITE LIFECYCLE CALLBACKS (13)
    // =========================================================================

    /**
     * New site created.
     *
     * @param \WP_Site $new_site
     */
    public function on_site_created($new_site) {
        $this->log('multisite_site_created', array(
            'blog_id' => $new_site->blog_id,
            'domain'  => $new_site->domain,
            'path'    => $new_site->path,
        ), 'info');
    }

    /**
     * Site deleted.
     *
     * @param \WP_Site $old_site
     */
    public function on_site_deleted($old_site) {
        $this->log('multisite_site_deleted', array(
            'blog_id' => $old_site->blog_id,
            'domain'  => $old_site->domain,
            'path'    => $old_site->path,
        ), 'warning');
    }

    /**
     * Site updated.
     *
     * @param \WP_Site $new_site
     */
    public function on_site_updated($new_site) {
        $this->log('multisite_site_updated', array(
            'blog_id' => $new_site->blog_id,
            'domain'  => $new_site->domain,
        ), 'info');
    }

    /**
     * Site activated.
     *
     * @param int $blog_id
     */
    public function on_site_activated($blog_id) {
        $this->log('multisite_site_activated', array(
            'blog_id' => $blog_id,
        ), 'info');
    }

    /**
     * Site deactivated.
     *
     * @param int $blog_id
     */
    public function on_site_deactivated($blog_id) {
        $this->log('multisite_site_deactivated', array(
            'blog_id' => $blog_id,
        ), 'warning');
    }

    /**
     * Site archived.
     *
     * @param int $blog_id
     */
    public function on_site_archived($blog_id) {
        $this->log('multisite_site_archived', array(
            'blog_id' => $blog_id,
        ), 'info');
    }

    /**
     * Site unarchived.
     *
     * @param int $blog_id
     */
    public function on_site_unarchived($blog_id) {
        $this->log('multisite_site_unarchived', array(
            'blog_id' => $blog_id,
        ), 'info');
    }

    /**
     * Site moved to trash (marked as deleted).
     *
     * @param int $blog_id
     */
    public function on_site_trashed($blog_id) {
        $this->log('multisite_site_trashed', array(
            'blog_id' => $blog_id,
        ), 'warning');
    }

    /**
     * Site restored from trash (undeleted).
     *
     * @param int $blog_id
     */
    public function on_site_restored($blog_id) {
        $this->log('multisite_site_restored', array(
            'blog_id' => $blog_id,
        ), 'info');
    }

    /**
     * Site visibility changed (public/private).
     *
     * @param int $blog_id
     */
    public function on_site_visibility($blog_id) {
        $blog = get_blog_details($blog_id);
        $this->log('multisite_site_visibility_changed', array(
            'blog_id' => $blog_id,
            'public'  => $blog ? $blog->public : null,
        ), 'warning');
    }

    /**
     * Site marked as spam.
     *
     * @param int $blog_id
     */
    public function on_site_spam($blog_id) {
        $this->log('multisite_site_spam', array(
            'blog_id' => $blog_id,
        ), 'warning');
    }

    /**
     * Site removed from spam.
     *
     * @param int $blog_id
     */
    public function on_site_unspam($blog_id) {
        $this->log('multisite_site_unspam', array(
            'blog_id' => $blog_id,
        ), 'info');
    }

    /**
     * Blog signup completed.
     *
     * @param string $domain
     * @param string $path
     * @param string $title
     * @param string $user
     * @param string $user_email
     * @param string $key
     * @param array  $meta
     */
    public function on_blog_signup($domain, $path, $title, $user, $user_email, $key, $meta) {
        $this->log('multisite_blog_signup', array(
            'domain'     => $domain,
            'path'       => $path,
            'title'      => $title,
            'user'       => $user,
            'user_email' => $user_email,
        ), 'info');
    }

    // =========================================================================
    // USER MANAGEMENT CALLBACKS (9)
    // =========================================================================

    /**
     * User added to a blog.
     *
     * @param int    $user_id
     * @param string $role
     * @param int    $blog_id
     */
    public function on_user_added($user_id, $role, $blog_id) {
        $user = get_userdata($user_id);
        $this->log('multisite_user_added', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
            'role'     => $role,
            'blog_id'  => $blog_id,
        ), 'info');
    }

    /**
     * User removed from a blog.
     *
     * @param int $user_id
     * @param int $blog_id
     */
    public function on_user_removed($user_id, $blog_id) {
        $user = get_userdata($user_id);
        $this->log('multisite_user_removed', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
            'blog_id'  => $blog_id,
        ), 'warning');
    }

    /**
     * New multisite user created.
     *
     * @param int $user_id
     */
    public function on_multisite_user_created($user_id) {
        $user = get_userdata($user_id);
        $this->log('multisite_user_created', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
        ), 'info');
    }

    /**
     * Multisite user permanently deleted.
     *
     * @param int $user_id
     */
    public function on_multisite_user_deleted($user_id) {
        $this->log('multisite_user_deleted', array(
            'user_id' => $user_id,
        ), 'warning');
    }

    /**
     * Multisite user account activated.
     *
     * @param int    $user_id
     * @param string $password
     * @param array  $meta
     */
    public function on_multisite_user_activated($user_id, $password, $meta) {
        $user = get_userdata($user_id);
        $this->log('multisite_user_activated', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
        ), 'info');
    }

    /**
     * User invited to a blog.
     *
     * @param int    $user_id
     * @param string $role
     * @param string $newuser_key
     */
    public function on_multisite_user_invited($user_id, $role, $newuser_key) {
        $this->log('multisite_user_invited', array(
            'user_id' => $user_id,
            'role'    => $role,
        ), 'info');
    }

    /**
     * User signed up for the network.
     *
     * @param string $user
     * @param string $user_email
     * @param string $key
     * @param array  $meta
     */
    public function on_multisite_user_signed_up($user, $user_email, $key, $meta) {
        $this->log('multisite_user_signed_up', array(
            'user'       => $user,
            'user_email' => $user_email,
        ), 'info');
    }

    /**
     * Super Admin privileges granted.
     *
     * @param int $user_id
     */
    public function on_super_admin_granted($user_id) {
        $user = get_userdata($user_id);
        $this->log('multisite_super_admin_granted', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
        ), 'critical');
    }

    /**
     * Super Admin privileges revoked.
     *
     * @param int $user_id
     */
    public function on_super_admin_revoked($user_id) {
        $user = get_userdata($user_id);
        $this->log('multisite_super_admin_revoked', array(
            'user_id'  => $user_id,
            'username' => $user ? $user->user_login : '',
        ), 'critical');
    }
}
