<?php

namespace App\Core\Service;

use App\Core\Mail\AffiliateRequestMail;
use App\Core\Repository\Property\PropertyRepository;
use App\Core\Repository\PropertyRoomRatePrice\PropertyRoomRatePriceRepository;
use App\Core\Repository\PropertyWeb\PropertyWebReviewRepository;
use App\Core\Repository\PropertyWebContent\PropertyWebContentRepository;
use App\Core\Repository\PropertyWebPhotoMapping\PropertyWebPhotoMappingRepository;
use App\Core\Service\PropertyAdditionalInfo\PropertyAdditionalInfoService;
use App\Core\Service\PropertyExecutiveService;
use App\Core\Service\PropertyPhotoService\PropertyPhotoService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\PropertyWebAboutUsService;
use App\Core\Validator\PropertyWeb\PropertyWebContactFormValidator;
use Illuminate\Mail\Mailer;
use App\Core\Mail\ContactFormMail;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use \App\Core\Service\PropertyFactService;
use \App\Core\Service\PropertyWebColorMappingService;
use Intervention\Image\Facades\Image;
use App\Core\Service\PropertyPlaceService;

use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Config;
use Carbon\Carbon;


class MyWebContentService
{

    private $propertyRepository;
    private $propertyAdditionalInfoService;
    private $propertyContactService;
    private $propertyFactService;
    private $propertyPhotoService;
    private $propertyRoomService;
    private $propertyExecutiveService;
    private $propertyWebPhotoMappingRepository;
    private $propertyWebColorMappingService;
    private $propertyWebAboutUsService;
    private $mailer;
    private $propertyWebContactFormValidator;
    private $propertyPlaceService;
    private $propertyWebContentRepository;
    private $propertyWebReviewRepository;


    public function __construct(
        Mailer                            $mailer,
        PropertyRepository                $propertyRepository,
        PropertyAdditionalInfoService     $propertyAdditionalInfoService,
        PropertyContactService            $propertyContactService,
        PropertyFactService               $propertyFactService,
        PropertyPhotoService              $propertyPhotoService,
        PropertyRoomService               $propertyRoomService,
        PropertyExecutiveService          $propertyExecutiveService,
        PropertyWebPhotoMappingRepository $propertyWebPhotoMappingRepository,
        PropertyWebColorMappingService    $propertyWebColorMappingService,
        PropertyWebContactFormValidator   $propertyWebContactFormValidator,
        PropertyPlaceService              $propertyPlaceService,
        PropertyWebAboutUsService         $propertyWebAboutUsService,
        PropertyWebRoomMappingService     $propertyWebRoomMappingService,
        PropertyFactMappingService        $propertyFactMappingService,
        PropertyWebContentRepository      $propertyWebContentRepository,
        PropertyWebReviewRepository       $propertyWebReviewRepository,
        PropertyRoomRatePriceRepository   $propertyRoomRatePriceRepository

    )
    {
        $this->propertyRepository = $propertyRepository;
        $this->propertyAdditionalInfoService = $propertyAdditionalInfoService;
        $this->propertyContactService = $propertyContactService;
        $this->propertyFactService = $propertyFactService;
        $this->propertyPhotoService = $propertyPhotoService;
        $this->propertyRoomService = $propertyRoomService;
        $this->propertyExecutiveService = $propertyExecutiveService;
        $this->propertyWebPhotoMappingRepository = $propertyWebPhotoMappingRepository;
        $this->propertyWebColorMappingService = $propertyWebColorMappingService;
        $this->propertyWebAboutUsService = $propertyWebAboutUsService;
        $this->mailer = $mailer;
        $this->propertyWebContactFormValidator = $propertyWebContactFormValidator;
        $this->propertyPlaceService = $propertyPlaceService;
        $this->propertyWebRoomMappingService = $propertyWebRoomMappingService;
        $this->propertyFactMappingService = $propertyFactMappingService;
        $this->propertyWebContentRepository = $propertyWebContentRepository;
        $this->propertyWebReviewRepository = $propertyWebReviewRepository;
        $this->propertyRoomRatePriceRepository = $propertyRoomRatePriceRepository;

    }

