<?php


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

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Search/filter parameters only.
$args = ! empty( $gn_params ) ? $gn_params : $_GET;

$active_profile_id   = gigneo_active_profile_id();
$active_profile_type = gigneo_profile_type( $active_profile_id );

$posts_per_page = gigneo_option( '_gigneo_ts_employers_per_page', 9 );
$is_map_active  = false;

$options        = get_query_var( 'gigneo_shortcode_options', array() );
$posts_per_page = ! empty( $options['per_page'] ) ? absint( $options['per_page'] ) : $posts_per_page;

$search_val       = ! empty( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '';
$location         = ! empty( $args['location'] ) ? sanitize_text_field( $args['location'] ) : '';
$categories       = ! empty( $args['categories'] ) ? array_map( 'sanitize_text_field', (array) $args['categories'] ) : array();
$skills           = ! empty( $args['skills'] ) ? array_map( 'sanitize_text_field', (array) $args['skills'] ) : array();
$rating           = ! empty( $args['rating'] ) ? absint( $args['rating'] ) : '';
$online           = ! empty( $args['online'] ) ? sanitize_text_field( $args['online'] ) : '';
$verified         = ! empty( $args['verified'] ) ? sanitize_text_field( $args['verified'] ) : '';
$sort             = ! empty( $args['sort'] ) ? sanitize_text_field( $args['sort'] ) : 'recommended';
$orders_completed = ! empty( $args['orders_completed'] ) ? absint( $args['orders_completed'] ) : '';
$current_page     = ! empty( $args['step'] ) ? max( 1, absint( $args['step'] ) ) : 1;

$country_state_list = gigneo_countries_with_states( 'active' );
$saved_items        = gigneo_post_meta( $active_profile_id, '_gigneo_pm_saved_employer' );
$saved_items        = ! empty( $saved_items ) && is_array( $saved_items ) ? $saved_items : array();

$employer_args = array(
	'post_type'      => 'gigneo_employer',
	'posts_per_page' => $posts_per_page,
	'paged'          => $current_page,
	's'              => $search_val,
	'post_status'    => 'publish',
);

$meta_query = array( 'relation' => 'AND' );

// Visibility Conditions
$profile_conditions = gigneo_option( '_gigneo_ts_employer_visibility_conditions' );
$profile_conditions = ! empty( $profile_conditions ) && is_array( $profile_conditions ) ? $profile_conditions : array();

if ( in_array( 'profile_image', $profile_conditions, true ) ) {
	$meta_query[] = array(
		'key'     => '_thumbnail_id',
		'compare' => 'EXISTS',
	);
}

$author_in = array();

if ( in_array( 'email_verified', $profile_conditions, true ) && in_array( 'email_verification', gigneo_active_modules(), true ) ) {
	$verified_users = gigneo_email_verified_users( 'employer' );
	$author_in      = ! empty( $verified_users ) ? $verified_users : array();
}

if ( ! empty( $author_in ) ) {
	$employer_args['author__in'] = $author_in;
}


if ( ! empty( $location ) ) {
	if ( str_contains( $location, ':' ) ) {
		$meta_query[] = array(
			'key'     => '_gigneo_pm_country_state',
			'value'   => $location,
			'compare' => '=',
		);
	} else {
		$meta_query[] = array(
			'relation' => 'OR',
			array(
				'key'     => '_gigneo_pm_country_state',
				'value'   => $location . ':',
				'compare' => 'LIKE',
			),
			array(
				'key'     => '_gigneo_pm_city',
				'value'   => $location,
				'compare' => '=',
			),
		);
	}
}

// Categories
if ( ! empty( $categories ) ) {
	$categories_meta = array( 'relation' => 'OR' );

	foreach ( $categories as $category_slug ) {
		$categories_meta[] = array(
			'key'     => 'carbon_fields:_gigneo_pm_categories',
			'value'   => '"' . $category_slug . '"',
			'compare' => 'LIKE',
		);
	}

	$meta_query[] = $categories_meta;
}


// Skills
if ( ! empty( $skills ) ) {
	$skills_meta = array( 'relation' => 'OR' );

	foreach ( $skills as $skill_slug ) {
		$skills_meta[] = array(
			'key'     => 'carbon_fields:_gigneo_pm_skills',
			'value'   => '"' . $skill_slug . '"',
			'compare' => 'LIKE',
		);
	}

	$meta_query[] = $skills_meta;
}

// Ratings
if ( ! empty( $rating ) ) {
	$meta_query[] = array(
		'key'     => '_gigneo_pm_average_rating',
		'value'   => $rating,
		'compare' => '>=',
		'type'    => 'NUMERIC',
	);
}

// Orders completed
if ( ! empty( $orders_completed ) ) {
	$meta_query[] = array(
		'key'     => '_gigneo_pm_orders_completed',
		'value'   => $orders_completed,
		'compare' => '>=',
		'type'    => 'NUMERIC',
	);
}

//Verified
if ( ! empty( $verified ) ) {
	$employer_args['post__in'] = gigneo_verified_profiles( 'employer' );
}

//Online
if ( ! empty( $online ) ) {
	$employer_args['post__in'] = gigneo_online_profiles( 'employer' );
}

/* Attach meta_query only if filters exist */
if ( count( $meta_query ) > 1 ) {
	// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
	$employer_args['meta_query'] = $meta_query;
}

// Sorting
switch ( $sort ) {

	case 'recommended':
		// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		$employer_args['meta_key'] = '_gigneo_pm_orders_completed';
		$employer_args['orderby']  = 'meta_value_num';
		break;

	case 'top_rated':
		// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		$employer_args['meta_key'] = '_gigneo_pm_average_rating';
		$employer_args['orderby']  = 'meta_value_num';
		break;
}
$employer_args['order'] = 'DESC';

$employer_query = new WP_Query( $employer_args );

$filter_args = array(
	'search'           => $search_val,
	'location'         => $location,
	'categories'       => $categories,
	'skills'           => $skills,
	'rating'           => $rating,
	'online'           => $online,
	'verified'         => $verified,
	'sort'             => $sort,
	'orders_completed' => $orders_completed,
	'found_posts'      => $employer_query->found_posts ?? 0,
);

?>
<div class="gn-archive-wrapper gn-employer-archive-wrapper gn-employer-archive-layout-1"
	data-archive-type="employer" data-ajax-action="gigneo_filter_employers" data-map-id="gn-employer-archive-map"
	data-shortcode-options="<?php echo esc_attr( wp_json_encode( $options ) ); ?>">
	<?php gigneo_load_template( 'archive/employer/parts/filter-layout-1', false, $filter_args ); ?>
	<div class="gn-archive-content gn-employer-archive-content">
		<div class="gn-archive-content-inner">
			<div class="gn-archive-main gn-employer-archive-main">
				<?php
				if ( $employer_query->have_posts() ) {
					while ( $employer_query->have_posts() ) {
						$employer_query->the_post();
						$e_id = get_the_ID();

						$item_args = array(
							'employer_profile_id' => $e_id,
							'profile_id'          => $active_profile_id,
							'profile_type'        => $active_profile_type,
							'country_state_list'  => $country_state_list,
							'saved_items'         => $saved_items,
						);
						gigneo_load_template( 'archive/employer/parts/card-layout-1', false, $item_args );
					}
				} else {
					gigneo_not_found( 'employer_archive' );
				}
				?>
			</div>
		</div>
		<?php
		if ( $employer_query->have_posts() ) {
			gigneo_pagination( $employer_query, $current_page, 'gn-pagination-wrapper' );
			wp_reset_postdata();
		}
		?>
	</div>
</div>