<commit_before><?php

namespace TravelPAQ\PackagesAPI\Services;

use TravelPAQ\PackagesAPI\Services\Service;
use TravelPAQ\PackagesAPI\Models\PackagesPagination;
use TravelPAQ\PackagesAPI\Models\Package;
use TravelPAQ\PackagesAPI\Models\DestinyResultPagination;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;

class PackageService extends Service
{
	public function getPackageByFixedSearches(){
		try {
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 'sliders');
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			if($body_decoded == null){
				throw new \Exception($body);
			}
			$response = [];			
			foreach ($body_decoded['data'] as $key => $fixed) {
				$respon = [];
				$respon['id'] = $fixed['id']; 
				$respon['name'] = $fixed['name']; 
				$respon['active'] = $fixed['active']; 
				$respon['main'] = $fixed['main']; 
				$respon['packages'] = [];
				foreach($fixed['packages'] as $package){
					$respon['packages'][] = new Package($package);						
				}

				$response[] = $respon;
			}

			return $response;
		} catch (RequestException $e) {
			$response_str = "";
			if ($e->hasResponse())
				$response_str = $e->getResponse()->getBody()->getContents();
			return $response_str;
		}
	}

	public function getPackageByFixedSearch($name, $page){
		try {
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 'Packages/getPackageByFixedSearch/' . $name . '/' . $page);
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			$packages = [];
			foreach($body_decoded as $package){
				$packages[] = new Package($package);
			}
			return $packages;
		} catch (RequestException $e) {
			$response_str = "";
			if ($e->hasResponse())
				$response_str = $e->getResponse()->getBody()->getContents();
			return $response_str;
		}
	}

	public function getPackageGroup($params, $groups = null,$page = 0,$filters = null){
		
		if(array_key_exists('order_field', $params) && $params['order_field'] === 'DEPARTURE_DATE')
			$params['order_field'] = 'DEPARTURE';
		/**
		* 
		* Formato v3
		*
		*/
		// rooms
		$room__ = [];
		foreach ($params['Room'] as $r => $room) {	
			$room__[$r]['adult'] = $room['adult'];
			if(array_key_exists('Children', $room)){ 
				if(count($room['Children']) > 0 ){ 
					foreach ($room['Children'] as $c => $child) {	
						$room__[$r]['children'][] = $child['age'];
					}
					$room__[$r]['children'] = implode(',', $room__[$r]['children']);
				}
			}
		}
		$params['rooms'] = $room__;
		unset($params['Room']);		
		$params_get_url = http_build_query($params);	
		
		
		// filters
		$filter__ = [];	
		if($filters){
			
			// hotel star rating

			foreach ($filters as $f => $filter) {
				
				switch ($filter['name']) {
					case 'companies':
						$filter__['companies']	= implode(',', $filter['companies']);			
						break;

					case 'hotel_names':
						$filter__['hotel_names']	= implode(',', $filter['hotel_names']);			
						break;

					case 'hotel_regimes':
						$filter__['hotel_regimes']	= implode(',', $filter['hotel_regimes']);			
						break;

					case 'hotel_room_kinds':
						$filter__['hotel_room_kinds']	= implode(',', $filter['hotel_room_kinds']);			
						break;

					case 'stars':
						$filter__['hotel_star_ratings']	= "{$filter['from']},{$filter['to']}";			
						break;

					case 'nights':
						$filter__['min_nights'] = $filter['from'];	
						$filter__['max_nights'] = $filter['to'];	
						break;

					case 'price':
						$filter__['min_price'] = $filter['from'];	
						$filter__['max_price'] = $filter['to'];	
						break;	

					case 'departure':
						$filter__['min_departure_date'] = date('Y-m-d',strtotime(str_replace('/', '-', $filter['from'])));
						$filter__['max_departure_date'] = date('Y-m-d',strtotime(str_replace('/', '-', $filter['to'])));		
						break;	
				}

			}
		}

		$filters_get_url = http_build_query($filter__);
		if(count($filter__)>0)
			$filters_get_url =  '&'.$filters_get_url;		
		
		// groups
		$groups_get_url = '';		
		if($groups)
			$groups_get_url =  '&'.http_build_query($groups);
		
		try {
			
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 
							  		   "packages/grouped?{$params_get_url}{$filters_get_url}&page_index=$page"
							 );
			
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			
			if($body_decoded == null){
				throw new \Exception($body);
			}

			return new PackagesPagination($body_decoded);
		} catch (RequestException $e) {

			$response_str = "";<commit_msg>Acoplando los parametros de agrupamiento para que SDK<commit_after><?php

namespace TravelPAQ\PackagesAPI\Services;

use TravelPAQ\PackagesAPI\Services\Service;
use TravelPAQ\PackagesAPI\Models\PackagesPagination;
use TravelPAQ\PackagesAPI\Models\Package;
use TravelPAQ\PackagesAPI\Models\DestinyResultPagination;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;

class PackageService extends Service
{
	public function getPackageByFixedSearches(){
		try {
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 'sliders');
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			if($body_decoded == null){
				throw new \Exception($body);
			}
			$response = [];			
			foreach ($body_decoded['data'] as $key => $fixed) {
				$respon = [];
				$respon['id'] = $fixed['id']; 
				$respon['name'] = $fixed['name']; 
				$respon['active'] = $fixed['active']; 
				$respon['main'] = $fixed['main']; 
				$respon['packages'] = [];
				foreach($fixed['packages'] as $package){
					$respon['packages'][] = new Package($package);						
				}

				$response[] = $respon;
			}

			return $response;
		} catch (RequestException $e) {
			$response_str = "";
			if ($e->hasResponse())
				$response_str = $e->getResponse()->getBody()->getContents();
			return $response_str;
		}
	}

	public function getPackageByFixedSearch($name, $page){
		try {
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 'Packages/getPackageByFixedSearch/' . $name . '/' . $page);
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			$packages = [];
			foreach($body_decoded as $package){
				$packages[] = new Package($package);
			}
			return $packages;
		} catch (RequestException $e) {
			$response_str = "";
			if ($e->hasResponse())
				$response_str = $e->getResponse()->getBody()->getContents();
			return $response_str;
		}
	}

	public function getPackageGroup($params, $groups = null,$page = 0,$filters = null){
		
		if(array_key_exists('order_field', $params) && $params['order_field'] === 'DEPARTURE_DATE')
			$params['order_field'] = 'DEPARTURE';
		/**
		* 
		* Formato v3
		*
		*/
		// rooms
		$room__ = [];
		foreach ($params['Room'] as $r => $room) {	
			$room__[$r]['adult'] = $room['adult'];
			if(array_key_exists('Children', $room)){ 
				if(count($room['Children']) > 0 ){ 
					foreach ($room['Children'] as $c => $child) {	
						$room__[$r]['children'][] = $child['age'];
					}
					$room__[$r]['children'] = implode(',', $room__[$r]['children']);
				}
			}
		}
		$params['rooms'] = $room__;
		unset($params['Room']);		
		$params_get_url = http_build_query($params);	
		
		
		// filters
		$filter__ = [];	
		if($filters){
			
			// hotel star rating

			foreach ($filters as $f => $filter) {
				
				switch ($filter['name']) {
					case 'companies':
						$filter__['companies']	= implode(',', $filter['companies']);			
						break;

					case 'hotel_names':
						$filter__['hotel_names']	= implode(',', $filter['hotel_names']);			
						break;

					case 'hotel_regimes':
						$filter__['hotel_regimes']	= implode(',', $filter['hotel_regimes']);			
						break;

					case 'hotel_room_kinds':
						$filter__['hotel_room_kinds']	= implode(',', $filter['hotel_room_kinds']);			
						break;

					case 'stars':
						$filter__['hotel_star_ratings']	= "{$filter['from']},{$filter['to']}";			
						break;

					case 'nights':
						$filter__['min_nights'] = $filter['from'];	
						$filter__['max_nights'] = $filter['to'];	
						break;

					case 'price':
						$filter__['min_price'] = $filter['from'];	
						$filter__['max_price'] = $filter['to'];	
						break;	

					case 'departure':
						$filter__['min_departure_date'] = date('Y-m-d',strtotime(str_replace('/', '-', $filter['from'])));
						$filter__['max_departure_date'] = date('Y-m-d',strtotime(str_replace('/', '-', $filter['to'])));		
						break;	
				}

			}
		}

		$filters_get_url = http_build_query($filter__);
		if(count($filter__)>0)
			$filters_get_url =  '&'.$filters_get_url;		
		
		// groups
		$groups_get_url = '';		
		if($groups)
			$groups_get_url =  '&'.http_build_query($groups);
		
		try {
			
			$response = $this->http_client
							 ->http_client
							 ->request('GET', 
							  		   "packages/grouped?{$params_get_url}{$filters_get_url}&page_index=$page{$groups_get_url}"
							 );
			
			$body = $response->getBody()->getContents();
			$body_decoded = json_decode($body,true);
			
			if($body_decoded == null){
				throw new \Exception($body);
			}

			return new PackagesPagination($body_decoded);
		} catch (RequestException $e) {

			$response_str = "";
{'fraction_non_alphanumeric': 0.19476899989280738, 'fraction_numerical': 0.0010719262514738985, 'mean_word_length': 3.459847036328872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5372835', 'n_tokens_mistral': 4047, 'n_tokens_neox': 3186, 'n_words': 503}