<?php
/**
 * Dashboard Manager — FREE
 *
 * Controls the visibility of default WordPress dashboard widgets.
 *
 * Data structure stored in 'pnlrdb_hidden_dashboard_widgets':
 * array(
 *   'global'       => array( 'dashboard_right_now', ... ),  // widget IDs that are HIDDEN globally
 *   'role_hidden'  => array(
 *     'editor' => array( 'dashboard_primary', ... ),        // widget IDs hidden for this role
 *   ),
 *   'role_enabled' => array(
 *     'editor' => true,   // true = this role has a custom override active
 *   ),
 * )
 *
 * Priority logic:
 *   1. If the role has an override enabled  → use role_hidden[ role ]
 *   2. Otherwise                            → use global hidden list
 *
 * A widget is HIDDEN when its ID appears in the relevant list.
 * Empty list = all widgets visible (default WordPress behaviour).
 *
 * @package Panelra
 */

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

/**
 * Class Pnlrdb_Dashboard_Manager
 */
class Pnlrdb_Dashboard_Manager {

	/** Option key used to store all settings. */
	const OPTION_KEY = 'pnlrdb_hidden_dashboard_widgets';

	/**
	 * Returns the list of known default WordPress dashboard widgets.
	 *
	 * @return array  Associative array: widget_id => array( label, desc, context )
	 */
	public static function get_default_widgets() {
		return array(
			'dashboard_right_now'   => array(
				'label'   => __( 'At a Glance', 'panelra-dashboard-builder' ),
				'desc'    => __( 'Posts, pages and comments summary.', 'panelra-dashboard-builder' ),
				'context' => 'normal',
			),
			'dashboard_activity'    => array(
				'label'   => __( 'Activity', 'panelra-dashboard-builder' ),
				'desc'    => __( 'Recent published posts and pending comments.', 'panelra-dashboard-builder' ),
				'context' => 'normal',
			),
			'dashboard_quick_press' => array(
				'label'   => __( 'Quick Draft', 'panelra-dashboard-builder' ),
				'desc'    => __( 'Write and save a quick draft post.', 'panelra-dashboard-builder' ),
				'context' => 'side',
			),
			'dashboard_primary'     => array(
				'label'   => __( 'WordPress News', 'panelra-dashboard-builder' ),
				'desc'    => __( 'Latest news from WordPress.org.', 'panelra-dashboard-builder' ),
				'context' => 'side',
			),
			'welcome_panel'         => array(
				'label'   => __( 'Welcome Panel', 'panelra-dashboard-builder' ),
				'desc'    => __( '"Welcome to WordPress" introduction panel.', 'panelra-dashboard-builder' ),
				'context' => 'normal',
				'special' => true,
			),
			'dashboard_site_health' => array(
				'label'   => __( 'Site Health Status', 'panelra-dashboard-builder' ),
				'desc'    => __( 'Checks for common configuration issues.', 'panelra-dashboard-builder' ),
				'context' => 'normal',
			),
		);
	}

	/**
	 * Constructor — hooks into wp_dashboard_setup.
	 */
	public function __construct() {
		add_action( 'wp_dashboard_setup', array( $this, 'apply_widget_visibility' ), 999 );
	}

	/**
	 * Remove hidden widgets from the current user's dashboard.
	 *
	 * @return void
	 */
	public function apply_widget_visibility() {
		if ( ! is_user_logged_in() ) {
			return;
		}

		$settings   = get_option( self::OPTION_KEY, array() );
		$all_ids    = array_keys( self::get_default_widgets() );
		$widgets    = self::get_default_widgets();
		$user_roles = (array) wp_get_current_user()->roles;

		$hidden = $this->resolve_hidden( $settings, $user_roles );

		foreach ( $all_ids as $widget_id ) {
			if ( ! in_array( $widget_id, $hidden, true ) ) {
				continue; // Widget is visible — skip.
			}

			if ( 'welcome_panel' === $widget_id ) {
				remove_action( 'welcome_panel', 'wp_welcome_panel' );
				update_user_meta( get_current_user_id(), 'show_welcome_panel', 0 );
				continue;
			}

			$context = $widgets[ $widget_id ]['context'] ?? 'normal';
			remove_meta_box( $widget_id, 'dashboard', $context );
		}
	}

	/**
	 * Determine which widget IDs should be hidden for the current user.
	 *
	 * @param  array $settings   Saved option value.
	 * @param  array $user_roles Array of role slugs for the current user.
	 * @return array             Widget IDs that should be hidden.
	 */
	private function resolve_hidden( $settings, $user_roles ) {
		$role_enabled = isset( $settings['role_enabled'] ) ? (array) $settings['role_enabled'] : array();
		$role_hidden  = isset( $settings['role_hidden'] )  ? (array) $settings['role_hidden']  : array();
		$global       = isset( $settings['global'] )       ? (array) $settings['global']       : array();

		// Check if any of the user's roles has a custom override.
		foreach ( $user_roles as $role ) {
			if ( ! empty( $role_enabled[ $role ] ) ) {
				return isset( $role_hidden[ $role ] ) ? (array) $role_hidden[ $role ] : array();
			}
		}

		// No role override — use the global hidden list.
		return $global;
	}
}
