<?php
/**
 * Dashboard Orchestrator.
 *
 * On `wp_dashboard_setup`, for users holding any Helm persona, registers the
 * widget sets for EVERY persona they hold. A multi-role user (e.g. a manager
 * who is also the community manager) sees both sets — multi-role is a
 * first-class case. Users with no Helm persona keep the native dashboard
 * untouched.
 *
 * We add widgets; we do not strip the native dashboard. That's deliberate for a
 * public product — silently removing other plugins' widgets is hostile. Helm's
 * widgets simply appear alongside whatever else is there.
 *
 * @package Platform\Helm\Modules\Persona_Dashboards
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Persona_Dashboards;

use Platform\Helm\Modules\Persona_Dashboards\Widgets\AuthorWidgets;
use Platform\Helm\Modules\Persona_Dashboards\Widgets\CommunityWidgets;
use Platform\Helm\Modules\Persona_Dashboards\Widgets\EditorWidgets;
use Platform\Helm\Modules\Persona_Dashboards\Widgets\ManagerWidgets;

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

final class DashboardOrchestrator {

	private DataProviders $data;
	private ManagerWidgets $manager;
	private EditorWidgets $editor;
	private AuthorWidgets $author;
	private CommunityWidgets $community;

	public function __construct( private PersonaDetector $detector ) {
		$this->data      = new DataProviders();
		$this->manager   = new ManagerWidgets( $this->data );
		$this->editor    = new EditorWidgets( $this->data );
		$this->author    = new AuthorWidgets( $this->data );
		$this->community = new CommunityWidgets( $this->data );
	}

	public function register_hooks(): void {
		add_action( 'wp_dashboard_setup', [ $this, 'on_dashboard_setup' ], 20 );
	}

	public function on_dashboard_setup(): void {
		if ( ! $this->detector->should_customise() ) {
			return;
		}
		$personas = $this->detector->all();

		// Optional clean-dashboard mode: strip the noisy native widgets for
		// Helm-persona users. Opt-in; off by default to preserve the additive
		// policy. Only touches WordPress core widgets we know are commonly
		// considered clutter — never third-party plugin widgets, which would
		// be hostile in a public product.
		if ( $this->is_clean_mode() ) {
			$core_noise = [
				'dashboard_activity'        => 'normal',
				'dashboard_right_now'       => 'normal',
				'dashboard_quick_press'     => 'side',
				'dashboard_primary'         => 'side',   // WordPress Events and News.
				'dashboard_recent_drafts'   => 'side',
				'dashboard_recent_comments' => 'normal',
				'dashboard_site_health'     => 'normal',
			];
			foreach ( $core_noise as $widget => $context ) {
				remove_meta_box( $widget, 'dashboard', $context );
			}
			// Also hide the welcome panel for this user.
			remove_action( 'welcome_panel', 'wp_welcome_panel' );
			update_user_meta( $this->detector->current_user_id(), 'show_welcome_panel', 0 );
		}

		// A small multi-role banner widget when more than one persona applies.
		if ( count( $personas ) > 1 ) {
			$this->add_widget( 'phelm_multirole', __( 'Your Helm Dashboard', 'platform-helm' ), function () use ( $personas ) {
				echo Renderer::multi_role_note( $personas ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo '<p class="phelm-pd-empty">' . esc_html__( 'Widgets below are tailored to each role you hold.', 'platform-helm' ) . '</p>';
			} );
		}

		if ( in_array( PersonaDashboardsModule::PERSONA_MANAGER, $personas, true ) ) {
			$this->add_widget( 'phelm_mgr_pulse', __( 'Helm · Platform Pulse', 'platform-helm' ), [ $this->manager, 'render_pulse' ] );
			$this->add_widget( 'phelm_mgr_recent', __( 'Helm · Recent Content', 'platform-helm' ), [ $this->manager, 'render_recent_content' ] );
		}
		if ( in_array( PersonaDashboardsModule::PERSONA_EDITOR, $personas, true ) ) {
			$this->add_widget( 'phelm_editor_pipeline', __( 'Helm · Editorial Pipeline', 'platform-helm' ), [ $this->editor, 'render_pipeline' ] );
		}
		if ( in_array( PersonaDashboardsModule::PERSONA_COMMUNITY, $personas, true ) ) {
			$this->add_widget( 'phelm_comm_pulse', __( 'Helm · Community Pulse', 'platform-helm' ), [ $this->community, 'render_pulse' ] );
		}
		if ( in_array( PersonaDashboardsModule::PERSONA_AUTHOR, $personas, true ) ) {
			$this->add_widget( 'phelm_author_content', __( 'Helm · My Content', 'platform-helm' ), [ $this->author, 'render_my_content' ] );
		}
	}

	private function add_widget( string $id, string $title, callable $cb ): void {
		wp_add_dashboard_widget( $id, $title, $cb );
	}

	/**
	 * Whether clean-dashboard mode is enabled in settings. Read directly via
	 * get_option so we don't need to plumb the Settings service in here.
	 */
	private function is_clean_mode(): bool {
		$opts = get_option( 'platform_helm_settings', [] );
		return is_array( $opts ) && ! empty( $opts['pd_clean_mode'] );
	}
}
