<?php

namespace App\Http\Controllers\V1;

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


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


class PropertyPromotionController extends Controller
{

    private $request;
    private $propertyPromotionService;

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


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

            $getPromotionTypeList = $this->propertyPromotionService->getPromotionTypeList($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['promotion_type' => $getPromotionTypeList['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 getPropertyPromotionList(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;

            $getPropertyPromotionList = $this->propertyPromotionService->getPropertyPromotionList($params);

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

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

        try {

            $params = $this->request->params;
            $params['user_id'] = $request->credentials->user_id;

            $createResponse = $this->propertyPromotionService->createPropertyPromotion($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 getRoomRateChannelPromotion(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;

            $getPropertyPromotionList = $this->propertyPromotionService->getRoomRateChannelPromotion($params);

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

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

        try {

            $params = $this->request->params;
            $params['user_id'] = $request->credentials->user_id;

            $updateResponse = $this->propertyPromotionService->updatePropertyPromotion($params);

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

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


            $getPropertyPromotionList = $this->propertyPromotionService->updateRoomRateChannelPromotion($params);

            if ($getPropertyPromotionList['status'] != "success") {
                throw new ApiErrorException($getPropertyPromotionList['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 deletePropertyPromotion(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $params['user_id'] = $request->credentials->user_id;

            $updateResponse = $this->propertyPromotionService->deletePropertyPromotion($params);

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

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




}
