<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyRoomRateMappingSetup\PropertyRoomRateMappingSetupRepository;
use App\Core\Validator\PropertyRoomRateMappingSetup\PropertyRoomRateMappingSetupAddValidator;


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

class PropertyRoomRateMappingSetupService
{

    private $propertyRoomRateMappingSetupRepository;
    private $propertyRoomRateMappingSetupAddValidator;


    public function __construct(

        PropertyRoomRateMappingSetupRepository $propertyRoomRateMappingSetupRepository,
        PropertyRoomRateMappingSetupAddValidator $propertyRoomRateMappingSetupAddValidator
    )
    {

        $this->propertyRoomRateMappingSetupRepository = $propertyRoomRateMappingSetupRepository;
        $this->propertyRoomRateMappingSetupAddValidator = $propertyRoomRateMappingSetupAddValidator;


    }


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

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

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



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

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

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


            foreach ($validateData["setup"] as $item){
                $insertData = [
                    'room_rate_mapping_id' => $validateData['room_rate_mapping_id'] ,
                    'setup_type' => $item['setup_type'],
                    'value_type' => $item['value_type'],
                    'value' => $item['value'],
                    'status' => 1,
                    "created_by" => $validateData['created_by'] ,
                    "updated_by" =>$validateData['updated_by']  ,

                ];


                $userCreateResult = $this->propertyRoomRateMappingSetupRepository->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 getPropertyRoomRateMappingSetup($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

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


            $data = $this->propertyRoomRateMappingSetupRepository->findByCriteria($criteria, ['id', 'room_rate_mapping_id', 'setup_type', 'value_type', 'value']);
            $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);
    }




}
