<?php

namespace App\Core\Service;

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

use App\Core\Repository\PropertyAddon\PropertyAddonRepository;
use App\Core\Repository\PropertyAddon\PropertyChannelAddonRepository;
use App\Core\Validator\PropertyAddon\PropertyAddonValidator;

class PropertyAddonService
{

    private $propertyAddonRepository;
    private $propertyChannelAddonRepository;
    private $propertyAddonValidator;

    public function __construct(
        PropertyAddonRepository $propertyAddonRepository,
        PropertyChannelAddonRepository $propertyChannelAddonRepository,
        PropertyAddonValidator $propertyAddonValidator
    )
    {
        $this->propertyAddonRepository = $propertyAddonRepository;
        $this->propertyChannelAddonRepository = $propertyChannelAddonRepository;
        $this->propertyAddonValidator = $propertyAddonValidator;
    }

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

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

        try {

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

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

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

            $insertData = [
                "title" => fillOnUndefined($param, "title"),
                'description' => fillOnUndefined($param, 'description'),
                "property_id" => fillOnUndefined($param, "property_id"),
                "channel_id" => fillOnUndefined($param, "channel_id"),
                "property_addon_id" => fillOnUndefined($param, "property_addon_id"),
                "amount" => fillOnUndefined($param, "amount"),
                "type" => fillOnUndefined($param, "type", 'PRP'),
                "min_stay" => fillOnUndefined($param, "min_stay"),
                "status" => fillOnUndefined($param, "status", 1),
                "created_by" => fillOnUndefined($param, "created_by"),
                "updated_by" => fillOnUndefined($param, "updated_by"),
                "created_at" => time(),
                "updated_at" => time(),
            ];

            $insertDataResult = $this->propertyChannelAddonRepository->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 updatePropertyChannelAddon($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','description','amount','type','min_stay','status','updated_by'];
            foreach ($params as $key => $value) {
                if(!in_array($key, $updateParamAvailable)) {
                    unset($params[$key]);
                }
            }

            $result = $this->propertyChannelAddonRepository->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 deletePropertyChannelAddon($deleteThisId)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $deleteThisId = is_array($deleteThisId) ? $deleteThisId : [$deleteThisId];
            $deleteThisArray = $this->propertyChannelAddonRepository->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);
    }


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

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

        try {

            $data = $this->propertyAddonRepository->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);
    }

}
