<?php

namespace App\Http\Requests\Api\Dashboard\Admin\Product;

use App\Http\Requests\Api\ApiMasterRequest;
use Illuminate\Validation\Rule;

class ProductRequest extends ApiMasterRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        $required = isset($this->product) ? 'nullable' : 'required';
        $diet_required = 'nullable';
        if(isset($this->date) || isset($this->meal_id) || isset($this->yields)){
            $diet_required = 'required';
        }
        // $organic_required = $this->is_organic == 1 ? 'required' : 'nullable';
        $rules = [
            'base_image'                    => $required . '|string',
            'other_images'                  => $required . '|array',
            'is_organic'                    => 'required|in:0,1',
            'serving_size'                  => $required . '|in:cup,gram',
            'size_of_cup'                   => 'required_if:serving_size,cup|numeric|min:1',
            'calories'                      => $required . '|numeric|min:1',
            'calories_per'                  => $required . '|numeric|min:1',
            'yields'                        => $diet_required . '|string',
            'date'                          => $diet_required . '|date_format:Y-m-d',
            'total_protein'                 => 'required|numeric|min:1',
            'meal_id'                       => $diet_required . '|exists:meals,id,user_id,' . auth('api')->id(),
            'ingredients'                   => 'nullable|array|min:1',
            'ingredients.*.ingredient_id'   => 'nullable|exists:products,id,type,ingredient',
            'ingredients.*.size'            => 'nullable|numeric|min:1',
            'ingredients.*.unit'            => 'nullable|in:gram,cup',
            'carbs'                         => 'nullable|array',
            'carbs.total_carbs'             => 'required|numeric|min:0',
            'carbs.sugar'                   => 'nullable|numeric|min:0',
            'carbs.fiber'                   => 'nullable|numeric|min:0',
            'carbs.added_sugar'             => 'nullable|numeric|min:0',
            'carbs.sugar_alcohol'           => 'nullable|numeric|min:0',
            'carbs.glycerin'                => 'nullable|numeric|min:0',
            'carbs.erythritol'              => 'nullable|numeric|min:0',
            'carbs.polyols'                 => 'nullable|numeric|min:0',
            'fats'                          => 'nullable|array',
            'fats.total_fat'                => 'required|numeric|min:0',
            'fats.saturated_fat'            => 'nullable|numeric|min:0',
            'fats.trans_fat'                => 'nullable|numeric|min:0',
            'fats.monounsaturated_fat'      => 'nullable|numeric|min:0',
            'fats.polyunsaturated_fat'      => 'nullable|numeric|min:0',
            'others'                        => 'nullable|array',
            'others.sodium'                 => 'nullable|numeric|min:0',
            'others.salt'                   => 'nullable|numeric|min:0',
            'others.cholesterol'            => 'nullable|numeric|min:0',
            'micros'                        => 'nullable|array',
            'micros.calcium'                => 'nullable|numeric|min:0',
            'micros.iron'                   => 'nullable|numeric|min:0',
            'micros.potassium'              => 'nullable|numeric|min:0',
            'micros.vitamin_d'              => 'nullable|numeric|min:0',
            // 'directions'                    => 'nullable|array|min:1',

        ];
        // foreach (config('translatable.locales') as $locale) {
        //     $rules['directions.*.' . $locale . '.desc'] = $required . '|string';
        // }
        foreach (config('translatable.locales') as $locale) {
            $rules[$locale . '.desc']           = 'nullable|string|between:3,1000';
            $rules[$locale . '.serving_desc']   = 'nullable|string|between:3,1000';
            $rules[$locale . '.brand']          = 'required|string|between:3,190';
            $rules[$locale . '.title']          = [
                'required',
                'between:3,190',
            ];
        }
        return $rules;
    }

    
}
