<?php

namespace App\Core\Service;

use App\Core\Repository\Booking\BookingAddonRepository;

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

class BookingAddonService
{

    private $bookingAddonRepository;


    public function __construct(
        BookingAddonRepository $bookingAddonRepository
    )
    {
        $this->bookingAddonRepository = $bookingAddonRepository;
    }

    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 = [
                'booking_id' => fillOnUndefined($params, 'booking_id'),
                'property_channel_addon_id' => fillOnUndefined($params, 'property_channel_addon_id'),
                'count' => fillOnUndefined($params, 'count'),
                'amount' => fillOnUndefined($params, 'amount'),
                'total' => fillOnUndefined($params, 'total'),
                'currency_code' => fillOnUndefined($params, 'currency_code'),
                'attribute' => fillOnUndefined($params, 'attribute'),
                'status' => fillOnUndefined($params, 'status', 1),
            ];

            $createResult = $this->bookingAddonRepository->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->bookingAddonRepository->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->bookingAddonRepository->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->bookingAddonRepository->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);

    }

}
