<?php

namespace App\Core\Validator\PropertyPersonPricingPolicy;

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

use App\Core\Validator\BaseValidator;

use Exception;

class PropertyPricingPolicyAdultUpdateValidator extends BaseValidator
{


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

    }

    public function rules($params = null)
    {
        return
            [
                'pricing_policy_id' => 'required|numeric',
                'name' => 'nullable',
                'adult_action_type' => 'required|in:DEC,INC',
                'adult' => 'required|numeric',
                'action_type' => 'required|in:DEC,INC',
                'type' => 'required|in:PER,FIX',
                'value' => 'required|numeric',
                'checkData' => 'accepted',
             //   "checkPricingName" => "accepted"
            ];


    }


    public function checkData($params){

        $data = PropertyPricingPolicyAdult::where('id' , '=', $params['pricing_policy_id'])
            ->where('property_id' , '=', $params['property_id'])
            ->first();
        return $data ? true : false ;
    }

    public function checkPricingName($params)
    {
        try {
            $isExistPricingName = PropertyPricingPolicyAdult::where('property_id', $params['property_id'])
                ->where('name', $params['name'])
                ->first();

            return $isExistPricingName && $isExistPricingName['id'] !== $params['pricing_policy_id']  ? false : true;

        } catch (Exception $e) {
            return false;
        }
    }

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

    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["checkData"] = $this->checkData($params);
       /* $params["checkPricingName"] = $this->checkPricingName([
            'property_id' => $params["property_id"],
            'name' => $params["name"],
            'pricing_policy_id' => $params['pricing_policy_id']
        ]);*/

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

}
