<?php

namespace App\Http\Requests\Api\App\Calory;

use App\Http\Requests\Api\ApiMasterRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class CalculateCaloriesRequest 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()
    {
        if ($this->has(['carb', 'fat', 'protein']) && ($this->carb + $this->fat + $this->protein != 100)) {
            throw new HttpResponseException(response()->json([
            'status' => 'fail',
            'message' => trans('please, recalculate your macros'),
            'data' => null,
            ], 422));
        }

        $age_type = 'required_if:know_calories,false|in:under_18,above_18';
        $goal = 'required_if:know_calories,false|in:maintain_weight,loss_weight,gain_weight';
        $pregnancy = 'nullable';
        $lactating = 'nullable';
        $macros = 'required|in:default,keto,high_protein,low_carb,custom';

        if($this->gender == 'female'){
            $age_type = 'required_if:know_calories,false|in:under_18,above_18,above_18_pregnant,above_18_lactating';
            $pregnancy = 'required_if:age_type,above_18_pregnant|in:under_3,under_6,under_9';
            $lactating = 'required_if:age_type,above_18_lactating|in:under_6,above_6';

            if($this->age_type == 'above_18_pregnant') $goal = 'required_if:age_type,above_18_pregnant|in:monitoring,maintain_weight';
            

            if($this->age_type == 'above_18_pregnant' || $this->age_type == 'above_18_lactating') $macros = 'required|in:default,custom';
            
        }

        return [
            'gender'           => 'required|in:male,female',
            'know_calories'    => 'required|boolean',
            'age_type'         => $age_type,
            'age'              => 'required_if:know_calories,false|numeric|min:0|max:100',
            'pregnancy'        => $pregnancy,
            'lactating'        => $lactating,
            'weight'           => 'required_if:know_calories,false|numeric|min:0|max:500',
            'weight_by'        => 'required_if:know_calories,false|in:kg,pound',
            'hight'            => 'required_if:know_calories,false|numeric|min:0|max:500',
            'hight_by'         => 'required_if:know_calories,false|in:cm,inch',
            'physical_level'   => 'required_if:know_calories,false|in:secondary,low_active,active,extra_active,intense_active',
            'goal'             => $goal,
            'calories'         => 'required_if:know_calories,true|numeric|min:0',
            'macros'           => $macros,
            'carb'             => 'required_if:macros,custom|numeric|min:0|max:100',
            'fat'              => 'required_if:macros,custom|numeric|min:0|max:100',
            'protein'          => 'required_if:macros,custom|numeric|min:0|max:100',
            'factor'           => 'nullable',
        ];
    }

    public function getValidatorInstance()
    {
        $data = $this->all();

        if (isset($data['macros']) && $data['macros'] && $data['macros'] == 'default') {
            $data['carb'] = 50;
            $data['fat'] = 20;
            $data['protein'] = 30;
        }elseif(isset($data['macros']) && $data['macros'] && $data['macros'] == 'keto'){
            $data['carb'] = 5;
            $data['fat'] = 25;
            $data['protein'] = 70;
        }elseif(isset($data['macros']) && $data['macros'] && $data['macros'] == 'high_protein'){
            $data['carb'] = 25;
            $data['fat'] = 40;
            $data['protein'] = 35;
        }elseif(isset($data['macros']) && $data['macros'] && $data['macros'] == 'low_carb'){
            $data['carb'] = 20;
            $data['fat'] = 40;
            $data['protein'] = 40;
        }

       $this->getInputSource()->replace($data);
       return parent::getValidatorInstance();
    }

    
}
