<?php

namespace App\Http\Controllers\V1;

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

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

class PropertyChannelBookingPaymentSetupController
{


    private $request;
    private $propertyChannelBookingPaymentSetupService;


    public function __construct(
        Request $request,
        PropertyChannelBookingPaymentSetupService $propertyChannelBookingPaymentSetupService
    )
    {
        $this->request = $request;
        $this->propertyChannelBookingPaymentSetupService = $propertyChannelBookingPaymentSetupService;

    }



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

            $propertyChannelBookingPaymentSetup = $this->propertyChannelBookingPaymentSetupService->getChannelBookingPaymentSetup($requestParams);

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

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

            $propertyChannelBookingPaymentSetup = $this->propertyChannelBookingPaymentSetupService->addChannelBookingPaymentSetup($requestParams);

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

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


    }

}
