<?php

namespace App\Http\Controllers\V1;

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

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

class PropertyRoomRatePriceController
{


    private $request;
    private $propertyRoomRatePriceService;


    public function __construct(
        Request $request,
        PropertyRoomRatePriceService $propertyRoomRatePriceService
    )
    {
        $this->request = $request;
        $this->propertyRoomRatePriceService = $propertyRoomRatePriceService;

    }



    public function getPropertyRoomRatePrice(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'),
            ];

            $propertyRoomRatePrice = $this->propertyRoomRatePriceService->getPropertyRoomRatePrices($requestParams);
            if($propertyRoomRatePrice['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRatePrice['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRatePrice['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 addPropertyRoomRatePrice(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'),
                'property_room_id' => fillOnUndefined($params, 'property_room_id'),
                'room_rate_mapping_id' => fillOnUndefined($params, 'room_rate_mapping_id'),
                'offer_id' => fillOnUndefined($params, 'offer_id'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
                'min_stay' => fillOnUndefined($params, 'min_stay'),
                'max_stay' => fillOnUndefined($params, 'max_stay'),
                'stop_sell' => fillOnUndefined($params, 'stop_sell'),
                'booking_on_request' => fillOnUndefined($params, 'booking_on_request'),
                'start_date' => fillOnUndefined($params, 'start_date'),
                'end_date' => fillOnUndefined($params, 'end_date'),
                'amount' => fillOnUndefined($params, 'amount'),
                'currency' => fillOnUndefined($params, 'currency'),
                'user_id' => $this->request->auth->id,
            ];


            $propertyRoomRatePrice = $this->propertyRoomRatePriceService->addPropertyRoomRatePrice($requestParams);
            if($propertyRoomRatePrice['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRatePrice['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRatePrice['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 updatePropertyRoomRatePrice(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'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'name' => fillOnUndefined($params, 'name'),
                'description' => fillOnUndefined($params, 'description'),
                'min_stay' => fillOnUndefined($params, 'min_stay'),

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


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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyRoomRatePrice['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 deletePropertyRoomRatePrice(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,

            ];

            $propertyRoomRatePrice = $this->propertyRoomRatePriceService->deletePropertyRoomRatePrice($requestParams);
            if($propertyRoomRatePrice['status'] != 'success'){
                throw new ApiErrorException($propertyRoomRatePrice['message']);
            }

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


    }





}
