<?php

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

use Gigneo\Modules;
use Gigneo\Post_Types;

// Get current user details.
global $current_user;

if ( ! $current_user ) {
	wp_safe_redirect( home_url() );
	exit;
}

// Verify user access and redirect if unauthorized.
if ( ! gigneo_user_have_access() ) {
	wp_safe_redirect( home_url() );
	exit;
}

// Retrieve profile and platform details.
$profile_id = gigneo_active_profile_id();

if ( ! $profile_id ) {
	wp_safe_redirect( home_url() );
	exit();
}

$profile_type     = gigneo_profile_type( $profile_id );
$platform_type    = gigneo_platform_type();
$dashboard_access = true;

// Fetch dashboard layout settings.
$menu_layout      = gigneo_option( '_gigneo_ts_dashboard_menu_layout', 'side' );
$layout_style     = gigneo_option( '_gigneo_ts_dashboard_menu_style', '1' );
$menu_scheme      = gigneo_option( '_gigneo_ts_dashboard_menu_scheme', 'default' );
$layout_color     = gigneo_option( '_gigneo_ts_dashboard_layout_background', '' );
$layout_behaviour = gigneo_option( '_gigneo_ts_dashboard_sidebar_behaviour', 'expand' );

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only route params; source/ref validated against switch allowlist below; p_id authorized in child templates.
$source = ! empty( $_GET['source'] ) ? sanitize_text_field( wp_unslash( $_GET['source'] ) ) : '';
$ref    = ! empty( $_GET['ref'] ) ? sanitize_text_field( wp_unslash( $_GET['ref'] ) ) : '';
$p_id   = ! empty( $_GET['p_id'] ) ? absint( $_GET['p_id'] ) : 0;
// phpcs:enable WordPress.Security.NonceVerification.Recommended -- Read-only route params; source/ref validated against switch allowlist below; p_id authorized in child templates.

$gigneo_args = array(
	'source' => $source,
	'ref'    => $ref,
	'p_id'   => $p_id,
);

if ( isset( $_COOKIE['gigneo_sidebar_collapse'] ) ) {
	$layout_behaviour = 'true' === $_COOKIE['gigneo_sidebar_collapse'] ? 'collapse' : 'expand';
}

$scheme = 'default';
$scheme = 'default' !== $menu_scheme ? ( 'light' === $menu_scheme ? 'dark' : 'light' ) : $scheme;

// Build dashboard wrapper classes.
$dashboard_classes = array(
	'gn-dashboard-wrapper',
	"gn-dashboard-layout-{$layout_style}",
	"gn-dashboard-{$menu_layout}-menu",
	"gn-dashboard-scheme-{$scheme}",
	'side' === $menu_layout && 'collapse' === $layout_behaviour ? 'gn-collapsed' : '',
	! empty( $source ) ? "gn-{$source}" . ( ! empty( $ref ) ? "-{$ref}" : '' ) : '',
);

// Remove empty classes and join.
$dashboard_class = implode( ' ', array_filter( $dashboard_classes ) );

// Get post-type slugs.
$post_types_slug = Post_Types::post_type_slug();
// Load dashboard header.
gigneo_load_template( 'dashboard/header' );

//Account Status Check
$post_status = get_post_status( $profile_id );
if ( 'deactivated' === $post_status || 'closed' === $post_status || 'blocked' === $post_status ) {
	gigneo_load_template( 'dashboard/error', true, array( 'type' => $post_status ) );
	$dashboard_access = false;
}

if ( $dashboard_access ) {
	$credit_system = gigneo_has_credit_system();
	?>
<div class="<?php echo esc_attr( $dashboard_class ); ?>">
	<?php
	if ( $dashboard_access ) {
		// Load dashboard menu based on layout.
		gigneo_load_template( "dashboard/menu/{$menu_layout}-menu" );
		?>
		<div class="gn-dashboard-content-wrapper">
			<?php
			// Load top area template.
			gigneo_load_template( 'dashboard/top-area' );
			?>

			<div class="gn-dashboard-content-area">
				<div class="container">
					<?php

					// Initialize template loading flag.
					$template_loaded = false;

					// Default template for no source or ref.
					if ( empty( $source ) && empty( $ref ) ) {

						// Load notices.
						gigneo_load_template( 'dashboard/notice' );

						gigneo_load_template( 'dashboard/account/overview' );
						$template_loaded = true;
					} else {
						// Define available templates based on source.
						$templates = array();
						switch ( $source ) {
							case 'account':
								$templates = array(
									'overview' => 'dashboard/account/overview',
									'wallet'   => 'dashboard/account/wallet',
								);
								if ( 'freelancer' === $profile_type ) {
									$templates['earning'] = 'dashboard/account/earning';
								} else {
									$templates['spending'] = 'dashboard/account/spending';
								}
								if ( $credit_system ) {
									$templates['credits'] = 'dashboard/account/credit';
								}
								break;

							case 'service':
							case $post_types_slug['gigneo_service']:
								if ( 'freelancer' === $profile_type && 'jobs' !== $platform_type ) {
									$templates = array(
										'list' => 'dashboard/service/list',
										'edit' => 'dashboard/service/edit',
									);
								}
								break;

							case 'job':
							case $post_types_slug['gigneo_job']:
								if ( 'services' !== $platform_type ) {
									$templates = array(
										'list'     => 'dashboard/job/list',
										'edit'     => 'dashboard/job/edit',
										'proposal' => 'dashboard/job/proposal',
									);
								}
								break;
							case 'proposal':
								$templates = array(
									'list'   => 'dashboard/proposal/list',
									'detail' => 'dashboard/proposal/detail',
								);
								break;
							case 'order':
								$service_slug = $post_types_slug['gigneo_service'];
								$job_slug     = $post_types_slug['gigneo_job'];
								$templates    = array(
									"$service_slug-list"   => 'dashboard/order/service/list',
									"$service_slug-detail" => 'dashboard/order/service/detail',
									"$job_slug-list"       => 'dashboard/order/job/list',
									"$job_slug-detail"     => 'dashboard/order/job/detail',
								);
								break;
							case 'notification':
								$templates = array(
									'all'    => 'dashboard/notification',
									'read'   => 'dashboard/notification',
									'unread' => 'dashboard/notification',
								);
								break;

							case 'message':
								if ( in_array( 'live_chat' , gigneo_active_modules(), true  ) ) {
									$templates = array(
										'list' => 'dashboard/message/list',
									);
								}
								break;

							case 'setting':
								$templates = array(
									'account'  => 'dashboard/setting/account',
									'profile'  => 'dashboard/setting/profile',
									'billing'  => 'dashboard/setting/billing',
									'security' => 'dashboard/setting/security',
								);
								break;

							case 'support':
								$templates = apply_filters( 'gigneo_dashboard_general_support_templates', $templates );
								break;

							case 'saved':
								$templates = array(
									'list' => 'dashboard/saved/list',
								);
								break;
						}

						// Load the matching template if it exists.
						if ( ! empty( $templates ) && isset( $templates[ $ref ] ) ) {

							// Load notices.
							gigneo_load_template( 'dashboard/notice' );

							//Load the template.
							gigneo_load_template( $templates[ $ref ] );
							$template_loaded = true;
						}
					}

					// Fallback to not-found template if no valid template is loaded.
					if ( ! $template_loaded ) {
						gigneo_not_found( 'general' );
					}
					?>
				</div>
			</div>
		</div>
		<?php
	}
	?>
</div>
	<?php
}
// Load dashboard footer.
gigneo_load_template( 'dashboard/footer' );
