<?php

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

$service_id    = ! empty( $args['service_id'] ) ? $args['service_id'] : 0;
$plan          = ! empty( $args['plan'] ) ? $args['plan'] : 'one';
$delivery_type = ! empty( $args['delivery_type'] ) ? $args['delivery_type'] : '';

$profile_id          = gigneo_active_profile_id();
$profile_type        = gigneo_profile_type( $profile_id );
$thumbnail_id        = get_post_thumbnail_id( $service_id );
$service_type        = gigneo_service_type( $service_id );
$service_price_plans = gigneo_service_price_plan();

$plan               = in_array( $plan, array( 'one', 'two', 'three' ), true ) ? $plan : 'one';
$plan_title         = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_title" );
$plan_title         = ! empty( $plan_title ) ? $plan_title : '';
$delivery_time_type = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_delivery_time_type" );
$delivery_time      = ! empty( $plan_one_delivery_time ) ? intval( $plan_one_delivery_time ) : 0;
$delivery_time      = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_delivery_time" );
$revisions          = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_revisions" );
$revisions          = ! empty( $revisions ) ? $revisions : esc_html__( 'Unlimited', 'gigneo' );
$price              = gigneo_post_meta( $service_id, "_gigneo_pm_price_plan_{$plan}_price" );
$price              = ! empty( $price ) ? gigneo_price_format( $price, true ) : '';

$author_id         = get_post_field( 'post_author', $service_id );
$author_profile_id = gigneo_profile_id( $author_id, 'freelancer' );
$author_avatar_id  = get_post_thumbnail_id( $author_profile_id ) ?? 0;
$online_status     = gigneo_online_status( $author_profile_id );

$author_profile_average_rating = gigneo_profile_rating( $author_profile_id, 'average' );
$author_profile_reviews        = gigneo_profile_rating( $author_profile_id );

$title_type = gigneo_option( '_gigneo_ts_service_pricing_title', 'custom' );
?>
<div class="gn-hire-service-info">
	<div class="gn-body">
		<figure class="gn-featured-image">
			<?php gigneo_attachment_image_src( $thumbnail_id ?? 0 ); ?>
		</figure>
		<div class="gn-info-wrapper">
			<div class="gn-info">
				<h3 class="gn-title">
					<a href="<?php echo esc_url( get_permalink( $service_id ) ); ?>"><?php echo esc_html( get_the_title( $service_id ) ); ?></a>
				</h3>
				<ul class="gn-features">
					<li class="gn-feature gn-delivery-time">
						<i class="hi hi-clock" aria-hidden="true"></i>
						<span class="gn-title">
						<?php
						printf(
								// translators: %s is the delivery time, e.g., 3 days.
							esc_html__( 'Delivery in %s', 'gigneo' ),
							'<strong>' . esc_html( gigneo_delivery_time( $delivery_time, $delivery_time_type ) ) . '</strong>'
						);
						?>
								</span>
					</li>
					<li class="gn-feature gn-revision">
						<i class="hi hi-arrow-path" aria-hidden="true"></i>
						<span class="gn-label">
							<?php
							printf(
								// translators: %1$s is the number of revisions & %2$s is the round text
								esc_html__( '%1$s %2$s of revisions', 'gigneo' ),
								'<strong>' . esc_html( $revisions ) . '</strong>',
								'<strong>' . esc_html__( 'round', 'gigneo' ) . '</strong>'
							)
							?>
						</span>
					</li>
					<li class="gn-feature gn-type">
						<i class="hi hi-map-pin" aria-hidden="true"></i>
						<span class="gn-label">
							<?php
							$service_type_text = esc_html__( 'Remote', 'gigneo' );
							if ( 'onsite' === $service_type ) {
								$service_type_text = esc_html__( 'Onsite', 'gigneo' );
								if ( 'on_location' === $delivery_type ) {
									$service_type_text = sprintf(
									// translators: %s is the service type
										esc_html__( '%s - To your location', 'gigneo' ),
										'<strong>' . esc_html__( 'Onsite', 'gigneo' ) . '</strong>'
									);
								} elseif ( 'visit_me' === $delivery_type ) {
									$service_type_text = sprintf(
									// translators: 1: is the service type 2: is the freelancer text
										esc_html__( '%1$s - %2$s visit you', 'gigneo' ),
										'<strong>' . esc_html__( 'Onsite', 'gigneo' ) . '</strong>',
										esc_html( gigneo_freelancer_singular() )
									);
								}
							}
							echo wp_kses_post( $service_type_text );
							?>
						</span>
					</li>
				</ul>
			</div>
			<div class="gn-pricing">
				<span class="gn-plan"><?php echo esc_html( 'custom' === $title_type ? $plan_title : $service_price_plans[ $plan ] ); ?></span>
				<h3 class="gn-amount"><?php echo esc_html( $price ); ?></h3>
			</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>
				<p class="gn-ratings">
					<i class="hi hi-star-alt" aria-hidden="true"></i>
					<?php echo esc_html( $author_profile_average_rating ?? 0 ); ?>
					<span class="gn-count">(<?php echo esc_html( $author_profile_reviews ?? 0 ); ?>)</span>
				</p>
			</div>
		</div>
		<div class="gn-actions">
			<?php
			do_action(
				'gigneo_action_buttons',
				array(
					'member_id'  => $author_profile_id,
					'profile_id' => $profile_id,
					'context'    => 'hire_info',
				)
			);
			?>
			<a href="<?php echo esc_url( get_the_permalink( $author_profile_id ) ); ?>" class="gn-btn gn-btn-outline-grey">
				<i class="hi hi-eye" aria-hidden="true"></i>
			</a>
		</div>
	</div>
</div>
