<?php
/**
 * Template Dashboard
 *
 * @package jobus
 * @author  spider-themes
 */

/**
 * Use namespace to avoid conflict
 */
namespace jobus\includes\Frontend;

use WP_User;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Class Dashboard
 *
 * Handles the candidate dashboard logic, including:
 * - Registering dashboard endpoints
 * - Rendering the dashboard and sidebar
 * - Loading section templates based on the active endpoint
 *
 * @package Jobus\Includes\Frontend
 */
class Dashboard_Candidate {

	/**
	 * Instance of this class.
	 *
	 * @var object
	 */
	private static $instance = null;

	/**
	 * Get instance of this class.
	 *
	 * @return Dashboard_Candidate
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Dashboard constructor.
	 *
	 * Registers shortcode and dashboard endpoints.
	 */
	public function __construct() {
		// Register endpoints for each dashboard section
		add_action( 'init', [ $this, 'register_candidate_dashboard_endpoints' ] );
	}

	/**
	 * Register custom rewrite endpoints for dashboard navigation.
	 *
	 * Adds endpoints for each dashboard section (e.g., profile, resume, etc.).
	 * This enables pretty URLs for each section.
	 */
	public function register_candidate_dashboard_endpoints(): void {
		add_rewrite_endpoint( 'dashboard', EP_PAGES );
		add_rewrite_endpoint( 'profile', EP_PAGES );
		add_rewrite_endpoint( 'resume', EP_PAGES );
		add_rewrite_endpoint( 'applied-jobs', EP_PAGES );
		add_rewrite_endpoint( 'saved-jobs', EP_PAGES );
		add_rewrite_endpoint( 'messages', EP_PAGES );
		add_rewrite_endpoint( 'change-password', EP_PAGES );
	}

