<?php

namespace App\Services;


class CaloryService
{

    public static function convertWeight($weight, $unit)
    {
        return $unit === 'pound' ? $weight * 0.453592 : $weight;
    }

    public static function convertHeight($height, $unit)
    {
        return $unit === 'inch' ? $height * 2.54 : $height;
    }

    public static function calculateCaloriesBasedOnFactors($request, $weight, $height)
    {
        $factor = self::getFactor($request->gender, $request->age_type, $request->physical_level);

        if ($request->gender === 'male') {
            $calories = self::calculateMaleCalories($request, $weight, $height, $factor);
            return self::getGoalCalories($request->goal ?? 'maintain_weight', $calories);
        }

        $calories = self::calculateFemaleCalories($request, $weight, $height, $factor);
        return self::getGoalCalories($request->goal ?? 'maintain_weight', $calories);
    }

    public static function calculateMaleCalories($request, $weight, $height, $factor)
    {
        if ($request->age_type === 'above_18') {
            return ((10 * $weight) + (6.25 * $height) - (5 * $request->age) + 5) * $factor;
        }

        return 88.5 - (61.9 * $request->age) + $factor * ((26.7 * $weight) + (903 * ($height / 100))) + 25;
    }

    public static function calculateFemaleCalories($request, $weight, $height, $factor)
    {
        if ($request->age_type === 'above_18_pregnant') {
            return self::calculatePregnantCalories($request, $weight, $height, $factor);
        }

        if ($request->age_type === 'above_18_lactating') {
            return self::calculateLactatingCalories($request, $weight, $height, $factor);
        }

        if ($request->age_type === 'above_18') {
            return ((10 * $weight) + (6.25 * $height) - (5 * $request->age) - 161) * $factor;
        }

        return 135.3 - (30.8 * $request->age) + $factor * ((10 * $weight) + (934 * ($height / 100))) + 25;
    }

    public static function calculatePregnantCalories($request, $weight, $height, $factor)
    {
        $baseCalories = 354 - (6.91 * $request->age) + $factor * ((9.36 * $weight) + (726 * ($height / 100)));

        switch ($request->pregnancy) {
            case 'under_3':
                return $baseCalories;
            case 'under_6':
                return $baseCalories + 340;
            case 'under_9':
                return $baseCalories + 452;
        }
    }

    public static function calculateLactatingCalories($request, $weight, $height, $factor)
    {
        $baseCalories = 354 - (6.91 * $request->age) + $factor * ((9.36 * $weight) + (726 * ($height / 100)));

        return $request->lactating === 'under_6' ? $baseCalories + 330 : $baseCalories + 400;
    }

    public static function calculateMacros($calories, $carb, $fat, $protein, $macros)
    {
        return [
            'carb' => (($calories / 100) * $carb) / 4,
            'fat' => (($calories / 100) * $fat) / 9,
            'protein' => (($calories / 100) * $protein) / 4,
            'calories' => $calories,
            'macros'   => $macros,
        ];
    }

    public static function getGoalCalories($goal, $calories)
    {
        switch ($goal) {
            case 'loss_weight':
                return $calories - ($calories * 0.14);
            case 'gain_weight':
                return $calories + ($calories * 0.14);
            case 'maintain_weight':
                return $calories;
            default:
                return $calories;
        }
    }

    public static function getFactor($gender, $age_type, $physical_level)
    {
        $factors = [
            'above_18' => [
                'secondary' => 1.2,
                'low_active' => 1.375,
                'active' => 1.55,
                'extra_active' => 1.725,
                'intense_active' => 1.9,
            ],
            'under_18' => [
                'male' => [
                    'secondary' => 1,
                    'low_active' => 1.13,
                    'active' => 1.26,
                    'extra_active' => 1.42,
                    'intense_active' => 1.9,
                ],
                'female' => [
                    'secondary' => 1,
                    'low_active' => 1.16,
                    'active' => 1.31,
                    'extra_active' => 1.56,
                ],
            ],
            'above_18_pregnant' => [
                'secondary' => 1,
                'low_active' => 1.12,
                'active' => 1.27,
                'extra_active' => 1.45,
            ],
            'above_18_lactating' => [
                'secondary' => 1,
                'low_active' => 1.12,
                'active' => 1.27,
                'extra_active' => 1.45,
            ],
        ];

        return $factors[$age_type][$gender][$physical_level] ?? $factors[$age_type][$physical_level];
    }
}
