<?php

namespace App\Core\Service;

use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;
use App\Core\Repository\PropertyQuickPricingMapping\PropertyQuickPricingMappingRepository;
use App\Core\Validator\PropertyQuickPricing\PropertyQuickPricingCreateValidator;

class PropertyQuickPricingService
{

    private $propertyQuickPricingMappingRepository;
    private $propertyQuickPricingCreateValidator;

    public function __construct(
        PropertyQuickPricingMappingRepository $propertyQuickPricingMappingRepository,
        PropertyQuickPricingCreateValidator $propertyQuickPricingCreateValidator
    )
    {
        $this->propertyQuickPricingMappingRepository = $propertyQuickPricingMappingRepository;
        $this->propertyQuickPricingCreateValidator = $propertyQuickPricingCreateValidator;
    }

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

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

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

            $insertData = [
                "property_id" => fillOnUndefined($param, "property_id"),
                "channel_id" => fillOnUndefined($param, "channel_id"),
                "room_rate_channel_mapping_id" => fillOnUndefined($param, "room_rate_channel_mapping_id"),
                "action_type" => fillOnUndefined($param, "action_type"),
                "price_type" => fillOnUndefined($param, "price_type", 'PER'),
                "price_value" => fillOnUndefined($param, "price_value"),
                "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->propertyQuickPricingMappingRepository->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 selectPropertyQuickPricingMapping($param = [], $column = ['*'])
    {

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

        try {

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

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


}
