<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyRoomRateInclusionMapping\PropertyRoomRateInclusionMappingRepository;
use App\Core\Validator\PropertyRoomRateInclusionMapping\PropertyRoomRateInclusionMappingAddValidator;


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

class PropertyRoomRateInclusionMappingService
{

    private $propertyRoomRateInclusionMappingRepository;
    private $propertyRoomRateInclusionMappingAddValidator;


    public function __construct(

        PropertyRoomRateInclusionMappingRepository $propertyRoomRateInclusionMappingRepository,
        PropertyRoomRateInclusionMappingAddValidator $propertyRoomRateInclusionMappingAddValidator
    )
    {

        $this->propertyRoomRateInclusionMappingRepository = $propertyRoomRateInclusionMappingRepository;
        $this->propertyRoomRateInclusionMappingAddValidator = $propertyRoomRateInclusionMappingAddValidator;


    }


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

            DB::beginTransaction();
            $validateData =
                [
                    'room_rate_id' => fillOnUndefined($params, 'room_rate_id'),
                    'facts' => fillOnUndefined($params, 'facts'),
                    "created_by" => fillOnUndefined($params, "user_id"),
                    "updated_by" => fillOnUndefined($params, "user_id")
                ];

            $validationResult = $this->propertyRoomRateInclusionMappingAddValidator->validate($validateData);


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

            $deleteCriteria = [
                'criteria' => [
                    ['field' => 'room_rate_id', 'condition' => '=', 'value' => $validateData['room_rate_id']],
                ]
            ];

            $this->propertyRoomRateInclusionMappingRepository->delete($deleteCriteria);


            foreach ($validateData["facts"] as $fact){
                $insertData = [
                    'room_rate_id' => $validateData['room_rate_id'] ,
                    'fact_id' => $fact,
                    'status' => 1,
                    "created_by" => $validateData['created_by'] ,
                    "updated_by" =>$validateData['updated_by']  ,

                ];


                $userCreateResult = $this->propertyRoomRateInclusionMappingRepository->create($insertData);
                if ($userCreateResult['status'] != 'success') {
                    throw new Exception('api-unknown_error');
                }
            }

            $response = [
                'status' => true,
                'data' => null,
            ];
            DB::commit();

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

        return output($response);
    }

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

            $criteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'room_rate_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'room_rate_id')],
                ],
                'with' => ['propertyFact']
            ];

            $data = $this->propertyRoomRateInclusionMappingRepository->findByCriteria($criteria, ['id', 'room_rate_id', 'fact_id']);
            $return = [];
            foreach ($data as $item){
                if(isset($item['property_fact'])){
                    $row = $item['property_fact'];
                    $return[] = $row ;
                }
            }

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

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




}
