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

use Gigneo\Modules;

$proposal_id       = $args['p_id'] ?? '';
$author_id         = $args['author_id'] ?? get_post_field( 'post_author', $proposal_id );
$job_id            = $args['job_id'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_job_id' );
$order_id          = $args['order_id'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_order_id' );
$price             = $args['price'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_budget' );
$duration          = $args['duration'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_duration' );
$duration_type     = $args['duration_type'] ?? gigneo_post_meta( $proposal_id, '_gigneo_pm_duration_type' );
$author_profile_id = gigneo_profile_id( $author_id, 'freelancer' );
$redirect_url      = gigneo_dashboard_url( 'order', 'job-detail', array( 'p_id' => $order_id ) );

$date_format      = get_option( 'date_format' );
$time_format      = get_option( 'time_format' );
$date_time_format = $date_format . ' ' . $time_format;

$posted_time           = get_post_time( 'Y-m-d H:i:s', true, $proposal_id );
$time_diff             = gigneo_datetime( $posted_time, $date_time_format );
$user_profile_id       = gigneo_active_profile_id();
$profile_type          = gigneo_profile_type();
$job_author_id         = get_post_field( 'post_author', $job_id );
$job_author_profile_id = gigneo_profile_id( $job_author_id, 'employer' );

$job_status      = get_post_status( $job_id );
$proposal_status = $args['proposal_status'] ?? get_post_status( $proposal_id );

$participant_profile_id   = 'freelancer' === $profile_type ? $job_author_profile_id : $author_profile_id;
$profile_singular         = 'freelancer' === $profile_type ? gigneo_employer_singular() : gigneo_freelancer_singular();
$tagline_raw              = gigneo_post_meta( $participant_profile_id, '_gigneo_pm_tagline' );
$participant_profile_type = gigneo_profile_type( $participant_profile_id );
$tagline_raw              = ! empty( $tagline_raw ) ? $tagline_raw : $profile_singular;
$author_avatar_id         = get_post_thumbnail_id( $participant_profile_id ) ?? 0;
$online_status            = gigneo_online_status( $participant_profile_id );
$country_state_list       = gigneo_countries_with_states( 'active' );
$country_state_raw        = gigneo_post_meta( $participant_profile_id, '_gigneo_pm_country_state' );
$country_label            = ! empty( $country_state_list[ $country_state_raw ] ) ? $country_state_list[ $country_state_raw ] : '—';
$orders_data              = gigneo_profile_stat( 'completed_orders_count', $participant_profile_id );
$earnings_data            = gigneo_profile_stat( 'total_earning', $participant_profile_id );
$spending_data            = gigneo_profile_stat( 'total_spending', $participant_profile_id );

$pref_location        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_location' );
$pref_timezone        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_timezone' );
$pref_language        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_language' );
$pref_language        = ! empty( $pref_language ) ? get_term( $pref_language, 'gigneo_language' ) : '';
$language_proficiency = gigneo_post_meta( $job_id, '_gigneo_pm_language_proficiency' );

$pref_country  = is_array( $pref_location ) ? ( $pref_location[0] ?? '' ) : $pref_location;
$countries     = gigneo_allowed_countries();
$pref_location = $countries[ $pref_country ] ?? $pref_country;

?>
<div class="gn-section gn-proposal-info">
	<?php if ( ! empty( $pref_location ) || ! empty( $pref_timezone ) || ! empty( $pref_language ) ) { ?>
		<div class="gn-sub-section gn-preferences">
			<div class="gn-head">
				<h2 class="gn-title"><?php echo esc_html__( 'Preferences', 'gigneo' ); ?></h2>
			</div>
			<div class="gn-body">
				<ul class="gn-preferences">
					<?php if ( ! empty( $pref_language ) ) { ?>
						<li class="gn-info gn-language">
							<span class="gn-title"><?php echo esc_html__( 'Language', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $profile_type ) {
								$active_language = 'hi-exclamation-circle';
								$active_class    = 'gn-value-exclamation';
								if ( has_term( $pref_language->term_id, 'gigneo_language', $author_profile_id ) ) {
									$active_language = 'hi-check-circle';
									$active_class    = '';
									if ( ! empty( $language_proficiency ) ) {
										$profile_languages = gigneo_post_meta( $author_profile_id, '_gigneo_pm_languages' );
										if ( ! empty( $profile_languages ) && is_array( $profile_languages ) ) {
											foreach ( $profile_languages as $lang ) {
												if ( is_array( $lang ) && isset( $lang['name'] ) && strtolower( $lang['name'] ) === strtolower( $pref_language->name ) && isset( $lang['level'] ) && strtolower( $lang['level'] ) === strtolower( $language_proficiency ) ) {
													$active_language = 'hi-check-circle';
													$active_class    = '';
													break;
												} else {
													$active_language = 'hi-exclamation-circle';
													$active_class    = 'gn-value-exclamation';
												}
											}
										}
									}
								}
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php
								echo esc_html( $pref_language->name ?? '' );
								if ( ! empty( $language_proficiency ) ) {
									echo ' (' . esc_html( $language_proficiency ) . ')';
								}
								?>
								<?php if ( 'freelancer' === $profile_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_language ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>
					<?php if ( ! empty( $pref_timezone ) && in_array( 'timezone', gigneo_active_modules(), true ) ) { ?>
						<li class="gn-info gn-timezone">
							<span class="gn-title"><?php echo esc_html__( 'Timezone', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $profile_type ) {
								$profile_timezone = gigneo_timezone( $author_profile_id );
								$active_timezone  = $profile_timezone === $pref_timezone ? 'hi-check-circle' : 'hi-exclamation-circle';
								$active_class     = $profile_timezone === $pref_timezone ? '' : 'gn-value-exclamation';
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php echo esc_html( $pref_timezone ?? '' ); ?>
								<?php if ( 'freelancer' === $profile_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_timezone ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>
					<?php if ( ! empty( $pref_location ) ) { ?>
						<li class="gn-info gn-location">
							<span class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></span>
							<?php
							$active_class = '';
							if ( 'freelancer' === $profile_type ) {
								$profile_location = gigneo_post_meta( $author_profile_id, '_gigneo_pm_country_state' );
								$country_state    = ! empty( $profile_location ) ? explode( ':', (string) $profile_location, 2 ) : array();
								$country_state    = ! empty( $country_state[0] ) ? $country_state[0] : '';
								$active_location  = ! empty( $pref_country ) && $country_state === $pref_country ? 'hi-check-circle' : 'hi-exclamation-circle';
								$active_class     = ! empty( $pref_country ) && $country_state === $pref_country ? '' : 'gn-value-exclamation';
							}
							?>
							<span class="gn-value <?php echo esc_attr( $active_class ); ?>">
								<?php echo esc_html( $pref_location ?? '' ); ?>
								<?php if ( 'freelancer' === $profile_type ) { ?>
									<i class="hi <?php echo esc_attr( $active_location ); ?>" aria-hidden="true"></i>
								<?php } ?>
							</span>
						</li>
					<?php } ?>

				</ul>
			</div>
		</div>
	<?php } ?>
	<div class="gn-sub-section gn-activity">
		<div class="gn-head">
			<h3 class="gn-title"><?php echo esc_html__( 'Proposal overview', 'gigneo' ); ?></h3>
		</div>
		<div class="gn-body">
			<ul class="gn-additional-info">
				<li>
					<span class="gn-label"><?php echo esc_html__( 'Bid amount', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( gigneo_price_format( $price, true ) ); ?></span>
				</li>
				<li>
					<span class="gn-label"><?php echo esc_html__( 'Estimated timeline', 'gigneo' ); ?></span>
					<span
							class="gn-value"><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></span>
				</li>
				<li>
					<span class="gn-label"><?php echo esc_html__( 'Submitted at', 'gigneo' ); ?></span>
					<span class="gn-value"><?php echo esc_html( $time_diff ); ?></span>
				</li>
			</ul>
		</div>
	</div>
</div>

<div class="gn-section gn-proposal-user-info">
	<div class="gn-body">
		<div class="gn-profile">
			<figure class="gn-avatar gn-<?php echo esc_attr( $online_status ? 'online' : 'offline' ); ?>">
				<?php gigneo_attachment_image_src( $author_avatar_id, 'thumbnail', 'profile', '', '', $participant_profile_id ); ?>
			</figure>
			<div class="gn-info">
				<div class="gn-name-wrapper">
					<a href="<?php echo esc_url( get_the_permalink( $participant_profile_id ) ); ?>" target="_blank"
						class="gn-name"><?php echo esc_html( gigneo_profile_name( $participant_profile_id ) ); ?></a>
					<?php gigneo_verification_badge( $participant_profile_id, true ); ?>
				</div>
				<?php if ( ! empty( $tagline_raw ) ) { ?>
					<p class="gn-tagline"><?php echo esc_html( $tagline_raw ); ?></p>
					<?php
				}
				?>
			</div>
		</div>
		<ul class="gn-additional-info">
			<li class="gn-ratings">
				<span class="gn-label"><?php echo esc_html__( 'Rating', 'gigneo' ); ?></span>
				<span class="gn-value">
					<i class="hi hi-star-alt" aria-hidden="true"></i>
					<?php echo esc_html( gigneo_profile_rating( $participant_profile_id, 'average' ) ); ?>
					<span
						class="gn-count">(<?php echo esc_html( gigneo_profile_rating( $participant_profile_id ) ); ?>)</span>
				</span>
			</li>
			<li>
				<span class="gn-label">
					<?php
					echo esc_html(
						'freelancer' === $participant_profile_type
						? __( 'Jobs completed', 'gigneo' )
						: __( 'Jobs posted', 'gigneo' )
					);
					?>
				</span>
				<strong class="gn-value"><?php echo esc_html( intval( $orders_data['lifetime'] ) ); ?></strong>
			</li>
			<li>
				<span class="gn-label">
					<?php
					echo esc_html(
						'freelancer' === $participant_profile_type
						? __( 'Total earned', 'gigneo' )
						: __( 'Total spent', 'gigneo' )
					);
					?>
				</span>
				<strong class="gn-value">
					<?php
					if ( 'freelancer' === $participant_profile_type ) {
						echo esc_html( gigneo_price_format( $earnings_data['lifetime'], true ) );
					} else {
						echo esc_html( gigneo_price_format( $spending_data['lifetime'], true ) );
					}
					?>
				</strong>
			</li>
			<li>
				<span class="gn-label"><?php echo esc_html__( 'Location', 'gigneo' ); ?></span>
				<strong class="gn-value"><?php echo esc_html( $country_label ); ?></strong>
			</li>
		</ul>
		<?php if ( 'employer' === $profile_type ) { ?>
			<div class="gn-actions">
				<?php
				do_action(
					'gigneo_action_buttons',
					array(
						'member_id'  => $author_profile_id,
						'profile_id' => $user_profile_id,
						'context'    => 'proposal',
					)
				);
				?>
				<?php
				if ( $job_author_profile_id === $user_profile_id && ! in_array( $proposal_status, array( 'hired', 'completed', 'cancelled' ), true ) && 'publish' === $job_status ) {
					?>
					<button type="button" class="gn-btn gn-btn-primary gn-btn-block gn-btn-md gn-job-proposal-hire-btn"
						data-gn-proposal-id="<?php echo esc_attr( $proposal_id ); ?>"
						data-gn-redirect-url="<?php echo esc_url( $redirect_url ); ?>">
						<?php echo esc_html__( 'Hire', 'gigneo' ); ?>
					</button>
				<?php } else { ?>
					<a href="<?php echo esc_url( get_permalink( $author_profile_id ) ); ?>"
						class="gn-btn gn-btn-primary gn-btn-md">
						<?php echo esc_html__( 'View profile', 'gigneo' ); ?>
					</a>
				<?php } ?>
				<?php
				if ( $job_author_profile_id === $user_profile_id && ! in_array( $proposal_status, array( 'hired', 'completed', 'cancelled' ), true ) && 'publish' === $job_status ) {
					$is_shortlisted = ( 'shortlisted' === $proposal_status );
					$shortlist_text = $is_shortlisted ? esc_html__( 'Shortlisted', 'gigneo' ) : esc_html__( 'Shortlist', 'gigneo' );
					?>
					<a href="#"
						class="gn-btn gn-btn-outline-grey gn-btn-md gn-shortlist-btn <?php echo $is_shortlisted ? 'gn-active' : ''; ?>"
						data-gn-id="<?php echo esc_attr( $proposal_id ); ?>">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
							stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round"
								d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
						</svg>
						<span><?php echo esc_html( $shortlist_text ); ?></span>
					</a>
				<?php } elseif ( in_array( $proposal_status, array( 'hired', 'completed', 'cancelled' ), true ) ) { ?>
					<a href="<?php echo esc_url( $redirect_url ); ?>" class="gn-btn gn-btn-outline-grey gn-btn-md">
						<i class="hi hi-calendar" aria-hidden="true"></i>
						<?php echo esc_html__( 'View order', 'gigneo' ); ?>
					</a>
				<?php } ?>
			</div>
		<?php } ?>
	</div>
</div>