<?php

namespace App\Core\Validator\PropertyRoom;

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use App\Models\PropertyRoom;

use App\Core\Validator\BaseValidator;

use Exception;

class PropertyRoomAndBedUpdateValidator extends BaseValidator
{


    public function __construct
    (

        Translator $translator,
        Container $container
    )
    {
        parent::__construct($translator, $container);

    }

    public function rules($params = null)
    {
        return
            [
                'property_id' => 'required|numeric',
                'room_id' => 'required|numeric',
                'name' => 'required|max:200',
                "checkName" => "accepted",
                'room_type_id' => 'required|numeric',
                'room_type_count' => 'required|numeric',
                'max_occupancy' => 'required|numeric|min:1',
                'max_adult' => 'required|numeric',
                'max_child' => 'required|numeric',
                'room_bed_group' => 'nullable|array',
                'room_bed_group.*' => 'nullable|array',
                'room_bed_group.*.*.bed_type_id' => 'nullable|numeric',
                'exclude_occupancy' => 'nullable|max:500',
                'room_size_type' => 'required|max:30',
                'room_size' => 'required|max:11',
                'is_connected_room' => 'nullable|boolean',
                'is_connected_room_price' => 'nullable|boolean',
                'is_connected_room_availability' => 'nullable|boolean',
            ];


    }

    public function messages()
    {
        $thisMessages = [];
        return array_merge(parent::messages() , $thisMessages);
    }


    public function checkName($params)
    {
        try {
            $roomRate = PropertyRoom::where('property_id', $params['property_id'])
                ->where('name', $params['name'])
                ->where('room_type_id', $params['room_type_id'])
                ->where('id', '!=', $params['room_id'])
                ->where('status', 1)
                ->first();

            return $roomRate ? false : true;
        } catch (Exception $e) {
            return false;
        }
    }

    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["checkName"] = $this->checkName(
            [
                'property_id' => $params["property_id"],
                'name' => $params["name"],
                'room_id' => $params['room_id'] ,
                'room_type_id' => $params['room_type_id']
            ]);

        return $this->make($params, $this->rules($params), $this->messages());
    }

}
