<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyBookingEngine\PropertyBookingEngineSearchRepository;

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

class PropertyBookingEngineSearchService
{

    private $bookingAddonRepository;


    public function __construct(
        PropertyBookingEngineSearchRepository $propertyBookingEngineSearchRepository
    )
    {
        $this->propertyBookingEngineSearchRepository = $propertyBookingEngineSearchRepository;
    }

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

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

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

            $insertData = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'search_key' => fillOnUndefined($params, 'search_key'),
                'channel_id' => fillOnUndefined($params, 'channel_id'),
                'checkin_date' => fillOnUndefined($params, 'checkin_date'),
                'checkout_date' => fillOnUndefined($params, 'checkout_date'),
                'pax' => fillOnUndefined($params, 'pax'),
                'rooms' => fillOnUndefined($params, 'rooms'),
                'booking_code' => fillOnUndefined($params, 'booking_code'),
                'amount' => fillOnUndefined($params, 'best_amount'),
                'currency_code' => fillOnUndefined($params, 'currency_code'),
                'ip_address' => fillOnUndefined($params, 'ip_address'),
                'country_code' => fillOnUndefined($params, 'country_code'),
                'language_code' => fillOnUndefined($params, 'language_code', 'en'),
                'detail' => fillOnUndefined($params, 'detail'),
                'referrer' => fillOnUndefined($params, 'referrer'),
                'status' => fillOnUndefined($params, 'status', 3),
            ];

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

            if ($createResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $userData = $createResult["data"];
            $response = [
                'status' => true,
                'data' => $userData,
            ];

        } 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->propertyBookingEngineSearchRepository->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->propertyBookingEngineSearchRepository->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 delete($ids = []){

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


            $deleteResult = $this->propertyBookingEngineSearchRepository->destroy($ids);

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

            $response = [
                'status' => true,
                'data' => $deleteResult['data'] // affected row count
            ];

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

    }

}
