<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyChannelGroup\PropertyChannelGroupChannelMappingRepository;

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

class PropertyChannelGroupChannelMappingService
{

    private $propertyChannelGroupChannelMappingRepository;

    public function __construct(

        PropertyChannelGroupChannelMappingRepository $propertyChannelGroupChannelMappingRepository

    )
    {

        $this->propertyChannelGroupChannelMappingRepository = $propertyChannelGroupChannelMappingRepository;

    }

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

            /* Todo: validator
            $validationResult = $this->propertyChannelAddValidator->validate($params);

            if ($validationResult->errors()->first()) {
                $errors = $validationResult->errors()->all();
                throw new ApiErrorException($errors);
            }
            */

            $insertData =
                [
                    'channel_group_id' => fillOnUndefined($params, 'channel_group_id'),
                    'channel_id' => fillOnUndefined($params, 'channel_id'),
                    "status" => fillOnUndefined($params, "status", 1),
                    "created_by" => fillOnUndefined($params, "created_by", 0),
                    "updated_by" => fillOnUndefined($params, "updated_by", 0),
                    "created_at"     => time(),
                    "updated_at"     => time(),
                ];

            $insertResponse = $this->propertyChannelGroupChannelMappingRepository->create($insertData);

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

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

        } 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);
    }

    public function select($param = [], $column = ['*'])
    {

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

        try {

            $data = $this->propertyChannelGroupChannelMappingRepository->findByCriteria($param, $column);

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

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

        return output($response);
    }

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


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


            $updateResult = $this->propertyChannelGroupChannelMappingRepository->update($id, $param);
            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $updateData = $updateResult["data"];
            $response = [
                'status' => true,
                'data' => $updateData,
            ];

        } 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);
    }


}
