<?php

namespace App\Core\Validator\PropertyPlace;

use App\Models\PropertyPlace;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Exception;
use App\Core\Validator\BaseValidator;

class PropertyPlaceUpdateValidator extends BaseValidator
{

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

    public function rules($params = null)
    {
        return
            [
                "place_category_id" => 'required|numeric',
                "place_working_hour_id" => 'required|numeric',
                "name" => 'required|min:1|max:50',
                "reservation_requirement" => "required|boolean",
                "include" => "nullable|numeric",
                "hasName" => "accepted",

            ];
    }

    public function messages()
    {
        $thisMessages =
            [
                "hasName.accepted" => "This name added before",
            ];
        return array_merge(parent::messages() , $thisMessages);
    }


    public function hasName($params)
    {
        try {
            $place = PropertyPlace::where('property_id', $params['property_id'])
                ->where('name', $params['name'])
                ->where('id', "!=", $params['property_place_id'])
                ->first();
            return $place ? false : true;
        } catch (Exception $e) {
            return false;
        }
    }



    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["hasName"] = $this->hasName($params);
        return $this->make($params, $this->rules($params), $this->messages());
    }

}
