<?php

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

$profile_id   = $args['profile_id'] ?? '';
$profile_type = $args['profile_type'] ?? '';

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

$user_id            = gigneo_post_meta( $profile_id, '_gigneo_pm_user_id' );
$profile_type       = gigneo_profile_type( $profile_id );
$name               = gigneo_profile_name( $profile_id );
$cover_image_id     = gigneo_post_meta( $profile_id, '_gigneo_pm_cover_image' );
$thumbnail_id       = get_post_thumbnail_id( $profile_id );
$online             = gigneo_online_status( $profile_id );
$qualification      = gigneo_post_meta( $profile_id, '_gigneo_pm_qualification' );
$country_state_raw  = gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' );
$country_state_list = gigneo_countries_with_states( 'active' );

// Stats
$total_spent      = gigneo_profile_stat( 'total_spending', $profile_id );
$total_earning    = gigneo_profile_stat( 'total_earning', $profile_id );
$completed_orders = gigneo_posts_status_count( 'gigneo_order', $profile_id );
$active_orders    = gigneo_profile_stat( 'active_orders_count', $profile_id );
$success_rate     = gigneo_profile_stat( 'success_rate', $profile_id );

$genders        = gigneo_genders( false );
$qualifications = gigneo_qualifications( false );
?>
<div class="gn-profile-media-wrapper">
	<!--Cover image-->
	<div class="gn-profile-cover">
		<figure class="gn-cover">
			<?php gigneo_attachment_image_src( $cover_image_id, 'full', 'cover' ); ?>
		</figure>
	</div>
	<!--Profile image-->
	<div class="gn-profile-img">
		<figure class="gn-avatar gn-<?php echo esc_attr( $online ? 'online' : 'offline' ); ?>">
			<?php gigneo_attachment_image_src( $thumbnail_id, 'thumbnail', 'profile', '', '', $profile_id ); ?>
		</figure>
	</div>
	<!--Profile info-->
	<div class="gn-profile-info">
		<h3 class="gn-name"><?php echo esc_html( $name ); ?></h3>
		<ul class="gn-meta">
			<li class="gn-rating">
				<i class="hi hi-star-alt gn-filled"></i>
				<?php echo esc_html( gigneo_profile_rating( $profile_id, 'average' ) ); ?>
				<span class="gn-count">(<?php echo esc_html( gigneo_profile_rating( $profile_id ) ); ?>)</span>
			</li>
			<?php
			//Qualification
			if ( ! empty( $qualifications[ $qualification ] ) ) {
				?>
				<li class="gn-qualification">
					<i class="hi hi-academic-cap"></i>
					<?php echo esc_html( $qualifications[ $qualification ] ); ?>
				</li>
				<?php
			}
			//Location
			if ( ! empty( $country_state_list[ $country_state_raw ] ) ) {
				?>
				<li class="gn-location">
					<i class="hi hi-map-pin"></i>
					<?php echo esc_html( $country_state_list[ $country_state_raw ] ); ?>
				</li>
				<?php
			}
			?>
		</ul>
	</div>
	<!-- Stats -->
	<div class="gn-stat-cards">
		<div class="gn-stat-card">
			<?php if ( 'freelancer' === $profile_type ) { ?>
						<div class="gn-head">
							<i class="hi hi-banknotes"></i>
							<span class="gn-label"><?php echo esc_html__( 'Total earnings', 'gigneo' ); ?></span>
						</div>
						<h5 class="gn-value">
							<?php echo esc_html( gigneo_price_format( $total_earning['lifetime'] ?? 0, true ) ); ?>
						</h5>
					<?php } else { ?>
						<div class="gn-head">
							<i class="hi hi-banknotes"></i>
							<span class="gn-label"><?php echo esc_html__( 'Total spent', 'gigneo' ); ?></span>
						</div>
						<h5 class="gn-value">
							<?php echo esc_html( gigneo_price_format( $total_spent['lifetime'] ?? 0, true ) ); ?>
						</h5>
					<?php } ?>
		</div>
		<div class="gn-stat-card">
			<div class="gn-head">
				<i class="hi hi-briefcase"></i>
				<span class="gn-label"><?php echo esc_html__( 'Active orders', 'gigneo' ); ?></span>
			</div>
			<h5 class="gn-value"><?php echo esc_html( $active_orders['lifetime'] ?? 0 ); ?></h5>
		</div>
		<div class="gn-stat-card">
			<div class="gn-head">
				<i class="hi hi-check-badge"></i>
				<span class="gn-label"><?php echo esc_html__( 'Completed orders', 'gigneo' ); ?></span>
			</div>
			<h5 class="gn-value"><?php echo esc_html( $completed_orders['completed'] ?? 0 ); ?></h5>
		</div>
		<div class="gn-stat-card">
			<div class="gn-head">
				<i class="hi hi-trophy"></i>
				<span class="gn-label"><?php echo esc_html__( 'Success rate', 'gigneo' ); ?></span>
			</div>
			<h5 class="gn-value"><?php echo esc_html( $success_rate['lifetime'] ?? 0 ); ?>%</h5>
		</div>
	</div>
</div>
