<?php

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

$search_val     = ! empty( $args['search'] ) ? $args['search'] : '';
$location       = ! empty( $args['location'] ) ? $args['location'] : '';
$category       = ! empty( $args['category'] ) ? $args['category'] : '';
$sub_category   = ! empty( $args['subcategory'] ) ? $args['subcategory'] : '';
$specific_types = ! empty( $args['specific_types'] ) ? $args['specific_types'] : array();
$skills         = ! empty( $args['skills'] ) ? $args['skills'] : array();
$types          = ! empty( $args['type'] ) ? $args['type'] : ( ! empty( $args['types'] ) ? $args['types'] : array() );
$delivery       = ! empty( $args['delivery'] ) ? $args['delivery'] : array();
$min_price      = ! empty( $args['min_price'] ) ? $args['min_price'] : '';
$max_price      = ! empty( $args['max_price'] ) ? $args['max_price'] : '';
$rating         = ! empty( $args['rating'] ) ? $args['rating'] : '';
$featured       = $args['featured'] ?? false;
$verified       = $args['verified'] ?? false;
$map_address    = $args['address'] ?? '';
$map_location   = $args['map_location'] ?? '';

// Build active filter pills
$active_pills = array();

$current_url = ! empty( $args['current_url'] ) ? $args['current_url'] : remove_query_arg( array( 'paged', 'step' ) );

// Search pill.
if ( ! empty( $search_val ) ) {
	$active_pills[] = array(
		'label' => sprintf(
			// translators: %s is the search term entered by the user.
			esc_html__( 'Search: %s', 'gigneo' ),
			esc_html( $search_val )
		),
		'url'   => remove_query_arg( 'search', $current_url ),
	);
}

// Location pill.
if ( ! empty( $location ) ) {
	$location_label = gigneo_get_location_label( $location );
	$active_pills[] = array(
		'label' => $location_label,
		'url'   => remove_query_arg( 'location', $current_url ),
	);
}

// Map pill.
if ( ! empty( $map_address ) ) {
	$active_pills[] = array(
		'label' => $map_address,
		'url'   => remove_query_arg( array( 'address', 'map_location' ), $current_url ),
	);
}

// Category pill.
if ( ! empty( $category ) ) {
	$cat_term       = get_term_by( 'slug', $category, 'gigneo_facet' );
	$active_pills[] = array(
		'label' => $cat_term ? $cat_term->name : $category,
		'url'   => remove_query_arg( array( 'category', 'subcategory', 'specific_types' ), $current_url ),
	);
}

if ( ! empty( $sub_category ) ) {
	$sub_cat_term   = get_term_by( 'slug', $sub_category, 'gigneo_facet' );
	$active_pills[] = array(
		'label' => $sub_cat_term ? $sub_cat_term->name : $sub_category,
		'url'   => remove_query_arg( array( 'subcategory', 'specific_types' ), $current_url ),
	);
}

if ( ! empty( $specific_types ) && is_array( $specific_types ) ) {
	foreach ( $specific_types as $sp_type ) {
		$sp_term   = get_term_by( 'slug', $sp_type, 'gigneo_facet' );
		$remaining = array_diff( $specific_types, array( $sp_type ) );
		$pill_url  = remove_query_arg( 'specific_types', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'specific_types', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $sp_term ? $sp_term->name : $sp_type,
			'url'   => $pill_url,
		);
	}
}

if ( ! empty( $skills ) && is_array( $skills ) ) {
	foreach ( $skills as $skill_slug ) {
		$skill_term = get_term_by( 'slug', $skill_slug, 'gigneo_skill' );
		$remaining  = array_diff( $skills, array( $skill_slug ) );
		$pill_url   = remove_query_arg( 'skills', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'skills', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $skill_term ? $skill_term->name : $skill_slug,
			'url'   => $pill_url,
		);
	}
}

$has_min = '' !== $min_price && floatval( $min_price ) > 0;
$has_max = '' !== $max_price && floatval( $max_price ) < 10000;

if ( $has_min || $has_max ) {
	$price_label = '';
	if ( $has_min && $has_max ) {
		$price_label = gigneo_price_format( $min_price, true ) . ' - ' . gigneo_price_format( $max_price, true );
	} elseif ( $has_min ) {
		$price_label = gigneo_price_format( $min_price, true ) . '+';
	} else {
		$price_label = esc_html__( 'Up to ', 'gigneo' ) . gigneo_price_format( $max_price, true );
	}
	$active_pills[] = array(
		'label' => $price_label,
		'url'   => remove_query_arg( array( 'min_price', 'max_price' ), $current_url ),
	);
}

if ( ! empty( $types ) && is_array( $types ) ) {
	foreach ( $types as $type_val ) {
		$type_label = 'onsite' === $type_val ? esc_html__( 'Onsite', 'gigneo' ) : esc_html__( 'Remote', 'gigneo' );
		$remaining  = array_diff( $types, array( $type_val ) );
		$pill_url   = remove_query_arg( 'type', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'type', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $type_label,
			'url'   => $pill_url,
		);
	}
}

if ( ! empty( $delivery ) && is_array( $delivery ) ) {
	foreach ( $delivery as $del_val ) {
		$del_label = 'visit_me' === $del_val ? esc_html__( "I'll visit freelancer", 'gigneo' ) : esc_html__( 'Freelancer visit me', 'gigneo' );
		$remaining = array_diff( $delivery, array( $del_val ) );
		$pill_url  = remove_query_arg( 'delivery', $current_url );
		if ( ! empty( $remaining ) ) {
			$pill_url = add_query_arg( 'delivery', array_values( $remaining ), $pill_url );
		}
		$active_pills[] = array(
			'label' => $del_label,
			'url'   => $pill_url,
		);
	}
}

if ( ! empty( $rating ) ) {
	$active_pills[] = array(
		'label' => sprintf(
				// translators: %s filter rating value
			esc_html__( '%s Stars & up', 'gigneo' ),
			$rating
		),
		'url'   => remove_query_arg( 'rating', $current_url ),
	);
}

if ( $featured ) {
	$active_pills[] = array(
		'label' => esc_html__( 'Featured', 'gigneo' ),
		'url'   => remove_query_arg( 'featured', $current_url ),
	);
}

if ( $verified ) {
	$active_pills[] = array(
		'label' => esc_html__( 'Verified', 'gigneo' ),
		'url'   => remove_query_arg( 'verified', $current_url ),
	);
}

if ( ! empty( $active_pills ) ) {
	?>
	<div class="gn-active-filters-pills">
		<?php foreach ( $active_pills as $pill ) { ?>
			<a href="<?php echo esc_url( $pill['url'] ); ?>" class="gn-filter-pill">
				<?php echo esc_html( $pill['label'] ); ?>
				<i class="hi hi-x-mark" aria-hidden="true"></i>
			</a>
		<?php } ?>
	</div>
	<?php
}
