<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyRoomRateService;
use App\Core\Service\PropertyRoomRateMappingService;
use App\Core\Service\PropertyRoomService;
use Illuminate\Http\Request;

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

class PropertyRoomRateController
{


    private $request;
    private $propertyRoomRateService;
    private $propertyRoomRateMappingService;
    private $propertyRoomService;


    public function __construct(
        Request $request,
        PropertyRoomRateService $propertyRoomRateService,
        PropertyRoomRateMappingService $propertyRoomRateMappingService,
        PropertyRoomService $propertyRoomService
    )
    {
        $this->request = $request;
        $this->propertyRoomRateService = $propertyRoomRateService;
        $this->propertyRoomRateMappingService = $propertyRoomRateMappingService;
        $this->propertyRoomService = $propertyRoomService;
    }



    public function getPropertyRoomRate(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
            ];

            $propertyRoomRate = $this->propertyRoomRateService->getPropertyRoomRates($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function getPropertyRoomRateConnected(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'room_rate_id' => fillOnUndefined($params, 'room_rate_id'),
            ];

            $propertyRoomRateConnected = $this->propertyRoomRateMappingService->getPropertyRoomRateMappingConnected($requestParams);
            if($propertyRoomRateConnected['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateConnected['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRateConnected['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function syncPropertyRoomRateConnected(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'room_rate_id' => fillOnUndefined($params, 'room_rate_id'),
                'connected_room_rate' => fillOnUndefined($params, 'connected_room_rate'),
            ];

            $propertyRoomRateConnected = $this->propertyRoomRateMappingService->syncPropertyRoomRateMappingConnected($requestParams);
            if($propertyRoomRateConnected['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateConnected['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRateConnected['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function addPropertyRoomRate(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'name' => fillOnUndefined($params, 'name'),
                'description' => fillOnUndefined($params, 'description'),
                'accommodation_type' => fillOnUndefined($params, 'accommodation_type'),
                'min_stay' => fillOnUndefined($params, 'min_stay'),
                'max_stay' => fillOnUndefined($params, 'max_stay'),
                'user_id' => $this->request->auth->id,
            ];


            $propertyRoomRate = $this->propertyRoomRateService->addPropertyRoomRate($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function addPropertyRoomRateWithInclusion(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'name' => fillOnUndefined($params, 'name'),
                'accommodation_type' => fillOnUndefined($params, 'accommodation_type'),
                'description' => fillOnUndefined($params, 'description'),
              /*  'min_stay' => fillOnUndefined($params, 'min_stay'),
                'max_stay' => fillOnUndefined($params, 'max_stay'),*/
                'facts' => fillOnUndefined($params, 'facts'),
                'user_id' => $this->request->auth->id,
            ];

            $propertyRoomRate = $this->propertyRoomRateService->addPropertyRoomRateWithInclusion($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            foreach ($params['rooms'] as $room){

                $requestParams = [
                    'locale' => fillOnUndefined($params, 'locale'),
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'room_id' => [$room['room_id']],
                    'room_rate_id' => $propertyRoomRate['data']['id'],
                    'rack_rate' => fillOnUndefined($params, 'rack_rate'),
                    'included_occupancy' => $room['value'],
                    'room_rate_mapping_setup' => [
                        [
                            'setup_type' => 'EXT_ADULT',
                            'value_type' => fillOnUndefined($params, 'ext_adult_type'),
                            'value' => fillOnUndefined($params, 'ext_adult_rate'),
                        ],

                        [
                            'setup_type' => 'EXT_CHILD',
                            'value_type' => fillOnUndefined($params, 'ext_child_type'),
                            'value' => fillOnUndefined($params, 'ext_child_rate'),
                        ],

                        [
                            'setup_type' => 'DIS_GUEST',
                            'value_type' => fillOnUndefined($params, 'dis_guest_type'),
                            'value' => fillOnUndefined($params, 'dis_guest_rate'),
                        ],


                    ],
                    'user_id' => $this->request->auth->id,
                ];

                $propertyRoomRateMapping = $this->propertyRoomRateMappingService->addPropertyRoomRateMappingWithSetup($requestParams);
                if ($propertyRoomRateMapping['status'] != 'success') {
                    throw new ApiErrorException($propertyRoomRateMapping['message']);
                }

            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function updatePropertyRoomRate(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            DB::beginTransaction();
            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [

                'id' => fillOnUndefined($params, 'id'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'name' => trim(fillOnUndefined($params, 'name')),
                'accommodation_type' => fillOnUndefined($params, 'accommodation_type'),
                'min_stay' => fillOnUndefined($params, 'min_stay'),
                'facts' => fillOnUndefined($params, 'facts', []),
                'description' => fillOnUndefined($params, 'description'),
                'user_id' => $this->request->auth->id,
            ];


            if(isset($params['status'])){
                $requestParams['status'] = fillOnUndefined($params, "status", 0);
            }
            $propertyRoomRate = $this->propertyRoomRateService->updatePropertyRoomRate($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            if(isset($params['rooms'])){
                foreach ($params['rooms'] as $room){

                    $criteria = [

                            ['field' => 'room_id', 'condition' => '=', 'value' => $room['room_id']],
                            ['field' => 'room_rate_id', 'condition' => '=', 'value' => $params['id']]

                    ];

                    $data = [
                        'room_id' => fillOnUndefined($room, 'room_id'),
                        'room_rate_id' => fillOnUndefined($params, 'id'),
                        'included_occupancy' => fillOnUndefined($room, 'value'),
                        'status' => 1,
                        'created_by' => $this->request->auth->id,
                        'updated_by' => $this->request->auth->id,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ];

                    $propertyRoomRateMapping = $this->propertyRoomRateMappingService->updateOrCreate($criteria,$data);
                    if($propertyRoomRateMapping['status'] != 'success'){
                        throw new ApiErrorException($propertyRoomRateMapping['message']);
                    }
                }
            }

            DB::commit();
            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            DB::rollBack();
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            DB::rollBack();
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function deletePropertyRoomRate(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [

                'id' => fillOnUndefined($params, 'id'),
                'user_id' => $this->request->auth->id,

            ];

            $propertyRoomRate = $this->propertyRoomRateService->deletePropertyRoomRate($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function getPropertyAccommodationTypes(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $this->request->auth->id,

            ];
            $propertyRoomRate = $this->propertyRoomRateService->getPropertyAccommodationTypes($requestParams);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function editPropertyRoomRate(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;


            $criteria = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['room_rate_id']],
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'firstRow' => 1
            ];

            $propertyRoomRate = $this->propertyRoomRateService->select($criteria);
            if($propertyRoomRate['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRate['message']);
            }

            $criteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1]
                ],
                'with' => ['propertyRoomRateMapping']
            ];

            $propertyRoom = $this->propertyRoomService->select($criteria);
            if($propertyRoom['status'] != 'success'){
                throw new ApiErrorException($propertyRoom['message']);
            }

            $propertyRoomRate['data']['rooms'] = $propertyRoom['data'];

            foreach ($propertyRoomRate['data']['rooms'] as $roomKey => $roomVal){

                $propertyRoomRate['data']['rooms'][$roomKey]['is_selected'] = false;
                $propertyRoomRate['data']['rooms'][$roomKey]['is_selected_lock'] = false;
                $propertyRoomRate['data']['rooms'][$roomKey]['included_occupancy'] = null;

                foreach ($roomVal['property_room_rate_mapping'] as $rate){
                    if($rate['room_rate_id'] == $params['room_rate_id']){
                        $propertyRoomRate['data']['rooms'][$roomKey]['is_selected'] = true;
                        $propertyRoomRate['data']['rooms'][$roomKey]['is_selected_lock'] = true;
                        $propertyRoomRate['data']['rooms'][$roomKey]['included_occupancy'] = $rate['included_occupancy'];
                        break;
                    }
                }

                unset($propertyRoomRate['data']['rooms'][$roomKey]['property_room_rate_mapping']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRate['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }




}
