<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyChannelService;
use Illuminate\Http\Request;

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

class PropertyRoomRateChannelMappingController
{


    private $request;
    private $propertyRoomRateChannelMappingService;
    private $propertyChannelService ;


    public function __construct(
        Request $request,
        PropertyChannelService $propertyChannelService,
        PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService
    )
    {
        $this->request = $request;
        $this->propertyRoomRateChannelMappingService = $propertyRoomRateChannelMappingService;
        $this->propertyChannelService = $propertyChannelService ;

    }



    public function getPropertyRoomRateChannelMapping(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'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
            ];

           $propertyChannel = $this->propertyChannelService->getPropertyChannels($requestParams);
            if($propertyChannel['status'] != 'success'){
                throw new ApiErrorException($propertyChannel['message']);
            }
            $propertyChannels = collect($propertyChannel['data'])->where('is_selected', '=', true);
            if($requestParams['channel_id']){
                $propertyChannels = $propertyChannels->whereIn('id',  $requestParams['channel_id']) ;
            }
            $propertyChannels = $propertyChannels->values()->toArray() ;
            $requestParams['channels'] = $propertyChannels ;

            $propertyRoomRateChannelMapping = $this->propertyRoomRateChannelMappingService->getPropertyRoomRateChannelMapping($requestParams);
            if($propertyRoomRateChannelMapping['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateChannelMapping['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRateChannelMapping['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 addPropertyRoomRateChannelMapping(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_id' => fillOnUndefined($params, 'room_id'),
                'room_rate_mapping_id' => fillOnUndefined($params, 'room_rate_mapping_id'),
                'channels' => fillOnUndefined($params, 'channels'),
                'user_id' => $this->request->auth->id,
            ];


            $propertyRoomRateChannelMapping = $this->propertyRoomRateChannelMappingService->addPropertyRoomRateChannelMapping($requestParams);
            if($propertyRoomRateChannelMapping['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateChannelMapping['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRateChannelMapping['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 updatePropertyRoomRateChannelMapping(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_id' => fillOnUndefined($params, 'room_id'),
                'room_rate_channel_mapping' => fillOnUndefined($params, 'room_rate_channel_mapping'),
                'user_id' => $this->request->auth->id,
            ];


            $propertyRoomRateChannelMapping = $this->propertyRoomRateChannelMappingService->updatePropertyRoomRateChannelMapping($requestParams);
            if($propertyRoomRateChannelMapping['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateChannelMapping['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRateChannelMapping['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']);


    }




}
