<?php


namespace App\Core\Service;

use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;
use App\Core\Repository\ReputationManagement\PropertyReviewRepository;
use App\Core\Repository\ReputationManagement\PropertyReviewChannelRepository;
use App\Core\Repository\ReputationManagement\PropertyReviewChannelMappingRepository;
use App\Core\Repository\ReputationManagement\PropertyReviewCategoryRepository;


class ReputationManagementService
{


    public function __construct
    (
        PropertyReviewRepository $propertyReviewRepository,
        PropertyReviewChannelRepository $propertyReviewChannelRepository,
        PropertyReviewChannelMappingRepository $propertyReviewChannelMappingRepository,
        PropertyReviewCategoryRepository $propertyReviewCategoryRepository
    )
    {
        $this->propertyReviewRepository = $propertyReviewRepository;
        $this->propertyReviewChannelRepository = $propertyReviewChannelRepository;
        $this->propertyReviewChannelMappingRepository = $propertyReviewChannelMappingRepository;
        $this->propertyReviewCategoryRepository = $propertyReviewCategoryRepository;

    }

    public function select($param = [], $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $data = $this->propertyReviewRepository->findByCriteria($param, $column);
            if (!$data) {
                throw new ApiErrorException(lang('An unknown error occurred'));
            }
            $response['status'] = 1;
            $response['data'] = $data;

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function selectChannel($param = [], $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $data = $this->propertyReviewChannelRepository->findByCriteria($param, $column);
            if (!$data) {
                throw new ApiErrorException(lang('An unknown error occurred'));
            }
            $response['status'] = 1;
            $response['data'] = $data;

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function selectCategory($param = [], $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $data = $this->propertyReviewCategoryRepository->findByCriteria($param, $column);
            if (!$data) {
                throw new ApiErrorException(lang('An unknown error occurred'));
            }
            $response['status'] = 1;
            $response['data'] = $data;

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function selectChannelMapping($param = [], $column = ['*'])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $data = $this->propertyReviewChannelMappingRepository->findByCriteria($param, $column);
            if (!$data) {
                throw new ApiErrorException(lang('An unknown error occurred'));
            }
            $response['status'] = 1;
            $response['data'] = $data;

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function createChannelMapping($param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $createParam = [
                'property_id' => fillOnUndefined($param, 'property_id'),
                'channel_id' => fillOnUndefined($param, 'channel_id'),
                'parameter' => fillOnUndefined($param, 'parameter'),
                'status' => fillOnUndefined($param, 'status'),
                'created_by' => fillOnUndefined($param, 'created_by'),
                'created_at' => time()

            ];
            $createResult = $this->propertyReviewChannelMappingRepository->create($createParam);

            if ($createResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

            $response['status'] = 1;
            $response['data'] = $createResult["data"];

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function updateChannelMapping($id, $param = [])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $updateResult = $this->propertyReviewChannelMappingRepository->update($id, $param);
            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

            $response['status'] = 1;
            $response['data'] = $updateResult["data"];

        } catch (ApiErrorException $e) {
            $response['status'] = 0;
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function deleteChannelMapping($id)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $deleteCriteria = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $id]
                ]
            ];

            $deleteResult = $this->propertyReviewChannelMappingRepository->delete($deleteCriteria);
            if (empty($deleteResult)) {
                throw new Exception('api-unknown_error');
            }

            $response = [
                'status' => true,
                'data' => $deleteResult,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }
}
