<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyRoomRateInclusionMappingService;
use Illuminate\Http\Request;

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

class PropertyRoomRateInclusionMappingController
{


    private $request;
    private $propertyRoomRateInclusionMappingService;


    public function __construct(
        Request $request,
        PropertyRoomRateInclusionMappingService $propertyRoomRateInclusionMappingService
    )
    {
        $this->request = $request;
        $this->propertyRoomRateInclusionMappingService = $propertyRoomRateInclusionMappingService;

    }



    public function getPropertyRoomRateInclusionMapping(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_id' => fillOnUndefined($params, 'room_rate_id'),
                'user_id' => $this->request->auth->id,
            ];

            $propertyRoomRateInclusionMapping = $this->propertyRoomRateInclusionMappingService->getPropertyRoomRateInclusionMapping($requestParams);
            if($propertyRoomRateInclusionMapping['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateInclusionMapping['message']);
            }

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


            $propertyRoomRateInclusionMapping = $this->propertyRoomRateInclusionMappingService->addPropertyRoomRateInclusionMapping($requestParams);
            if($propertyRoomRateInclusionMapping['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRateInclusionMapping['message']);
            }

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


    }






}