<?php

namespace App\Core\Validator\PropertyRoomRate;

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

use App\Core\Validator\BaseValidator;

use Exception;

class PropertyRoomRateUpdateValidator extends BaseValidator
{


    public function __construct
    (

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

    }

    public function rules($params = null)
    {
        return
            [
                'id' => 'required|numeric',
                'property_id' => 'required|numeric',
                'name' => 'required',
                "checkName" => "accepted",
                'accommodation_type' => 'required|numeric',
                'status' => 'numeric|min:0|max:1'
            ];


    }

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

    public function checkName($params)
    {
        try {
            $roomRate = PropertyRoomRate::where('property_id', $params['property_id'])
                ->where('name', $params['name'])
                ->where('id', '!=' ,$params['id'])
                ->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"], 'id' => $params["id"] ]);
        return $this->make($params, $this->rules($params), $this->messages());
    }

}
