<?php

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

use Gigneo\Modules;

$job_id            = ! empty( $args['p_id'] ) ? $args['p_id'] : 0;
$proposal_id       = ! empty( $args['proposal_id'] ) ? $args['proposal_id'] : 0;
$job_title         = get_the_title( $job_id );
$job_url           = get_permalink( $job_id );
$author_id         = get_post_field( 'post_author', $job_id );
$author_profile_id = gigneo_profile_id( $author_id, 'employer' );
$author_avatar_id  = get_post_thumbnail_id( $author_profile_id ) ?? 0;
$online_status     = gigneo_online_status( $author_profile_id );
$proposal_count    = gigneo_count_job_proposals( $job_id );
$budget            = gigneo_post_meta( $job_id, '_gigneo_pm_price' );
$experience_level  = gigneo_post_meta( $job_id, '_gigneo_pm_experience_level' );
$description       = gigneo_post_meta( $job_id, '_gigneo_pm_description' );
$duration          = gigneo_post_meta( $job_id, '_gigneo_pm_duration' );
$duration_type     = gigneo_post_meta( $job_id, '_gigneo_pm_duration_type' );
$work_mode         = gigneo_post_meta( $job_id, '_gigneo_pm_type' );
$open_positions    = gigneo_post_meta( $job_id, '_gigneo_pm_open_positions' );
$open_positions    = ! empty( $open_positions ) ? $open_positions : 1; // Default to 1 if not set
$template_args     = array(
	'job_id'      => $job_id,
	'proposal_id' => $proposal_id,
);
$job_status        = get_post_status( $job_id );
$hires             = gigneo_profile_stat( 'total_orders_count', $author_profile_id );
$total_jobs        = gigneo_profile_stat( 'completed_orders_count', $author_profile_id );
$hire_rate         = max(
	0,
	min(
		100,
		$hires['lifetime'] > 0
		? round( ( $total_jobs['lifetime'] / $hires['lifetime'] ) * 100, 1 )
		: 0
	)
);
$profile_id        = gigneo_active_profile_id();
$user_type         = gigneo_profile_type( $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;
if ( 'paused' === $job_status ) { ?>
		<div class="gn-notices-wrapper">
			<div class="gn-notice gn-notice-danger">
				<div class="gn-notice-icon">
					<i class="hi hi-exclamation-circle" aria-hidden="true"></i>
				</div>
				<div class="gn-notice-info">
					<h3 class="gn-title"><?php echo esc_html__( 'Paused', 'gigneo' ); ?></h3>
					<p class="gn-description">
						<?php echo esc_html__( 'This job is currently paused by the author and is not accepting new proposals.', 'gigneo' ); ?>
					</p>
				</div>
			</div>
		</div>
<?php } elseif ( 'in-progress' === $job_status ) { ?>
		<div class="gn-notices-wrapper">
			<div class="gn-notice gn-notice-info">
				<div class="gn-notice-icon">
					<i class="hi hi-exclamation-circle" aria-hidden="true"></i>
				</div>
				<div class="gn-notice-info">
					<h3 class="gn-title"><?php echo esc_html__( 'In progress', 'gigneo' ); ?></h3>
					<p class="gn-description"><?php echo esc_html__( 'This job has been in progress.', 'gigneo' ); ?></p>
				</div>
			</div>
		</div>
<?php } ?>
<div class="gn-job-hire-card gn-job-card-layout-1 gn-job-card">
	<div class="gn-head">
		<div class="gn-heading-wrapper">
			<h2 class="gn-title">
				<?php echo esc_html( $job_title ); ?>
			</h2>
		</div>
		<div class="gn-meta-wrapper">
			<div class="gn-meta">
				<?php if ( $work_mode ) { ?>
						<div class="gn-status-tag">
							<i class="hi hi-map-pin" aria-hidden="true"></i>
							<?php echo esc_html( ucfirst( $work_mode ) ); ?>
						</div>
						<?php
				}
				if ( ! empty( $duration ) && ! empty( $duration_type ) ) {
					?>
						<div class="gn-status-tag">
							<i class="hi hi-clock" aria-hidden="true"></i>
							<?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?>
						</div>
						<?php
				}
				?>
			</div>
		</div>
	</div>
	<div class="gn-body">
		<p class="gn-description">
			<?php echo esc_html( wp_trim_words( $description, 15, '...' ) ); ?>
		</p>
		<div class="gn-actions">
			<a href="<?php echo esc_url( get_the_permalink( $job_id ) ); ?>" class="gn-link gn-link-primary">
				<?php esc_html_e( 'View job posting', 'gigneo' ); ?>
				<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
			</a>
		</div>
		<div class="gn-sub-section gn-preferences">
			<?php if ( ! empty( $pref_location ) || ! empty( $pref_timezone ) || ! empty( $pref_language ) ) { ?>

					<div class="gn-head">
						<h2 class="gn-title"><?php echo esc_html__( 'Preferences', 'gigneo' ); ?></h2>
					</div>

			<?php } ?>
			<div class="gn-body">
				<?php if ( ! empty( $pref_location ) || ! empty( $pref_timezone ) || ! empty( $pref_language ) ) { ?>
						<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' === $user_type ) {
											$active_language = 'hi-exclamation-circle';
											$active_class    = 'gn-value-exclamation';
											if ( has_term( $pref_language->term_id, 'gigneo_language', $profile_id ) ) {
												$active_language = 'hi-check-circle';
												$active_class    = '';
												if ( ! empty( $language_proficiency ) ) {
													$profile_languages = gigneo_post_meta( $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' === $user_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' === $user_type ) {
											$profile_timezone = gigneo_timezone( $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' === $user_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' === $user_type ) {
											$profile_location = gigneo_post_meta( $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' === $user_type ) { ?>
												<i class="hi <?php echo esc_attr( $active_location ); ?>" aria-hidden="true"></i>
											<?php } ?>
										</span>
									</li>
							<?php } ?>
						</ul>
				<?php } ?>
				<ul class="gn-info-wrapper">
					<li class="gn-info">
						<span class="gn-title"><?php echo esc_html__( 'Client\'s budget', 'gigneo' ); ?></span>
						<span class="gn-value"><?php echo esc_html( gigneo_price_format( $budget, true ) ); ?></span>
					</li>
					<li class="gn-info">
						<span class="gn-title"><?php echo esc_html__( 'Experience level', 'gigneo' ); ?></span>
						<span class="gn-value"><?php echo esc_html( ucfirst( $experience_level ) ); ?></span>
					</li>
					<li class="gn-info">
						<span class="gn-title"><?php echo esc_html__( 'Open positions', 'gigneo' ); ?></span>
						<span class="gn-value"><?php echo esc_html( $open_positions ); ?></span>
					</li>
				</ul>
			</div>
		</div>
	</div>
	<div class="gn-footer">
		<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', '', '', $author_profile_id ); ?>
			</figure>
			<div class="gn-info">
				<a href="<?php echo esc_url( get_the_permalink( $author_profile_id ) ); ?>" target="_blank"
					class="gn-name">
					<?php echo esc_html( gigneo_profile_name( $author_profile_id ) ); ?>
				</a>
				<div class="gn-meta-info">
				<p class="gn-ratings">
					<i class="hi hi-star-alt" aria-hidden="true"></i>
					<?php echo esc_html( gigneo_profile_rating( $author_profile_id, 'average' ) ); ?>
					<em class="gn-count">(<?php echo esc_html( gigneo_profile_rating( $author_profile_id ) ); ?>)</em>
				</p>
				<p class="gn-hire-rate">
					<i class="hi hi-user" aria-hidden="true"></i>
					<?php
					printf(
						// translators: %s is hire rate in percentage.
						esc_html__( '%s%% hire rate', 'gigneo' ),
						esc_html( $hire_rate )
					);
					?>
				</p>
				</div>
			</div>
		</div>

	</div>
</div>