<?php

namespace App\Core\Service;

use App\Core\Repository\ServiceLog\ServiceLogRepository;

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

class ServiceLogService
{

    private $serviceLogRepository;


    public function __construct(
        ServiceLogRepository $serviceLogRepository
    )
    {

        $this->serviceLogRepository = $serviceLogRepository;

    }

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

            $insertData = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => fillOnUndefined($params, 'user_id', 1),
                'type' => fillOnUndefined($params, 'type', null),
                'service' => fillOnUndefined($params, 'service'),
                'request' => fillOnUndefined($params, 'request'),
                'response' => fillOnUndefined($params, 'response'),
                'response_time' => fillOnUndefined($params, 'response_time'),
                'ip_address' => fillOnUndefined($params, 'ip_address'),
                'status' => fillOnUndefined($params, 'status'),
                'created_by' => fillOnUndefined($params, 'user_id', 1),
                'updated_by' => fillOnUndefined($params, 'user_id', 1),
            ];

            $createResult = $this->serviceLogRepository->create($insertData);

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

            $response = [
                'status' => true,
                'data' => $createResult["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->serviceLogRepository->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->serviceLogRepository->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);
    }

    public function updateOrCreate($criteria = [], $saveData = [])
    {


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

        try {

            $data = $this->serviceLogRepository->updateOrCreate($criteria, $saveData);
            if ($data['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $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);


    }


}
