<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyPersonPricingPolicyService;


use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;


class PropertyPersonPricingPolicyController extends Controller
{

    private $request;
    private $propertyPersonPricingPolicyService;

    public function __construct(
        Request $request,
        PropertyPersonPricingPolicyService $propertyPersonPricingPolicyService
    )
    {
        $this->request = $request;
        $this->propertyPersonPricingPolicyService = $propertyPersonPricingPolicyService;
    }

    public function getPropertyPersonPricingPolicyList(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;

            $getPropertyPersonPricingList = $this->propertyPersonPricingPolicyService->getPropertyPersonPricingPolicyList($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['pricing_policies' => $getPropertyPersonPricingList['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 createPropertyPersonPricingAdultPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params =
                [
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'adult_action_type' => fillOnUndefined($params, 'adult_action_type'),
                    'adult' => fillOnUndefined($params, 'adult'),
                    'action_type' => fillOnUndefined($params, 'action_type'),
                    'type' => fillOnUndefined($params, 'type'),
                    'value' => fillOnUndefined($params, 'value'),
                    'reservation_start_date' => fillOnUndefined($params, 'reservation_start_date'),
                    'reservation_end_date' => fillOnUndefined($params, 'reservation_end_date'),
                    'user_id' => $userId,
                ];

            $createResponse = $this->propertyPersonPricingPolicyService->createPropertyPersonPricingAdultPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 createPropertyPersonPricingChildPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params =
                [
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'adult' => fillOnUndefined($params, 'adult'),
                    'child_order' => fillOnUndefined($params, 'child_order'),
                    'child_age_start' => fillOnUndefined($params, 'child_age_start'),
                    'child_age_end' => fillOnUndefined($params, 'child_age_end'),
                    'type' => fillOnUndefined($params, 'type'),
                    'value' => fillOnUndefined($params, 'value'),
                    'reservation_start_date' => fillOnUndefined($params, 'reservation_start_date'),
                    'reservation_end_date' => fillOnUndefined($params, 'reservation_end_date'),
                    'user_id' => $userId,
                ];

            $createResponse = $this->propertyPersonPricingPolicyService->createPropertyPersonPricingChildPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 updatePropertyPersonPricingAdultPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params =
                [
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'pricing_policy_id' => fillOnUndefined($params, 'pricing_policy_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'adult_action_type' => fillOnUndefined($params, 'adult_action_type'),
                    'adult' => fillOnUndefined($params, 'adult'),
                    'action_type' => fillOnUndefined($params, 'action_type'),
                    'type' => fillOnUndefined($params, 'type'),
                    'value' => fillOnUndefined($params, 'value'),
                    'reservation_start_date' => fillOnUndefined($params, 'reservation_start_date'),
                    'reservation_end_date' => fillOnUndefined($params, 'reservation_end_date'),
                    'user_id' => $userId,
                ];

            $createResponse = $this->propertyPersonPricingPolicyService->updatePropertyPersonPricingAdultPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 updatePropertyPersonPricingChildPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params =
                [
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'pricing_policy_id' => fillOnUndefined($params, 'pricing_policy_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'adult' => fillOnUndefined($params, 'adult'),
                    'child_order' => fillOnUndefined($params, 'child_order'),
                    'child_age_start' => fillOnUndefined($params, 'child_age_start'),
                    'child_age_end' => fillOnUndefined($params, 'child_age_end'),
                    'type' => fillOnUndefined($params, 'type'),
                    'value' => fillOnUndefined($params, 'value'),
                    'reservation_start_date' => fillOnUndefined($params, 'reservation_start_date'),
                    'reservation_end_date' => fillOnUndefined($params, 'reservation_end_date'),
                    'room_rate_channel_mapping_id' => fillOnUndefined($params, 'room_rate_channel_mapping_id'),
                    'user_id' => $userId,
                ];

            $createResponse = $this->propertyPersonPricingPolicyService->updatePropertyPersonPricingChildPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 deletePropertyPersonPricingAdultPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'pricing_policy_id' => fillOnUndefined($params, 'pricing_policy_id'),
                'user_id' => $userId,
            ];

            $createResponse = $this->propertyPersonPricingPolicyService->deletePropertyPersonPricingAdultPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 deletePropertyPersonPricingChildPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $userId = $request->credentials->user_id;
            $params = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'pricing_policy_id' => fillOnUndefined($params, 'pricing_policy_id'),
                'user_id' => $userId,
            ];

            $createResponse = $this->propertyPersonPricingPolicyService->deletePropertyPersonPricingChildPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 updateRoomRatePricingAdultPolicy(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;
            $params['user_id'] = $this->request->auth->id;

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

            $getPropertyPersonPricingList = $this->propertyPersonPricingPolicyService->updateRoomRatePricingAdultPolicy($params);

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

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

        } 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 updateRoomRatePricingChildPolicy(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;
            $params['user_id'] = $this->request->auth->id;

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

            $getPropertyPersonPricingList = $this->propertyPersonPricingPolicyService->updateRoomRatePricingChildPolicy($params);

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

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

        } 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 getRoomRateChannelPersonPricingPolicy(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;

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

            $getPropertyPersonPricingList = $this->propertyPersonPricingPolicyService->getRoomRateChannelPersonPricingPolicy($params);

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

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


    }


}
