<?php

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

use Gigneo\Notifications;

global $gigneo_args;

$profile_id    = gigneo_active_profile_id();
$profile_type  = gigneo_profile_type( $profile_id );
$credit_system = gigneo_has_credit_system( $profile_type );
$platform_type = gigneo_platform_type();

$source = ! empty( $gigneo_args['source'] ) ? $gigneo_args['source'] : 'account';
$ref    = ! empty( $gigneo_args['ref'] ) ? $gigneo_args['ref'] : '';

$menu_layout = gigneo_option( '_gigneo_ts_dashboard_menu_layout', 'side' );

$sections = array(
	'account'      => array(
		'title'          => esc_html__( 'Dashboard', 'gigneo' ),
		'description'    => esc_html__( 'Stay organised and manage your account here.', 'gigneo' ),
		'tabs'           => array(
			'overview' => esc_html__( 'Overview', 'gigneo' ),
			'earning'  => esc_html__( 'Earnings', 'gigneo' ),
			'spending' => esc_html__( 'Spending', 'gigneo' ),
			'wallet'   => esc_html__( 'Wallet', 'gigneo' ),
			'credits'  => esc_html__( 'Credits', 'gigneo' ),
		),
		'active_default' => 'overview',
	),
	'notification' => array(
		'title'       => esc_html__( 'Notifications', 'gigneo' ),
		'description' => esc_html__( 'Notifications related to your account activities', 'gigneo' ),
		'tabs'        => array(),
	),
	'service'      => array(
		'title'       => esc_html__( 'Services', 'gigneo' ),
		'description' => esc_html__( 'Create and manage your service offerings.', 'gigneo' ),
		'tabs'        => array(),
	),
	'job'          => array(
		'title'       => esc_html__( 'Jobs', 'gigneo' ),
		'description' => esc_html__( 'Create and manage your job offerings.', 'gigneo' ),
		'tabs'        => array(),
	),
	'order'        => array(
		'title'       => esc_html__( 'Orders', 'gigneo' ),
		'description' => esc_html__( 'Manage your orders and track your earnings.', 'gigneo' ),
		'class'       => 'gn-order-top-area',
		'tabs'        => array(),

	),
	'setting'      => array(
		'title'          => esc_html__( 'Settings', 'gigneo' ),
		'description'    => esc_html__( 'Manage account, privacy, and system settings', 'gigneo' ),
		'tabs'           => array(
			'profile'  => esc_html__( 'Profile', 'gigneo' ),
			'account'  => esc_html__( 'Account', 'gigneo' ),
			'billing'  => esc_html__( 'Billing', 'gigneo' ),
			'security' => esc_html__( 'Security', 'gigneo' ),
		),
		'active_default' => 'profile',
	),
	'proposal'     => array(
		'title'       => esc_html__( 'Proposals', 'gigneo' ),
		'description' => esc_html__( 'Track and manage all your job applications', 'gigneo' ),
		'tabs'        => array(),
	),
	'saved'        => array(
		'title'       => esc_html__( 'Saved Items', 'gigneo' ),
		'description' => esc_html__( 'Manage your bookmarked items and profiles.', 'gigneo' ),
		'tabs'        => array(),
	),
);

$sections = apply_filters( 'gigneo_dashboard_top_area_sections', $sections );

// Get current section config
$section_config = $sections[ $source ] ?? array();

if ( empty( $section_config ) ) {
	return;
}

// Filter tabs based on user type & settings
$tab_menus = $section_config['tabs'] ?? array();

if ( 'account' === $source ) {
	if ( ! $credit_system ) {
		unset( $tab_menus['credits'] );
	}
	if ( 'employer' === $profile_type ) {
		unset( $tab_menus['earning'] );
	}
	if ( 'freelancer' === $profile_type ) {
		unset( $tab_menus['spending'] );
	}
}

// Determine active tab
$active_tab = ! empty( $ref ) ? $ref : ( $section_config['active_default'] ?? key( $tab_menus ) );

$unread_notifications = Notifications::get_unread_notification_count();
?>
<div class="gn-dashboard-top-area">
	<div class="container">
		<!--Header Info-->
		<div class="gn-heading-area">
			<h1 class="gn-title"><?php echo esc_html( $section_config['title'] ); ?></h1>
			<p class="gn-description"><?php echo esc_html( $section_config['description'] ); ?></p>
		</div>
		<?php if ( ! empty( $tab_menus ) ) { ?>
			<!--Tab Menu-->
			<div class="gn-dashboard-tab-menu-wrapper">
				<div class="gn-dashboard-tab-menu">
					<?php
					foreach ( $tab_menus as $tab_key => $tab_label ) :
						$tab_class = 'gn-tab-item gn-' . esc_attr( $tab_key );
						if ( $tab_key === $active_tab ) {
							$tab_class .= ' gn-active';
						}
						?>
						<a href="<?php echo esc_url( gigneo_dashboard_url( $source, $tab_key ) ); ?>"
							class="<?php echo esc_attr( $tab_class ); ?>">
							<?php echo esc_html( $tab_label ); ?>
						</a>
					<?php endforeach; ?>
				</div>
			</div>
		<?php } ?>
		<!--Actions-->
		<div class="gn-action-<?php echo esc_attr( 'side' === $menu_layout ? 'menu' : 'button' ); ?>-wrapper">
			<?php if ( 'side' === $menu_layout ) { ?>
				<div class="gn-responsive-menu-toggle">
					<div class="gn-icon">
						<i class="hi hi-bars-3"></i>
					</div>
				</div>
				<div class="gn-user-notifications">
					<?php do_action( 'gigneo_before_user_notification' ); ?>
					<div class="gn-user-notification">
						<div class="gn-icon">
							<i class="hi hi-bell-alert"></i>
							<span class="gn-count"><?php echo ! empty( $unread_notifications ) ? esc_html( $unread_notifications ) : ''; ?></span>
						</div>
						<?php gigneo_load_template( 'dropdown/notification' ); ?>
					</div>
					<?php do_action( 'gigneo_after_user_notification' ); ?>
				</div>
				<?php
			} elseif ( 'account' === $source ) {
				?>
					<button class="gn-btn gn-btn-outline-grey gn-btn-sm" type="button" data-gn-modal-toggle="gn-withdraw-funds">
						<?php echo esc_html__( 'Withdraw Funds', 'gigneo' ); ?>
						<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
					</button>
					<button class="gn-btn gn-btn-primary gn-btn-sm" type="button" data-gn-modal-toggle="gn-deposit-funds">
						<?php echo esc_html__( 'Add Funds', 'gigneo' ); ?>
						<i class="hi hi-plus" aria-hidden="true"></i>
					</button>
					<?php
			}
			?>
		</div>
	</div>
</div>
