<?php

namespace App\Core\Service;

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

use App\Core\Repository\Booking\PropertyChannelCouponRepository;
use App\Core\Validator\PropertyChannelCoupon\PropertyChannelCouponValidator;

class PropertyChannelCouponService
{

    private $propertyChannelCouponRepository;
    private $propertyChannelCouponValidator;

    public function __construct(
        PropertyChannelCouponRepository $propertyChannelCouponRepository,
        PropertyChannelCouponValidator $propertyChannelCouponValidator
    )
    {
        $this->propertyChannelCouponRepository = $propertyChannelCouponRepository;
        $this->propertyChannelCouponValidator = $propertyChannelCouponValidator;
    }

    public function select($param = [], $column = ['*'])
    {

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

        try {

            $data = $this->propertyChannelCouponRepository->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 create($param = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $validationResult = $this->propertyChannelCouponValidator->validate($param);

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

            $insertData = [
                "title" => fillOnUndefined($param, "title"),
                "property_id" => fillOnUndefined($param, "property_id"),
                "channel_id" => fillOnUndefined($param, "channel_id"),
                "code" => fillOnUndefined($param, "code"),
                "start_date" => fillOnUndefined($param, "start_date"),
                "end_date" => fillOnUndefined($param, "end_date"),
                "reservation_start_date" => fillOnUndefined($param, "reservation_start_date"),
                "reservation_end_date" => fillOnUndefined($param, "reservation_end_date"),
                'is_notify' => fillOnUndefined($param, 'is_notify'),
                'email' => fillOnUndefined($param, 'email'),
                "status" => fillOnUndefined($param, "status", 1),
                "type" => fillOnUndefined($param, "type"),
                "value" => fillOnUndefined($param, "value"),
                "created_by" => fillOnUndefined($param, "created_by"),
                "updated_by" => fillOnUndefined($param, "updated_by"),
                "created_at" => time(),
                "updated_at" => time(),
            ];

            $insertDataResult = $this->propertyChannelCouponRepository->create($insertData);
            if ($insertDataResult['status'] != 'success') {
                throw new Exception($insertDataResult['message']);
            }

            $insertDataResult = $insertDataResult["data"];

            $response = [
                'status' => true,
                'data' => $insertDataResult,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = 'api-unknown_error';
        }

        return output($response);
    }

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

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

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


            $updateParamAvailable = ['title', 'code', 'start_date', 'end_date', 'reservation_start_date', 'reservation_end_date', 'checked', 'used', 'type', 'value', 'is_notify', 'email' , 'status'];
            foreach ($params as $key => $value) {
                if (!in_array($key, $updateParamAvailable)) {
                    unset($params[$key]);
                }
            }

            $result = $this->propertyChannelCouponRepository->update($id, $params);
            if ($result['status'] != 'success') {
                throw new Exception($result['message']);
            }

            $result = $result["data"];

            $response = [
                'status' => true,
                'data' => $result,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = 'api-unknown_error';
        }

        return output($response);
    }

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

            $deleteThisId = is_array($deleteThisId) ? $deleteThisId : [$deleteThisId];
            $deleteThisArray = $this->propertyChannelCouponRepository->destroy($deleteThisId);

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

            $response = [
                'status' => true,
                'data' => null,
            ];

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

}