	/**
	 * Get dashboard navigation items (single source of truth).
	 *
	 * Returns an array of navigation items for the sidebar menu based on settings.
	 *
	 * @return array Navigation items with label and icon.
	 */
	public static function get_nav_items(): array {
		$nav_items = [];

		// Dashboard
		if ( jobus_opt( 'candidate_menu_dashboard', true ) ) {
			$nav_items['dashboard'] = [
				'label' => jobus_opt( 'label_candidate_dashboard', esc_html__( 'Dashboard', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/dashboard.svg'
			];
		}

		// My Profile
		if ( jobus_opt( 'candidate_menu_profile', true ) ) {
			$nav_items['profile'] = [
				'label' => jobus_opt( 'label_candidate_profile', esc_html__( 'My Profile', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/profile.svg'
			];
		}

		// Resume
		if ( jobus_opt( 'candidate_menu_resume', true ) ) {
			$nav_items['resume'] = [
				'label' => jobus_opt( 'label_candidate_resume', esc_html__( 'Resume', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/resume.svg'
			];
		}

		// Applied Jobs
		if ( jobus_opt( 'candidate_menu_applied_jobs', true ) ) {
			$nav_items['applied-jobs'] = [
				'label' => jobus_opt( 'label_candidate_applied_jobs', esc_html__( 'Applied Jobs', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/applied_job.svg'
			];
		}

		// Saved Jobs
		if ( jobus_opt( 'candidate_menu_saved_jobs', true ) ) {
			$nav_items['saved-jobs'] = [
				'label' => jobus_opt( 'label_candidate_saved_jobs', esc_html__( 'Saved Jobs', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/saved-job.svg'
			];
		}

		// Messages
		if ( jobus_opt( 'candidate_menu_messages', true ) ) {
			$nav_items['messages'] = [
				'label' => jobus_opt( 'label_messages', esc_html__( 'Messages', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/messages.svg'
			];
		}

		// Change Password
		if ( jobus_opt( 'candidate_menu_change_password', true ) ) {
			$nav_items['change-password'] = [
				'label' => jobus_opt( 'label_change_password', esc_html__( 'Change Password', 'jobus' ) ),
				'icon'  => JOBUS_IMG . '/dashboard/icons/password.svg'
			];
		}

		return $nav_items;
	}

	/**
	 * Render the candidate dashboard main layout.
	 *
	 * Handles authentication, role checking, sidebar rendering, and loads the correct section template.
	 *
	 * @return string Dashboard HTML output.
	 */
	public function candidate_dashboard(): string {

		if ( ! is_user_logged_in() ) {
			// Show the login form if user is not logged in
			return Template_Loader::get_template_part( 'dashboard/login-form' );
		}

		$user = wp_get_current_user();
		if ( ! array_intersect( ['jobus_candidate', 'administrator'], (array) $user->roles ) ) {
			return Template_Loader::get_template_part( 'dashboard/logout-form' );
		}

		$nav_items = self::get_nav_items();

		$active = 'dashboard';
		foreach ( $nav_items as $endpoint => $item ) {
			if ( isset( $GLOBALS['wp_query']->query_vars[ $endpoint ] ) ) {
				$active = $endpoint;
				break;
			}
		}

		ob_start();

		echo '<div class="dashboard-wrapper">';
		echo '<aside class="dashboard-navbar">';
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
		echo $this->load_sidebar_menu($active, $nav_items);
		echo '</aside>';
		echo '<main class="dashboard-body">';

		switch ( $active ) {
			case 'profile':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_profile( $user );
				break;
			case 'resume':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_resume( $user );
				break;
			case 'applied-jobs':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_candidate_job_applied( $user );
				break;
			case 'saved-jobs':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_candidate_saved_job( $user );
				break;
			case 'messages':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_messages( $user );
				break;
			case 'change-password':
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_change_password( $user );
				break;
			default:
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in template
				echo $this->load_candidate_dashboard( $user );
				break;
		}

		echo '</main>';
		echo '</div>';

		return ob_get_clean();
	}

	/**
	 * Load the candidate dashboard template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Candidate dashboard HTML.
	 */
	private function load_candidate_dashboard( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/candidate/dashboard', [
			'user_id'  => $user->ID,
			'username' => $user->user_login,
		] );
	}

	/**
	 * Load the change password section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Change password section HTML.
	 */
	private function load_change_password( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/global/change-password', [
			'user_id'  => $user->ID,
			'username' => $user->user_login,
		] );
	}

	/**
	 * Load the saved jobs section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Saved jobs section HTML.
	 */
	private function load_candidate_saved_job( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/candidate/saved-job', [
			'user_id'      => $user->ID,
			'username'     => $user->user_login,
			'is_dashboard' => false
		] );
	}

	/**
	 * Load the applied jobs section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Applied jobs section HTML.
	 */
	private function load_candidate_job_applied( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/candidate/job-applied', [
			'user_id'      => $user->ID,
			'username'     => $user->user_login,
			'is_dashboard' => false
		] );
	}

	/**
	 * Load the resume section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Resume section HTML.
	 */
	private function load_resume( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/candidate/resume', [
			'user_id'  => $user->ID,
			'username' => $user->user_login,
		] );
	}

	/**
	 * Load the profile section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Profile section HTML.
	 */
	private function load_profile( WP_User $user ): string {
		return Template_Loader::get_template_part( 'dashboard/candidate/profile', [
			'user_id'  => $user->ID,
			'username' => $user->user_login,
		] );
	}

	/**
	 * Load the messages section template.
	 *
	 * @param WP_User $user The current user.
	 *
	 * @return string Messages section HTML.
	 */
	private function load_messages( WP_User $user ): string {
		if (jobus_is_premium()) {
			return Template_Loader::get_template_part_pro('dashboard/global/messages', [
				'user_id'  => $user->ID,
				'username' => $user->user_login,
			]);
		} else {
			$image_url = JOBUS_IMG . '/dashboard/pro-features/messages.png';
			ob_start();
		?>
			<div class="jbs-dashboard-pro-notice" role="button" tabindex="0" aria-label="<?php esc_attr_e('Pro Feature - Upgrade required', 'jobus'); ?>">
				<div class="pro-image-wrap">
					<img src="<?php echo esc_url($image_url); ?>" alt="<?php esc_attr_e('Pro Feature', 'jobus'); ?>" />
					<span class="pro-badge" aria-hidden="true"><?php esc_html_e('Pro', 'jobus'); ?></span>
				</div>
			</div>
		<?php
			return ob_get_clean();
		}
	}

	/**
	 * Load the sidebar menu with active endpoint and navigation items.
	 *
	 * Passes the active endpoint and menu items to the sidebar template for rendering.
	 *
	 * @param string $active    Active endpoint slug.
	 * @param array  $nav_items Optional navigation items to override defaults.
	 *
	 * @return string Rendered sidebar menu HTML.
	 */
	private function load_sidebar_menu( string $active, array $nav_items = [] ): string {
		return Template_Loader::get_template_part( 'dashboard/global/sidebar-menu', [
			'active_endpoint' => $active,
			'menu_items' => $nav_items ?: self::get_nav_items(),
		] );
	}
}
