<?php

namespace App\Core\Service;

use App\Core\Repository\Booking\BookingRoomRepository;

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

class BookingRoomService
{

    private $bookingRoomRepository;


    public function __construct(
        BookingRoomRepository $bookingRoomRepository
    )
    {

        $this->bookingRoomRepository = $bookingRoomRepository;


    }

    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'),
                'room_order_number' => fillOnUndefined($params, 'room_order_number'),
                'occupancy_code' => fillOnUndefined($params, 'occupancy_code'),
                'checkin_date' => fillOnUndefined($params, 'checkin_date'),
                'checkout_date' => fillOnUndefined($params, 'checkout_date'),
                'rate_key' => fillOnUndefined($params, 'rate_key'),
                'rate_key_code' => fillOnUndefined($params, 'rate_key_code'),
                'availability_id' => fillOnUndefined($params, 'availability_id'),
                'availability_code' => fillOnUndefined($params, 'availability_code'),
                'room_id' => fillOnUndefined($params, 'room_id'),
                'room_name' => fillOnUndefined($params, 'room_name'),
                'room_rate_mapping_id' => fillOnUndefined($params, 'room_rate_mapping_id'),
                'room_rate_name' => fillOnUndefined($params, 'room_rate_name'),
                'cancellation_policy' => fillOnUndefined($params, 'cancellation_policy'),
                'payment_type_code' => fillOnUndefined($params, 'payment_type_code'),
                'daily_amount' => fillOnUndefined($params, 'daily_amount'),
                'extra_param' => fillOnUndefined($params, 'extra_param'),
                'rate_detail' => fillOnUndefined($params, 'rate_detail'),
                'amount' => fillOnUndefined($params, 'amount'),
                'discount_amount' => fillOnUndefined($params, 'discount_amount'),
                'total' => fillOnUndefined($params, 'total'),
                'currency_code' => fillOnUndefined($params, 'currency_code'),
                'status' => fillOnUndefined($params, 'status', 1),

            ];

            $userCreateResult = $this->bookingRoomRepository->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->bookingRoomRepository->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->bookingRoomRepository->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);
    }

}
