<?php

namespace App\Core\Validator\PropertyPlace;

use App\Models\PropertyPhoto;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use App\Models\PropertyPlace;

use App\Core\Validator\BaseValidator;

use Exception;

class PropertyPlacePhotoMappingAddValidator extends BaseValidator
{


    public function __construct
    (

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

    }

    public function rules($params = null)
    {
        return
            [
                'property_id' => 'required|numeric',
                'place_id' => 'required|numeric',
                "checkPropertyPlace" => "accepted",
                "checkPropertyPhoto" => "accepted",
                'property_place_photo' => 'required|array',
                'property_place_photo.*.id' => 'required|numeric',
                'property_place_photo.*.is_selected' => 'required|boolean',
            ];


    }

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

    public function checkPropertyPlace($params)
    {
        try {
            $placeData = PropertyPlace::where('property_id', $params['property_id'])
                ->where('id', $params['place_id'])
                ->first();

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

    public function checkPropertyPhoto($params)
    {
        try {

            $photos = collect($params['property_place_photo'])->keyBy('id')->keys()->toArray();
            $arrayCount = collect($params['property_place_photo'])->count();
            $photoData = PropertyPhoto::where('property_id', $params['property_id'])
                ->whereIn('id', $photos)
                ->count();
            return $arrayCount == $photoData ? true : false;
        } catch (Exception $e) {
            return false;
        }
    }



    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["checkPropertyPlace"] = $this->checkPropertyPlace(['property_id' => $params["property_id"], 'place_id' => $params["place_id"] ]);
        $params["checkPropertyPhoto"] = $this->checkPropertyPhoto(['property_id' => $params["property_id"], 'property_place_photo' => $params["property_place_photo"] ]);
        return $this->make($params, $this->rules($params), $this->messages());
    }

}
