<?php

namespace App\Core\Service;

use App\Core\Repository\Booking\BookingTicketRepository;

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

class BookingTicketService
{

    private $bookingTicketRepository;

    public function __construct(
        BookingTicketRepository $bookingTicketRepository
    )
    {

        $this->bookingTicketRepository = $bookingTicketRepository;


    }

    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 = [
                'parent_id' => fillOnUndefined($params, 'parent_id'),
                'booking_id' => fillOnUndefined($params, 'booking_id'),
                'code' => fillOnUndefined($params, 'code'),
                'user_id' => fillOnUndefined($params, 'user_id'),
                'message' => fillOnUndefined($params, 'message'),
                'is_note' => fillOnUndefined($params, 'is_note'),
                'is_viewed' => fillOnUndefined($params, 'is_viewed'),
                'is_log' => fillOnUndefined($params, 'is_log'),
                'log' => fillOnUndefined($params, 'log'),
                'ip_address' => fillOnUndefined($params, 'ip_address'),
                'status' => fillOnUndefined($params, 'status', 1)
            ];


            $userCreateResult = $this->bookingTicketRepository->create($insertData);

            if ($userCreateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $userData = $userCreateResult["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->bookingTicketRepository->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 updateWhere($criteria, $param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $updateResult = $this->bookingTicketRepository->updateWhereBulk($criteria, $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 update($id, $param = [])
    {

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


            $updateResult = $this->bookingTicketRepository->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);
    }

}
