<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;

use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyChannelContactService;
use Illuminate\Http\Request;

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

class PropertyChannelContactController extends Controller
{


    private $request;
    private $propertyChannelService;
    private $propertyChannelMappingService;
    private $propertyChannelContactService;


    public function __construct(
        Request $request,
        PropertyChannelService $propertyChannelService,
        PropertyChannelMappingService $propertyChannelMappingService,
        PropertyChannelContactService $propertyChannelContactService
    )
    {
        $this->request = $request;
        $this->propertyChannelService = $propertyChannelService;
        $this->propertyChannelMappingService = $propertyChannelMappingService;
        $this->propertyChannelContactService = $propertyChannelContactService;
    }


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

            $channelMappingRequest = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
            ];

            $channelMapping = $this->propertyChannelMappingService->getOnlyPropertyChannelMapping($channelMappingRequest);
            if($channelMapping['status'] != 'success'){
                throw new ApiErrorException($channelMapping['message']);
            }

            if(!count($channelMapping['data'])){
                throw new ApiErrorException("This channel is not connected with this property");
            }

            $channelContactRequest = [
                'property_channel_mapping_id' => $channelMapping["data"]["id"],
            ];

            if(isset($request->params['id'])){
                $channelContactRequest = [
                    'id' => fillOnUndefined($params, 'id'),
                    'property_channel_mapping_id' => $channelMapping["data"]["id"],
                ];
            }

            $channelContact = $this->propertyChannelContactService->getContact($channelContactRequest);
            if($channelContact['status'] != 'success'){
                throw new ApiErrorException($channelContact['message']);
            }


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

            $channelMappingRequest = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
            ];

            $channelMapping = $this->propertyChannelMappingService->getOnlyPropertyChannelMapping($channelMappingRequest);
            if($channelMapping['status'] != 'success'){
                throw new ApiErrorException($channelMapping['message']);
            }

            if(!count($channelMapping['data'])){
                throw new ApiErrorException("This channel is not connected with this property");
            }

            $requestParams = [
                'property_channel_mapping_id' => $channelMapping["data"]["id"],
                'name' => fillOnUndefined($params, 'name', null),
                'email' => fillOnUndefined($params, 'email', null),
                'user_id' => $this->request->auth->id,
            ];

            $channelContact = $this->propertyChannelContactService->add($requestParams);
            if($channelContact['status'] != 'success'){
                throw new ApiErrorException($channelContact['message']);
            }

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

            $channelMappingRequest = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
            ];

            $channelMapping = $this->propertyChannelMappingService->getOnlyPropertyChannelMapping($channelMappingRequest);
            if($channelMapping['status'] != 'success'){
                throw new ApiErrorException($channelMapping['message']);
            }

            if(!count($channelMapping['data']) || $channelMapping['data']['id'] != $params['property_channel_mapping_id']){
                throw new ApiErrorException("This channel is not connected with this property");
            }

            $requestParams = [
                'name' => fillOnUndefined($params, 'name', null),
                'email' => fillOnUndefined($params, 'email', null),
                'updated_by' => $this->request->auth->id,
                'updated_at' => time()
            ];

            $channelContact = $this->propertyChannelContactService->update($params['id'],$requestParams);
            if($channelContact['status'] != 'success'){
                throw new ApiErrorException($channelContact['message']);
            }

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

            $channelMappingRequest = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
            ];

            $channelMapping = $this->propertyChannelMappingService->getOnlyPropertyChannelMapping($channelMappingRequest);
            if($channelMapping['status'] != 'success'){
                throw new ApiErrorException($channelMapping['message']);
            }

            if(!count($channelMapping['data']) || $channelMapping['data']['id'] != $params['property_channel_mapping_id']){
                throw new ApiErrorException("This channel is not connected with this property");
            }

            $requestParams = [
                'id' => $params["id"],
                'property_channel_mapping_id' => $params['property_channel_mapping_id']
            ];

            $channelContact = $this->propertyChannelContactService->delete($requestParams);
            if($channelContact['status'] != 'success'){
                throw new ApiErrorException($channelContact['message']);
            }

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



}