    public function home($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => [
                    'propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyRooms.propertyRoomViewMapping.propertyRoomViewType',
                    'propertyChain', 'propertyRooms.propertyRoomPhotoMapping.propertyRoomPhoto',
                    'propertyWeb', 'propertyRooms.propertyRoomType', 'propertyAwardsCertificates.awardsCertificateCategory',
                    'propertyWeb.propertyWebPlaceMapping.placeDetail.propertyPlaceCategory',
                    'propertyWeb.propertyWebPlaceMapping.placeDetail.propertyPlacePhotoMapping.propertyPlacePhoto',
                    'propertyWeb.propertyWebPlaceMapping.placeDetail.propertyPlaceFactMapping.propertyPlaceFactTitleFactMapping.placeFact',
                    'propertyWeb.propertyWebPlaceMapping.placeDetail.propertyPlaceFactMapping.propertyPlaceFactTitleFactMapping.placeFactTitle',
                    'propertyWebComponent'
                ],
                'firstRow' => true
            ];


            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);

            if (!$property) {
                throw new ApiErrorException('property not found');
            }

            $propertyWebRoomCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ]
            ];
            $propertyWebRoom = $this->propertyWebRoomMappingService->select($propertyWebRoomCriteria, ['property_room_id']);
            if ($propertyWebRoom['status'] != 'success') {
                throw new ApiErrorException($propertyWebRoom['message']);
            }
            $propertyWebRoomIds = collect($propertyWebRoom['data'])->pluck('property_room_id')->toArray();


            $rooms = [];
            foreach ($propertyWebRoom['data'] as $roomMappingData) {
                $roomCheck = collect($property['property_rooms'])->where('id', $roomMappingData['property_room_id'])->first();
                if ($roomCheck) {


                    $rooms[$roomMappingData['property_room_id']] = $roomCheck;
                    $rooms[$roomMappingData['property_room_id']]['slug'] = Str::slug($roomCheck['name'], $separator = '-', $language = 'en');

                    unset($rooms[$roomMappingData['property_room_id']]['property_room_photo_mapping']);
                    $roomThumbnailPhoto = NULL;

                    $rooms[$roomMappingData['property_room_id']]['property_room_photos'] = collect($roomCheck['property_room_photo_mapping'])->map(function ($value, $key) use ($property, &$roomThumbnailPhoto) {
                        $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_1024x768.' . $value['property_room_photo']['file_ext'];
                        if (File::exists($photoUrlFilePath)) {
                            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_1024x768.' . $value['property_room_photo']['file_ext'];
                        } else {
                            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_medium.' . $value['property_room_photo']['file_ext'];
                        }
                        if ($key === 0) {
                            $photoThumbUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_200x200.' . $value['property_room_photo']['file_ext'];
                            if (File::exists($photoThumbUrlFilePath)) {
                                $roomThumbnailPhoto = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_200x200.' . $value['property_room_photo']['file_ext'];
                            } else {
                                $roomThumbnailPhoto = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_thumbnail.' . $value['property_room_photo']['file_ext'];
                            }
                        }
                        return $photoUrlFilePath;

                    })->all();

                    $rooms[$roomMappingData['property_room_id']]['cover_photo'] = $rooms[$roomMappingData['property_room_id']]['property_room_photos'] != null ? $rooms[$roomMappingData['property_room_id']]['property_room_photos'][0] : null;
                    $rooms[$roomMappingData['property_room_id']]['room_thumbnail_photo'] = $roomThumbnailPhoto;


                }
            }

            $rooms = array_values($rooms);

            /*
             $rooms = collect($property['property_rooms'])
                ->filter(function ($value) use ($propertyWebRoomIds) {
                    if (!empty($propertyWebRoomIds)) {
                        if (in_array($value['id'], $propertyWebRoomIds)) {
                            return $value;
                        }
                    } else {
                        return $value;
                    }
                })
                ->map(function ($value, $valueKey) use ($property, $propertyWebRoomIds) {

                    $room = $value;
                    unset($room['property_room_photo_mapping']);
                    $roomThumbnailPhoto = NULL;
                    $room['property_room_photos'] = collect($value['property_room_photo_mapping'])->map(function ($value, $key) use ($property, &$roomThumbnailPhoto) {
                        $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_1024x768.' . $value['property_room_photo']['file_ext'];
                        if (File::exists($photoUrlFilePath)) {
                            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_1024x768.' . $value['property_room_photo']['file_ext'];
                        } else {
                            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_medium.' . $value['property_room_photo']['file_ext'];
                        }
                        if ($key === 0) {
                            $photoThumbUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_200x200.' . $value['property_room_photo']['file_ext'];
                            if (File::exists($photoThumbUrlFilePath)) {
                                $roomThumbnailPhoto = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_200x200.' . $value['property_room_photo']['file_ext'];
                            } else {
                                $roomThumbnailPhoto = Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . '/' . $value['property_room_photo']['photo_name'] . '_thumbnail.' . $value['property_room_photo']['file_ext'];
                            }
                        }
                        return $photoUrlFilePath;

                    })->all();
                    $room['cover_photo'] = $room['property_room_photos'] != null ? $room['property_room_photos'][0] : null;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    $room['room_thumbnail_photo'] = $roomThumbnailPhoto;

                    return $room;
                })->values()->all();
            */

            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $coverPhotos = $this->coverPhotos($params);


            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $propertyAwardsCertificates = collect($property['property_awards_certificates'])
                ->where('status', '=', 1)
                ->where('awards_certificate_category.type', '!=', 'PLC')
                //->where('awards_certificate_category.type', '!=', 'MNU')
                ->map(function ($value) {

                    $return = $value;
                    unset($return['awards_certificate_category']);
                    $image = null;

                    if ($value['file_path']) {
                        $urlPath = '/property-photos/' . $value['property_id'] . "/awards-certificates/";
                        $image = Config::get('app.imageUrl') . $urlPath . $value['file_path'];
                    }


                    $catLogo = null;
                    if ($value['awards_certificate_category']['id']) {
                        $urlPath = '/assets/img/awards/';
                        $catLogo = Config::get('app.client_server') . $urlPath . $value['awards_certificate_category']['id'] . '.png';
                    }

                    $return['file_path'] = $image ? $image : null;

                    $category = [
                        'category_name' => $value['awards_certificate_category']['name'],
                        'category_language_key' => $value['awards_certificate_category']['language_key'],
                        'category_country_code' => $value['awards_certificate_category']['country_code'],
                        'category_logo' => $catLogo,
                        'category_type' => $value['awards_certificate_category']['type'],
                        'category_status' => $value['awards_certificate_category']['status'],
                    ];
                    return array_merge($return, $category);
                })->values()->all();

            array_multisort(
                array_column($propertyAwardsCertificates, 'date'), SORT_DESC,
                array_column($propertyAwardsCertificates, 'name'), SORT_ASC,
                $propertyAwardsCertificates
            );

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_language_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'landing_photo' => $coverPhotos,
                'description' => $property['name'],
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'awards_certificate_category' => $propertyAwardsCertificates,
                'property_contact' => $property['property_contact']
            ];

            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = $propertyAdditionalInfo['data'];
            }


            $propertyPlace = [];
            /** Property Place **/
            foreach ($property['property_web']['property_web_place_mapping'] as $place) {

                $place = $place['place_detail'];

                if ($place['status'] != 1) {
                    continue;
                }

                $propertyPlace[$place['id']]['id'] = $place['id'];
                $propertyPlace[$place['id']]['name'] = $place['name'];
                $propertyPlace[$place['id']]['slug'] = Str::slug($place['name'], $separator = '-', $language = 'en');

                $propertyPlace[$place['id']]['category']['name'] = $place['property_place_category']['name'];
                $propertyPlace[$place['id']]['category']['language_key'] = $place['property_place_category']['language_key'];

                $propertyPlace[$place['id']]['photo'] = [
                    'thumb' => '/assets/img/placeholder.webp',
                    'fixed' => '/assets/img/placeholder.webp'
                ];

                if (isset($place['property_place_photo_mapping'])) {
                    $propertyPlacePhotoMapping = collect($place['property_place_photo_mapping'])->sortBy('id')->first();
                    if ($propertyPlacePhotoMapping) {
                        $propertyPlace[$place['id']]['photo'] = [
                            'thumb' => $propertyPlacePhotoMapping['property_place_photo']['photoUrl']['thumb'],
                            'fixed' => $propertyPlacePhotoMapping['property_place_photo']['photoUrl']['fixed'],
                        ];
                    }
                }


                $propertyPlaceFact = [];
                foreach ($place['property_place_fact_mapping'] as $fact) {

                    $factId = $fact['property_place_fact_title_fact_mapping']['place_fact_id'];
                    $factCategoryId = $fact['property_place_fact_title_fact_mapping']['place_fact_title_id'];
                    $propertyPlaceFact[$factCategoryId]['category'] = [
                        'name' => $fact['property_place_fact_title_fact_mapping']['place_fact_title']['name'],
                        'language_key' => $fact['property_place_fact_title_fact_mapping']['place_fact_title']['language_key'],
                    ];

                    $propertyPlaceFact[$factCategoryId]['fact'][] = [
                        'name' => $fact['property_place_fact_title_fact_mapping']['place_fact']['name'],
                        'language_key' => $fact['property_place_fact_title_fact_mapping']['place_fact']['language_key'],
                        'icon' => $fact['property_place_fact_title_fact_mapping']['place_fact']['icon'],
                    ];

                }

                $propertyPlace[$place['id']]['feature'] = !empty($propertyPlaceFact) ? array_values($propertyPlaceFact) : [];

                if (count($propertyPlace) == 2) {
                    //break;
                }

            }

            /** Property Place **/
            $responseData['property_place'] = !empty($propertyPlace) ? array_values($propertyPlace) : [];


            /** Property Facts **/
            $getPropertyFact = $this->propertyFactService->getPropertyFact($params);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $getPropertyFact = $this->getPropertyFactForHtml($getPropertyFact['data']);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $responseData['property_facts'] = $getPropertyFact['data'];
            /** Property Facts **/


            /** Property Review **/
            $propertyWebReviewData = [];


            $propertyWebComponentCheck = collect($property['property_web_component'])->where('component_id', 3)->count();
            if ($propertyWebComponentCheck) {
                $propertyWebReviewRequest = [
                    'criteria' => [
                        ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                        ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ],
                    'orderBy' => [
                        ['field' => 'time', 'value' => 'DESC']
                    ]
                ];

                $propertyWebReview = $this->propertyWebReviewRepository->findByCriteria($propertyWebReviewRequest);


                $currentLanguageCheck = [];
                if (fillOnUndefined($params, 'currentLanguage')) {
                    $currentLanguageCheck = collect($propertyWebReview)->where('language_code', $params['currentLanguage'])->toArray();
                }

                if (count($currentLanguageCheck) > 4) {
                    $propertyWebReviewData = collect($currentLanguageCheck)->take(10)->toArray();
                } else {
                    $propertyWebReviewData = collect($propertyWebReview)->take(10)->toArray();
                }

                $propertyWebReviewData = $propertyWebReviewData ? array_values($propertyWebReviewData) : [];
            }

            $responseData['property_review'] = $propertyWebReviewData;
            /** Property Review **/


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function aboutUs($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $responseData = [];

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => [
                    'propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain',
                    'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates.awardsCertificateCategory',
                    'propertyLanguageSpoken.languageCode',
                ],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }

            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);

            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $coverPhotos = $this->coverPhotos($params);

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }

            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;


            $propertyWebAboutUsParams = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'property_web_id' => fillOnUndefined($params, 'property_web_id'),
                'mode' => $params['mode'] === "preview" ? 2 : 1, // live : 1 preview : 2
            ];
            $propertyWebAboutUs = $this->propertyWebAboutUsService->getPropertyWebAboutUs($propertyWebAboutUsParams);

            if ($propertyWebAboutUs['status'] != 'success') {
                throw new ApiErrorException($propertyWebAboutUs['message']);
            }

            $aboutUs = isset($propertyWebAboutUs['data']) && !empty($propertyWebAboutUs['data']) ? $propertyWebAboutUs['data'] : [];

            $aboutUsFiltred = collect($aboutUs)->first(function ($value, $key) use ($params) {
                if ($value['language_code'] == $params['currentLanguage']) {
                    return $value['value'];
                }
            });

            $propertyAwardsCertificates = collect($property['property_awards_certificates'])
                ->where('awards_certificate_category.type', '!=', 'PLC')
                ->where('status', '=', 1)
                ->map(function ($value) {

                    $return = $value;
                    unset($return['awards_certificate_category']);
                    $image = null;

                    if ($value['file_path']) {
                        $urlPath = '/property-photos/' . $value['property_id'] . "/awards-certificates/";
                        $image = Config::get('app.imageUrl') . $urlPath . $value['file_path'];
                    }


                    $catLogo = null;
                    if ($value['awards_certificate_category']['id']) {
                        $urlPath = '/assets/img/awards/';
                        $catLogo = Config::get('app.client_server') . $urlPath . $value['awards_certificate_category']['id'] . '.png';
                    }

                    $return['file_path'] = $image ? $image : null;

                    $category = [
                        'category_name' => $value['awards_certificate_category']['name'],
                        'category_language_key' => $value['awards_certificate_category']['language_key'],
                        'category_country_code' => $value['awards_certificate_category']['country_code'],
                        'category_logo' => $catLogo,
                        'category_type' => $value['awards_certificate_category']['type'],
                        'category_status' => $value['awards_certificate_category']['status'],
                    ];
                    return array_merge($return, $category);
                })->values()->all();

            array_multisort(
                array_column($propertyAwardsCertificates, 'date'), SORT_DESC,
                array_column($propertyAwardsCertificates, 'name'), SORT_ASC,
                $propertyAwardsCertificates
            );

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();

            $propertyLanguageSpoken = null;
            $propertyLanguageSpoken = collect($property['property_language_spoken'])->map(function ($spokenLanguage) {
                return [
                    'code' => $spokenLanguage['language_code']['code'],
                    'name' => $spokenLanguage['language_code']['name'],
                    'language_key' => $spokenLanguage['language_code']['language_key']
                ];
            });


            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => isset($aboutUsFiltred['value']) ? $aboutUsFiltred['value'] : "",
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'property_contact' => $property['property_contact'],
                'landing_photo' => $coverPhotos,
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'awards_certificate_category' => $propertyAwardsCertificates,
                'property_language_spoken' => $propertyLanguageSpoken,
            ];

            $getPropertyFact = $this->propertyFactService->getPropertyFact($params);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $getPropertyFact = $this->getPropertyFactForHtml($getPropertyFact['data']);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $responseData['property_facts'] = $getPropertyFact['data'];
            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = $propertyAdditionalInfo['data'];
            }


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function getPropertyFactForHtml($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $categories = $params;
            $responseArray = [];
            foreach ($categories as $category) {
                $categoryArray = [];
                $categoryItem = $category;
                foreach ($category['fact_subcategory'] as $subCategory) {
                    $subCategoryItem = $subCategory;
                    $facts = collect($subCategory['fact'])
                        ->where('is_selected', '=', true)
                        ->map(function ($value) {
                            return $value;
                        })->values()->all();
                    $subCategoryItem['fact'] = $facts ? $facts : [];
                    if ($facts) {
                        $categoryArray[$subCategory['id']] = $subCategoryItem;
                    }
                }
                $categoryItem['fact_subcategory'] = $categoryArray;
                $responseArray[$category['id']] = $categoryItem;
            }

            $response = [
                'status' => true,
                'data' => $responseArray,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);

    }

    public function gallery($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];


            $responseData['photos'] = $this->photos($params);;

            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function rooms($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);

            $propertyWebRoomCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ]
            ];
            $propertyWebRoom = $this->propertyWebRoomMappingService->select($propertyWebRoomCriteria, ['property_room_id']);
            if ($propertyWebRoom['status'] != 'success') {
                throw new ApiErrorException($propertyWebRoom['message']);
            }
            $propertyWebRoomIds = collect($propertyWebRoom['data'])->pluck('property_room_id')->toArray();

            $rooms = [];
            foreach ($propertyWebRoom['data'] as $roomMappingData) {
                $roomCheck = collect($property['property_rooms'])->where('id', $roomMappingData['property_room_id'])->first();
                if ($roomCheck) {
                    $rooms[$roomMappingData['property_room_id']] = $roomCheck;
                    $rooms[$roomMappingData['property_room_id']]['slug'] = Str::slug($roomCheck['name'], $separator = '-', $language = 'en');
                }
            }

            $rooms = array_values($rooms);

            /*$rooms = collect($property['property_rooms'])
                ->filter(function ($value) use ($propertyWebRoomIds) {
                    if (!empty($propertyWebRoomIds)) {
                        if (in_array($value['id'], $propertyWebRoomIds)) {
                            return $value;
                        }
                    } else {
                        return $value;
                    }
                })
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();*/

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];


            $criteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'with' => ['propertyRoomType', 'propertyRoomDefaultPhoto.propertyRoomPhoto', 'propertyRoomFactMapping.propertyFact', 'propertyRoomViewMapping.propertyRoomViewType']
            ];
            $propertyRooms = $this->propertyRoomService->select($criteria, ['id', 'name', 'room_type_id', 'max_occupancy', 'max_adult', 'max_child', 'room_size', 'room_size_type', 'room_type_count', 'room_count', 'bathroom_count', 'toilet_count', 'lounge_count', 'max_child_number']);
            if ($propertyRooms['status'] != 'success') {
                throw new ApiErrorException($propertyRooms['message']);
            }

            if (!$propertyRooms['data']) {
                throw new ApiErrorException('room data not found');
            }

            $rooms = [];
            foreach ($propertyWebRoom['data'] as $roomMappingData) {
                $roomCheck = collect($propertyRooms['data'])->where('id', $roomMappingData['property_room_id'])->first();
                if ($roomCheck) {
                    $rooms[$roomMappingData['property_room_id']] = $roomCheck;
                    $rooms[$roomMappingData['property_room_id']]['slug'] = Str::slug($roomCheck['name'], $separator = '-', $language = 'en');
                }
            }

            $newRoomMapping = [];
            foreach ($rooms as $room) {

                $roomPhoto = $room['property_room_default_photo'];

                $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_1024x768.' . $roomPhoto['property_room_photo']['file_ext'];
                $photoUrlThumbFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_200x200.' . $roomPhoto['property_room_photo']['file_ext'];

                $photoItem['photo_original'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '.' . $roomPhoto['property_room_photo']['file_ext'];

                if (File::exists($photoUrlFilePath)) {
                    $photoItem['photo_large'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_1024x768.' . $roomPhoto['property_room_photo']['file_ext'];
                } else {
                    $photoItem['photo_large'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_medium.' . $roomPhoto['property_room_photo']['file_ext'];
                }

                if (File::exists($photoUrlThumbFilePath)) {
                    $photoItem['photo_thumbnail'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_200x200.' . $roomPhoto['property_room_photo']['file_ext'];
                } else {
                    $photoItem['photo_thumbnail'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_thumbnail.' . $roomPhoto['property_room_photo']['file_ext'];
                }

                $room['default_photo'] = $photoItem;
                unset($room['property_room_default_photo']);
                $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');

                $roomFacts = $room['property_room_fact_mapping'];
                $newFacts = [];

                foreach ($roomFacts as $fact) {

                    $factItem['id'] = $fact['id'];
                    $factItem['fact_id'] = $fact['fact_id'];
                    $factItem['name'] = $fact['property_fact']['name'];
                    $factItem['language_key'] = $fact['property_fact']['language_key'];
                    $factItem['type'] = $fact['property_fact']['type'];
                    $factItem['order_number'] = $fact['property_fact']['order_number'];
                    $factItem['icon'] = $fact['property_fact']['icon'];
                    $factItem['is_feature'] = $fact['is_feature'];
                    $newFacts[] = $factItem;
                }

                unset($room['property_room_fact_mapping']);

                $room['facts'] = $newFacts;

                // Room Views
                $roomViews = $room['property_room_view_mapping'];
                $newViews = [];
                foreach ($roomViews as $view) {
                    $viewItem['id'] = $view['property_room_view_type']['id'];
                    $viewItem['name'] = $view['property_room_view_type']['name'];
                    $viewItem['language_key'] = $view['property_room_view_type']['language_key'];
                    $newViews[] = $viewItem;
                }
                unset($room['property_room_view_mapping']);
                $room['views'] = $newViews;

                $newRoomMapping[] = $room;
            }

            $responseData['room_detailed_list'] = $newRoomMapping;

            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function roomDetail($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => [
                    'propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain',
                    //'propertyRooms.propertyRoomDefaultPhoto.propertyRoomPhoto',
                    //'propertyRooms.propertyRoomType',
                    //'propertyRooms.propertyRoomFactMapping.propertyFact',
                    'propertyWebRooms.roomDetail.propertyRoomDefaultPhoto.propertyRoomPhoto',
                    'propertyWebRooms.roomDetail.propertyRoomType',
                    'propertyWeb', 'propertyAwardsCertificates'
                ],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }

            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);

            $rooms = collect($property['property_web_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value['room_detail'];
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    $room['property_room_default_photo'] = $room['property_room_default_photo']['property_room_photo']['photoUrl'];
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];


            $criteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'id', 'condition' => '=', 'value' => $params['room_id']],
                ],
                'with' => ['propertyRoomType', 'propertyRoomBedGroup.propertyRoomBedType', 'propertyRoomPhotoMapping.propertyRoomPhoto', 'propertyRoomFactMapping.propertyFact', 'propertyRoomViewMapping.propertyRoomViewType'],
                'firstRow' => 1
            ];

            $propertyRooms = $this->propertyRoomService->select($criteria, ['id', 'name', 'room_type_id', 'max_occupancy', 'max_adult', 'max_child', 'room_size', 'room_size_type', 'room_type_count', 'room_count', 'bathroom_count', 'toilet_count', 'lounge_count', 'max_child_number', 'description']);

            if ($propertyRooms['status'] != 'success') {
                throw new ApiErrorException($propertyRooms['message']);
            }

            if (!$propertyRooms['data']) {
                throw new ApiErrorException('room data not found');
            }
            $room = $propertyRooms['data'];

            $bedGroups = collect($room['property_room_bed_group'])->keyBy('bed_group')->keys();
            $group = [];
            foreach ($bedGroups as $bedGroup) {
                $thisGroup = collect($room['property_room_bed_group'])->where('bed_group', '=', $bedGroup)->map(function ($bedItem) use ($bedGroup) {
                    $responseMapping = $bedItem;
                    $responseMapping['bed_type_name'] = $bedItem['property_room_bed_type']['name'];
                    unset($responseMapping['property_room_bed_type']);
                    return $responseMapping;
                })->toArray();
                $newItem = [];
                foreach ($thisGroup as $item) {
                    $newItem[] = $item;
                }
                $group[] = $newItem;
            }
            $room['property_room_bed_group'] = $group;

            $roomDescriptions = json_decode($room['description'], true);
            $roomDescription = null;
            $currentLanguage = isset($params['currentLanguage']) ? $params['currentLanguage'] : 'en';

            if (isset($roomDescriptions[$currentLanguage]) && $roomDescriptions[$currentLanguage]) {
                $roomDescription = $roomDescriptions[$currentLanguage];
            }
            $room['description'] = $roomDescription;

            $roomPhotos = $room['property_room_photo_mapping'];
            $newRoomPhotos = [];

            foreach ($roomPhotos as $roomPhoto) {

                $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_1024x768.' . $roomPhoto['property_room_photo']['file_ext'];
                $photoUrlThumbFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_200x200.' . $roomPhoto['property_room_photo']['file_ext'];
                $photoItem['photo_original'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '.' . $roomPhoto['property_room_photo']['file_ext'];

                if (File::exists($photoUrlFilePath)) {
                    $photoItem['photo_large'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_1024x768.' . $roomPhoto['property_room_photo']['file_ext'];
                } else {
                    $photoItem['photo_large'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_medium.' . $roomPhoto['property_room_photo']['file_ext'];
                }
                if (File::exists($photoUrlThumbFilePath)) {
                    $photoItem['photo_thumbnail'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_200x200.' . $roomPhoto['property_room_photo']['file_ext'];
                } else {
                    $photoItem['photo_thumbnail'] = Config::get('app.imageUrl') . '/property-photos/' . $roomPhoto['property_id'] . '/' . $roomPhoto['property_room_photo']['photo_name'] . '_thumbnail.' . $roomPhoto['property_room_photo']['file_ext'];
                }
                $newRoomPhotos[] = $photoItem;
            }
            $room['default_photo'] = $newRoomPhotos ? $newRoomPhotos[0] : [];
            unset($room['property_room_photo_mapping']);
            $room['photos'] = $newRoomPhotos;

            $roomFacts = $room['property_room_fact_mapping'];
            $newFacts = [];
            foreach ($roomFacts as $fact) {

                $factItem['id'] = $fact['id'];
                $factItem['fact_id'] = $fact['fact_id'];
                $factItem['name'] = $fact['property_fact']['name'];
                $factItem['language_key'] = $fact['property_fact']['language_key'];
                $factItem['type'] = $fact['property_fact']['type'];
                $factItem['order_number'] = $fact['property_fact']['order_number'];
                $factItem['icon'] = $fact['property_fact']['icon'];
                $newFacts[] = $factItem;
            }
            unset($room['property_room_photo_mapping']);
            unset($room['property_room_fact_mapping']);
            $room['photos'] = $newRoomPhotos;
            $room['facts'] = $newFacts;

            // Room Views
            $roomViews = $room['property_room_view_mapping'];
            $newViews = [];
            foreach ($roomViews as $view) {
                $viewItem['id'] = $view['property_room_view_type']['id'];
                $viewItem['name'] = $view['property_room_view_type']['name'];
                $viewItem['language_key'] = $view['property_room_view_type']['language_key'];
                $newViews[] = $viewItem;
            }
            unset($room['property_room_view_mapping']);
            $room['views'] = $newViews;



            $room['minPrice'] = null;
            $room['priceRange'] = null;
            $roomRatesCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'property_room_id', 'condition' => '=', 'value' => $params['room_id']],
                    ['field' => 'amount', 'condition' => '>', 'value' => 0],
                    ['field' => 'date', 'condition' => '>=', 'value' => Carbon::now()->format('Y-m-d')],
                    ['field' => 'date', 'condition' => '<=', 'value' => Carbon::now()->addMonth()->format('Y-m-d')],
                ],
                'orderBy' => [
                    ['field' => 'amount', 'value' => 'ASC']
                ]
            ];

            $minRoomRatePrice = $this->propertyRoomRatePriceRepository->findbyCriteria($roomRatesCriteria, ['id', 'property_id', 'property_room_id', 'date', 'amount', 'currency']);

            $lowPrice = collect($minRoomRatePrice)->sortBy('amount')->first();
            $highPrice = collect($minRoomRatePrice)->sortByDesc('amount')->first();

            if($lowPrice && $highPrice) {

                $room['lowPrice'] = [
                    'amount' => $lowPrice['amount'],
                    'currency' => $lowPrice['currency'],
                ];

                $room['highPrice'] = [
                    'amount' => $highPrice['amount'],
                    'currency' => $highPrice['currency'],
                ];

            }


            $responseData['room_detail'] = $room;

            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function contact($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyType', 'propertyChain', 'propertyExecutive.executiveType', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }


            $newExecutiveArray = [];
            foreach ($property['property_executive'] as $propertyExecutive) {

                $executiveItem = [
                    'id' => $propertyExecutive['id'],
                    'executive_type_name' => $propertyExecutive['executive_type']['name'],
                    'executive_type_locale_key' => $propertyExecutive['executive_type']['language_key'],
                    'executive_type_icon' => $propertyExecutive['executive_type']['icon'],
                    'name_surname' => $propertyExecutive['name_surname'],
                    'email' => $propertyExecutive['email'],
                    'phone_code' => $propertyExecutive['phone_code'],
                    'phone' => $propertyExecutive['phone'],
                    'extension' => $propertyExecutive['extension'],
                    'mobile_code' => $propertyExecutive['mobile_code'],
                    'mobile' => $propertyExecutive['mobile'],
                    'fax_code' => $propertyExecutive['fax_code'],
                    'fax' => $propertyExecutive['fax'],
                    'view_full_phone' => $propertyExecutive['view_full_phone'],
                    'view_full_mobile' => $propertyExecutive['view_full_mobile'],
                    'view_full_fax' => $propertyExecutive['view_full_fax'],
                ];

                if ($propertyExecutive['status'] == 1) {
                    $newExecutiveArray[] = $executiveItem;
                }

            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $additionalInfoRequest = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'additional_info_key_id', 'condition' => '=', 'value' => 10]
                ],
                'firstRow' => true
            ];

            $myWebContactEmail = $this->propertyAdditionalInfoService->select($additionalInfoRequest);

            if ($myWebContactEmail['status'] != 'success') {
                throw new ApiErrorException($myWebContactEmail['message']);
            }

            $myWebContactEmail = isset($myWebContactEmail['data']['value']) && !empty($myWebContactEmail['data']['value']) ? $myWebContactEmail['data']['value'] : null;

            $property['property_contact']['myweb_contact_email'] = $myWebContactEmail;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();


            $distances = [];
            $factDistancesCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'with' => ['fact']
            ];

            $factDistances = $this->propertyFactMappingService->select($factDistancesCriteria);
            if ($factDistances['status'] == 'success') {
                $factDistances = collect($factDistances['data'])->where('fact.parent_id', 735)->toArray();
                foreach ($factDistances as $factDistance) {
                    $distances[] = [
                        'name' => $factDistance['fact']['name'],
                        'icon' => $factDistance['fact']['icon'],
                        'language_key' => $factDistance['fact']['language_key'],
                        'description' => $factDistance['descriptionArray'],
                    ];
                }
            }

            $coverPhotos = $this->coverPhotos($params);


            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'property_executive' => $newExecutiveArray,
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'landing_photo' => $coverPhotos,
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],
                'distances' => $distances

            ];


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function executiveDetail($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $executiveRequest = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'id', 'condition' => '=', 'value' => $params['executive_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'firstRow' => true,
                'with' => ['executiveType']
            ];

            $getExecutiveFields = ['id', 'property_id', 'executive_type_id', 'name_surname', 'email', 'phone_code', 'phone', 'extension', 'mobile_code', 'mobile', 'fax_code', 'fax'];
            $executive = $this->propertyExecutiveService->select($executiveRequest, $getExecutiveFields);
            if ($executive['status'] != 'success' || !$executive['data']) {
                throw new ApiErrorException($executive['message']);
            }

            $propertyExecutive = $executive['data'];
            $executiveItem = [
                'id' => $propertyExecutive['id'],
                'executive_type_name' => $propertyExecutive['executive_type']['name'],
                'executive_type_locale_key' => $propertyExecutive['executive_type']['language_key'],
                'executive_type_icon' => $propertyExecutive['executive_type']['icon'],
                'name_surname' => $propertyExecutive['name_surname'],
                'email' => $propertyExecutive['email'],
                'phone_code' => $propertyExecutive['phone_code'],
                'phone' => $propertyExecutive['phone'],
                'extension' => $propertyExecutive['extension'],
                'mobile_code' => $propertyExecutive['mobile_code'],
                'mobile' => $propertyExecutive['mobile'],
                'fax_code' => $propertyExecutive['fax_code'],
                'fax' => $propertyExecutive['fax'],
                'view_full_phone' => $propertyExecutive['view_full_phone'],
                'view_full_mobile' => $propertyExecutive['view_full_mobile'],
                'view_full_fax' => $propertyExecutive['view_full_fax'],
            ];

            $response = [
                'status' => true,
                'data' => $executiveItem,
            ];
        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function coverPhotos($params = [])
    {

        $propertyWebPhotoMappingRequest = [
            'criteria' => [
                ['field' => 'property_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                ['field' => 'property_web_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_web_id')],
                ['field' => 'is_cover', 'condition' => '=', 'value' => 1],
                ['field' => 'status', 'condition' => '=', 'value' => $params['mode'] == 'preview' ? 2 : 1],
            ],
            'with' => ['propertyPhoto']
        ];
        $propertyWebMappingPhotos = $this->propertyWebPhotoMappingRepository->findByCriteria($propertyWebPhotoMappingRequest);
        $propertyWebMappingPhotos = $propertyWebMappingPhotos ? $propertyWebMappingPhotos : [];
        $coverPhotos = [];

        foreach ($propertyWebMappingPhotos as $key => $photo) {
            $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_1024x768.' . $photo['property_photo']['file_ext'];
            $photoUrlThumbFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_200x200.' . $photo['property_photo']['file_ext'];

            if (File::exists($photoUrlFilePath)) {
                $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_1024x768.' . $photo['property_photo']['file_ext'];
            } else {
                $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_medium.' . $photo['property_photo']['file_ext'];
            }
            if (File::exists($photoUrlThumbFilePath)) {
                $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_200x200.' . $photo['property_photo']['file_ext'];
            } else {
                $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_thumbnail.' . $photo['property_photo']['file_ext'];
            }
            $coverPhotos[$key]['default'] = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '.' . $photo['property_photo']['file_ext'];
            $coverPhotos[$key]['fixed'] = $photoUrlFilePath;
            $coverPhotos[$key]['thumb'] = $photoUrlThumbFilePath;
        }

        return $coverPhotos;

    }

    public function photos($params = [])
    {

        $propertyWebPhotoMappingRequest = [
            'criteria' => [
                ['field' => 'property_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                ['field' => 'property_web_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_web_id')],
                //['field' => 'is_cover', 'condition' => '=', 'value' => 0],
                ['field' => 'status', 'condition' => '=', 'value' => $params['mode'] == 'preview' ? 2 : 1],
            ],
            'with' => ['propertyPhoto']
        ];
        $propertyWebMappingPhotos = $this->propertyWebPhotoMappingRepository->findByCriteria($propertyWebPhotoMappingRequest);
        $propertyWebMappingPhotos = $propertyWebMappingPhotos ? $propertyWebMappingPhotos : [];
        $responsePhotos = [];

        foreach ($propertyWebMappingPhotos as $photo) {

            $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_1024x768.' . $photo['property_photo']['file_ext'];
            $photoUrlThumbFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_200x200.' . $photo['property_photo']['file_ext'];


            if (File::exists($photoUrlFilePath)) {
                $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_1024x768.' . $photo['property_photo']['file_ext'];
            } else {
                $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_medium.' . $photo['property_photo']['file_ext'];
            }

            if (File::exists($photoUrlThumbFilePath)) {
                $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_200x200.' . $photo['property_photo']['file_ext'];
            } else {
                $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $params['property_id'] . '/' . $photo['property_photo']['photo_name'] . '_thumbnail.' . $photo['property_photo']['file_ext'];
            }


            $responsePhotos[] = [
                'id' => $photo['id'],
                'photo_url_thump' => $photoUrlThumbFilePath,
                'photo_url' => $photoUrlFilePath,
                'is_cover' => $photo['is_cover'],
                /*'medium_photo_size' => [
                    'width' => Image::make($photoUrlFilePath)->width(),
                    'height' => Image::make($photoUrlFilePath)->height()
                ] ,*/
            ];

        }
        return $responsePhotos;

    }

    private function checkWebColorMapping($params, $status)
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $webColorRequestData = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'property_web_id' => fillOnUndefined($params, 'property_web_id'),
                'status' => $status
            ];
            $webColorDatas = $this->propertyWebColorMappingService->getPropertyWebColorMappingWithStatus($webColorRequestData, ['id', 'color_code', 'order_number', 'status']);

            if ($webColorDatas['status'] != 'success') {
                throw new ApiErrorException($webColorDatas['message']);
            }

            $data = [];
            if (!empty($webColorDatas['data']['property_web_color_mapping'])) {
                foreach ($webColorDatas['data']['property_web_color_mapping'] as $key => $webColorMappingData) {
                    $data[$key]['color_number'] = $webColorMappingData['order_number'];
                    $data[$key]['color_code'] = $webColorMappingData['color_code'];
                }
            }

            $response = [
                'status' => true,
                'data' => $data
            ];
        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function contactForm($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $validationResult = $this->propertyWebContactFormValidator->validate($params);

            if ($validationResult->errors()->first()) {
                $errors = $validationResult->errors()->all();
                throw new ApiErrorException($errors);
            }


            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyExecutive.executiveType', 'propertyWeb'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if ($propertyAdditionalInfo['status'] != 'success') {
                throw new ApiErrorException('api-unknown-error');
            }
            $myWebContactEmail = $propertyAdditionalInfo['data']['myweb_contact_email'];

            if (!$myWebContactEmail) {
                throw new ApiErrorException('api-unknown-error');
            }

            $bcc = [];
            $mailData = [
                'to' => [
                    'name' => $property['name'],
                    'email' => $myWebContactEmail,
                ],
                'bcc' => $bcc

            ];

            $mailViewParams = $params;
            $mailViewParams['logo'] = 'https://www.extranetwork.com/assets/img/logo/logo.png';


            $mailParams = [
                'mailData' => $mailData,
                'mailViewParams' => $mailViewParams
            ];

            $this->mailer->onQueue(
                'contactFormMail',
                new ContactFormMail($mailParams)
            );

            $response = [
                'status' => true,
                'data' => null,
                'message' => 'myweb-contact_form-success_message',
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function placeCategoryPlaces($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'property_contact' => $property['property_contact'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),

            ];

            $categoryMapping = $this->propertyPlaceService->getCategoriesInList($params);
            if ($categoryMapping['status'] != 'success') {
                throw new ApiErrorException('api-unknown-error');
            }

            $responseData['places'] = $categoryMapping['data']['places'];
            $responseData['category'] = $categoryMapping['data']['category'];

            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function getPlace($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];


            $getPlace = $this->propertyPlaceService->getPlace($params);
            if ($getPlace['status'] != 'success') {
                throw new ApiErrorException('api-unknown-error');
            }

            if (isset($getPlace['data']['description']) && !is_null($getPlace['data']['description'])) {
                $description = json_decode($getPlace['data']['description'], 1);
                $getPlace['data']['description'] = null;
                if (isset($description[$params['currentLanguage']])) {
                    $getPlace['data']['description'] = $description[$params['currentLanguage']];
                }
            }

            $responseData['place'] = $getPlace['data'];

            //Category's Other Places
            $categoryPlaces = $this->propertyPlaceService->getCategoriesInList(['property_id' => $params['property_id'], 'place_category_id' => $getPlace['data']['category']['id']]);
            $responseData['categoryPlace'] = $categoryPlaces['status'] == 'success' && !empty($categoryPlaces['data']) && isset($categoryPlaces['data']['places']) ? $categoryPlaces['data']['places'] : [];

            $response = [
                'status' => true,
                'data' => $responseData,
            ];


        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function kvkk($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];

            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = collect($propertyAdditionalInfo['data'])->only(['kvkk_mersis_no', 'kvkk_data_controller', 'kvkk_contact_person', 'kvkk_addres', 'kvkk_phone', 'kvkk_tax_office', 'kvkk_tax_number', 'kvkk_email', 'kvkk_web_site', 'kvkk_contact_email', 'kvkk_contact_address'])->toArray();
            }

            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function multimedia($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyType', 'propertyChain', 'propertyExecutive.executiveType', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country', 'content_code'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }


            $newExecutiveArray = [];
            foreach ($property['property_executive'] as $propertyExecutive) {

                $executiveItem = [
                    'id' => $propertyExecutive['id'],
                    'executive_type_name' => $propertyExecutive['executive_type']['name'],
                    'executive_type_locale_key' => $propertyExecutive['executive_type']['language_key'],
                    'executive_type_icon' => $propertyExecutive['executive_type']['icon'],
                    'name_surname' => $propertyExecutive['name_surname'],
                    'email' => $propertyExecutive['email'],
                    'phone_code' => $propertyExecutive['phone_code'],
                    'phone' => $propertyExecutive['phone'],
                    'extension' => $propertyExecutive['extension'],
                    'mobile_code' => $propertyExecutive['mobile_code'],
                    'mobile' => $propertyExecutive['mobile'],
                    'fax_code' => $propertyExecutive['fax_code'],
                    'fax' => $propertyExecutive['fax'],
                    'view_full_phone' => $propertyExecutive['view_full_phone'],
                    'view_full_mobile' => $propertyExecutive['view_full_mobile'],
                    'view_full_fax' => $propertyExecutive['view_full_fax'],
                ];

                if ($propertyExecutive['status'] == 1) {
                    $newExecutiveArray[] = $executiveItem;
                }

            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $additionalInfoRequest = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'additional_info_key_id', 'condition' => '=', 'value' => 10]
                ],
                'firstRow' => true
            ];

            $myWebContactEmail = $this->propertyAdditionalInfoService->select($additionalInfoRequest);

            if ($myWebContactEmail['status'] != 'success') {
                throw new ApiErrorException($myWebContactEmail['message']);
            }

            $myWebContactEmail = isset($myWebContactEmail['data']['value']) && !empty($myWebContactEmail['data']['value']) ? $myWebContactEmail['data']['value'] : null;

            $property['property_contact']['myweb_contact_email'] = $myWebContactEmail;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();


            $distances = [];
            $factDistancesCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'with' => ['fact']
            ];

            $factDistances = $this->propertyFactMappingService->select($factDistancesCriteria);
            if ($factDistances['status'] == 'success') {
                $factDistances = collect($factDistances['data'])->where('fact.parent_id', 735)->toArray();
                foreach ($factDistances as $factDistance) {
                    $distances[] = [
                        'name' => $factDistance['fact']['name'],
                        'icon' => $factDistance['fact']['icon'],
                        'language_key' => $factDistance['fact']['language_key'],
                        'description' => $factDistance['descriptionArray'],
                    ];
                }
            }


            $responseData = [
                'id' => $property['id'],
                'name' => $property['name'],
                'content_code' => $property['content_code'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'property_executive' => $newExecutiveArray,
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],
                'distances' => $distances

            ];


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function content($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];

            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = collect($propertyAdditionalInfo['data'])->only(['kvkk_mersis_no', 'kvkk_data_controller', 'kvkk_contact_person', 'kvkk_addres', 'kvkk_phone', 'kvkk_tax_office', 'kvkk_tax_number', 'kvkk_email', 'kvkk_web_site', 'kvkk_contact_email', 'kvkk_contact_address'])->toArray();
            }

            $columns = ['id', 'property_id', 'content_category_id', 'title', 'slug', 'image', 'language_code', 'status', 'created_at'];
            $propertyWebContentCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'content_category_id', 'condition' => '=', 'value' => $params['content_category_id']],
                    ['field' => 'language_code', 'condition' => '=', 'value' => $params['language_code']],
                ],
                'with' => ['ContentCategory'],
                'orderBy' => [
                    ['field' => 'id', 'value' => 'DESC']
                ]
            ];

            $propertyWebContent = $this->propertyWebContentRepository->findByCriteria($propertyWebContentCriteria, $columns);

            $responseData['property_content'] = $propertyWebContent;


            $response = [
                'status' => true,
                'data' => $responseData,
            ];


        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function contentDetail($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);
            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }
            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();
            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => '',
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'property_contact' => $property['property_contact'],

            ];

            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = collect($propertyAdditionalInfo['data'])->only(['kvkk_mersis_no', 'kvkk_data_controller', 'kvkk_contact_person', 'kvkk_addres', 'kvkk_phone', 'kvkk_tax_office', 'kvkk_tax_number', 'kvkk_email', 'kvkk_web_site', 'kvkk_contact_email', 'kvkk_contact_address'])->toArray();
            }

            $columns = ['id', 'property_id', 'content_category_id', 'title', 'slug', 'image', 'language_code', 'content', 'status', 'created_at'];
            $propertyWebContentCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'slug', 'condition' => '=', 'value' => $params['slug']]
                ],
                'with' => ['ContentCategory'],
                'firstRow' => true
            ];

            $propertyWebContent = $this->propertyWebContentRepository->findByCriteria($propertyWebContentCriteria, $columns);

            $responseData['property_content'] = $propertyWebContent;


            $response = [
                'status' => true,
                'data' => $responseData,
            ];


        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function policy($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            $responseData = [];

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyPhotos', 'propertyType', 'propertyChain', 'propertyRooms', 'propertyWeb', 'propertyAwardsCertificates.awardsCertificateCategory'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $property['property_brand']['logo_url'] = $property['property_brand'] ? Config::get('app.imageUrl') . '/property-photos/' . $property['id'] . "/logo/" . $property['property_brand']['logo_name'] . '_250x250.' . $property['property_brand']['logo_file_ext'] : null;
            $property['property_contact']['social_media_addresses'] = json_decode($property['property_contact']['social_media_addresses'], 1);

            $rooms = collect($property['property_rooms'])
                ->map(function ($value) use ($property) {
                    $room = $value;
                    $room['slug'] = Str::slug($room['name'], $separator = '-', $language = 'en');
                    return $room;
                })->values()->all();

            $coverPhotos = $this->coverPhotos($params);

            $myWebServiceMode = fillOnUndefined($params, 'mode', 'live'); // preview or live
            $myWebServiceModeStatus = $myWebServiceMode === "live" ? 1 : 2;

            $webColormappingRequestData = [
                'property_id' => $property['id'],
                'property_web_id' => $property['property_web']['id']
            ];
            $webColormappingDatas = $this->checkWebColorMapping($webColormappingRequestData, $myWebServiceModeStatus);

            if (empty($webColormappingDatas['data'])) {
                $property['property_brand']['color_codes'] = isset($property['property_brand']['color_codes']) ? json_decode($property['property_brand']['color_codes'], true) : json_decode('[{"color_number":1,"color_code":"#000"},{"color_number":2,"color_code":"#000"}]');
            } else {
                $property['property_brand']['color_codes'] = $webColormappingDatas['data'];
            }

            $property['property_brand']['title'] = isset($property['property_brand']['title']) ? $property['property_brand']['title'] : '{"en":null,"de":null,"es":null,"tr":null}';
            $property['property_brand']['logo_name'] = isset($property['property_brand']['logo_name']) ? $property['property_brand']['logo_name'] : null;


            $propertyWebAboutUsParams = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'property_web_id' => fillOnUndefined($params, 'property_web_id'),
                'mode' => $params['mode'] === "preview" ? 2 : 1, // live : 1 preview : 2
            ];
            $propertyWebAboutUs = $this->propertyWebAboutUsService->getPropertyWebAboutUs($propertyWebAboutUsParams);

            if ($propertyWebAboutUs['status'] != 'success') {
                throw new ApiErrorException($propertyWebAboutUs['message']);
            }

            $aboutUs = isset($propertyWebAboutUs['data']) && !empty($propertyWebAboutUs['data']) ? $propertyWebAboutUs['data'] : [];

            $aboutUsFiltred = collect($aboutUs)->first(function ($value, $key) use ($params) {
                if ($value['language_code'] == $params['currentLanguage']) {
                    return $value['value'];
                }
            });

            $propertyAwardsCertificates = collect($property['property_awards_certificates'])
                ->where('awards_certificate_category.type', '=', 'PLC')
                ->where('status', '=', 1)
                ->map(function ($value) {

                    $return = $value;
                    unset($return['awards_certificate_category']);
                    $image = null;

                    if ($value['file_path']) {
                        $urlPath = '/property-photos/' . $value['property_id'] . "/awards-certificates/";
                        $image = Config::get('app.imageUrl') . $urlPath . $value['file_path'];
                    }


                    $catLogo = null;
                    if ($value['awards_certificate_category']['id']) {
                        $urlPath = '/assets/img/awards/';
                        $catLogo = Config::get('app.client_server') . $urlPath . $value['awards_certificate_category']['id'] . '.png';
                    }

                    $return['file_path'] = $image ? $image : null;

                    $category = [
                        'category_name' => $value['awards_certificate_category']['name'],
                        'category_language_key' => $value['awards_certificate_category']['language_key'],
                        'category_country_code' => $value['awards_certificate_category']['country_code'],
                        'category_logo' => $catLogo,
                        'category_type' => $value['awards_certificate_category']['type'],
                        'category_status' => $value['awards_certificate_category']['status'],
                    ];
                    return array_merge($return, $category);
                })->values()->all();

            array_multisort(
                array_column($propertyAwardsCertificates, 'date'), SORT_DESC,
                array_column($propertyAwardsCertificates, 'name'), SORT_ASC,
                $propertyAwardsCertificates
            );

            $haveSafeCertificate = collect($property['property_awards_certificates'])->where('category_id', '=', 2)->where('status', '=', 1)->first();

            $responseData = [
                'name' => $property['name'],
                'property_type' => fillOnUndefined($property['property_type'], 'name'),
                'property_type_key' => fillOnUndefined($property['property_type'], 'language_key'),
                'property_chain' => fillOnUndefined($property['property_chain'], 'name'),
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'currency_type' => $property['currency_type'],
                'country' => $property['country'],
                'about_us' => isset($aboutUsFiltred['value']) ? $aboutUsFiltred['value'] : "",
                'rooms' => $rooms,
                'property_brand' => $property['property_brand'],
                'property_contact' => $property['property_contact'],
                'landing_photo' => $coverPhotos,
                'have_safe_tourism_cert' => isset($haveSafeCertificate),
                'policy' => $propertyAwardsCertificates

            ];

            $getPropertyFact = $this->propertyFactService->getPropertyFact($params);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $getPropertyFact = $this->getPropertyFactForHtml($getPropertyFact['data']);
            if ($getPropertyFact['status'] != 'success') {
                throw new ApiErrorException($getPropertyFact['message']);
            }

            $responseData['property_facts'] = $getPropertyFact['data'];
            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if (isset($propertyAdditionalInfo['data'])) {
                $responseData['additional_info'] = $propertyAdditionalInfo['data'];
            }


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function promotion($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {
            $responseData = [];

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => [
                    'propertyPromotionMapping.propertyPromotion.promotionType',
                    'propertyPromotionMapping.propertyRoomRateChannelMapping.propertyRoomRateMapping.propertyRoom',
                    'propertyPromotionMapping.propertyRoomRateChannelMapping.propertyRoomRateMapping.propertyRoomRate.propertyRoomRateAccommodation'
                ],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }


            $promotionList = [];
            $responseData['promotion'] = [];

            if (!empty($property['property_promotion_mapping'])) {

                //Web Channel
                $propertyPromotionMapping = collect($property['property_promotion_mapping'])->where('property_room_rate_channel_mapping.channel_id', 1)->toArray();

                foreach ($propertyPromotionMapping as $promotion) {


                    $promotionList[$promotion['property_promotion_id']]['id'] = $promotion['property_promotion']['id'];
                    $promotionList[$promotion['property_promotion_id']]['title'] = __($promotion['property_promotion']['promotion_type']['language_key'], [], $params['language']);
                    $promotionList[$promotion['property_promotion_id']]['type_code'] = $promotion['property_promotion']['promotion_type']['type_code'];
                    $promotionList[$promotion['property_promotion_id']]['order_number'] = $promotion['property_promotion']['promotion_type']['order_number'];
                    $promotionList[$promotion['property_promotion_id']]['detail'] = [
                        'start_date' => $promotion['property_promotion']['start_date'],
                        'end_date' => $promotion['property_promotion']['end_date'],
                        'reservation_start_date' => $promotion['property_promotion']['reservation_start_date'],
                        'reservation_end_date' => $promotion['property_promotion']['reservation_end_date'],
                        'is_time' => $promotion['property_promotion']['is_time'],
                        'start_time' => $promotion['property_promotion']['start_time'],
                        'end_time' => $promotion['property_promotion']['end_time'],
                        'day_before' => $promotion['property_promotion']['day_before'],
                        'amount' => $promotion['property_promotion']['amount'],
                        'min_stay' => $promotion['property_promotion']['min_stay'],
                        'is_mobile' => $promotion['property_promotion']['is_mobile'],
                        'days' => $promotion['property_promotion']['daysArray'],
                    ];

                    $propertyRoomRateMapping = $promotion['property_room_rate_channel_mapping']['property_room_rate_mapping'];

                    $promotionList[$promotion['property_promotion_id']]['room'][$propertyRoomRateMapping['property_room']['id']]['id'] = $propertyRoomRateMapping['property_room']['id'];
                    $promotionList[$promotion['property_promotion_id']]['room'][$propertyRoomRateMapping['property_room']['id']]['title'] = $propertyRoomRateMapping['property_room']['name'];

                    $promotionList[$promotion['property_promotion_id']]['room'][$propertyRoomRateMapping['property_room']['id']]['rate'][$propertyRoomRateMapping['property_room_rate']['id']]['id'] = $propertyRoomRateMapping['property_room_rate']['id'];
                    $promotionList[$promotion['property_promotion_id']]['room'][$propertyRoomRateMapping['property_room']['id']]['rate'][$propertyRoomRateMapping['property_room_rate']['id']]['title'] = $propertyRoomRateMapping['property_room_rate']['name'];
                    $promotionList[$promotion['property_promotion_id']]['room'][$propertyRoomRateMapping['property_room']['id']]['rate'][$propertyRoomRateMapping['property_room_rate']['id']]['accommodation'] = __($propertyRoomRateMapping['property_room_rate']['property_room_rate_accommodation']['language_key'], [], $params['language']);

                    //dd($promotionList, $promotion);

                }


                foreach ($promotionList as $promotionKey => $promotion) {
                    foreach ($promotion['room'] as $promotionRoomKey => $promotionRoom) {
                        $promotionList[$promotionKey]['room'][$promotionRoomKey]['rate'] = array_values($promotionList[$promotionKey]['room'][$promotionRoomKey]['rate']);
                    }
                    $promotionList[$promotionKey]['room'] = array_values($promotionList[$promotionKey]['room']);
                }

                $promotionList = array_values($promotionList);

                $responseData['promotion'] = $promotionList;

            }


            $response = [
                'status' => true,
                'data' => $responseData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function affiliateRequestMail($params = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ],
                'with' => ['propertyBrand', 'propertyContact', 'propertyExecutive.executiveType', 'propertyWeb'],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'property_type_id', 'chain_id', 'rating', 'official_name', 'tax_office', 'tax_number', 'currency_type', 'country'];
            $property = $this->propertyRepository->findByCriteria($propertyRequest, $getPropertyFields);
            if (!$property) {
                throw new ApiErrorException('property not found');
            }
            $propertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo2KeyBy($params);
            if ($propertyAdditionalInfo['status'] != 'success') {
                throw new ApiErrorException('api-unknown-error');
            }
            $myWebContactEmail = $propertyAdditionalInfo['data']['myweb_contact_email'];

            if (!$myWebContactEmail) {
                throw new ApiErrorException('api-unknown-error');
            }

            $propertyExecutives = collect($property['property_executive'])->where('status', 1)->where('executive_type_id', 8)->pluck('email')->toArray();


            $bcc = [];
            $mailData = [
                'to' => [
                    'name' => $property['name'],
                    'email' => $myWebContactEmail,
                ],
                'bcc' => $propertyExecutives

            ];

            $mailViewParams = $params;
            $mailViewParams['logo'] = 'https://www.extranetwork.com/assets/img/logo/logo.png';

            $mailParams = [
                'mailData' => $mailData,
                'mailViewParams' => $mailViewParams
            ];


            $this->mailer->onQueue(
                'affiliateRequestMail',
                new AffiliateRequestMail($mailParams)
            );


            $response = [
                'status' => true,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }


}
