<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\ClothingOccasion;
use App\Enums\ClothingSeason;
use App\Enums\ClothingType;
use App\Enums\DietaryRestriction;
use App\Enums\FoodCategory;
use App\Enums\MealType;
use App\Enums\StylePreference;
use App\Enums\WeightUnit;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class EnumController extends Controller
{
    /** @var array<string, class-string<FoodCategory|WeightUnit|ClothingType|ClothingSeason|ClothingOccasion|MealType|DietaryRestriction|StylePreference>> */
    private const array ENUM_MAP = [
        'food-categories' => FoodCategory::class,
        'weight-units' => WeightUnit::class,
        'clothing-types' => ClothingType::class,
        'clothing-seasons' => ClothingSeason::class,
        'clothing-occasions' => ClothingOccasion::class,
        'meal-types' => MealType::class,
        'dietary-restrictions' => DietaryRestriction::class,
        'style-preferences' => StylePreference::class,
    ];

    public function index(): JsonResponse
    {
        $result = [];

        foreach (self::ENUM_MAP as $type => $enumClass) {
            $result[$type] = self::formatCases($enumClass::cases());
        }

        return response()->json($result);
    }

    public function show(string $type): JsonResponse
    {
        if (! isset(self::ENUM_MAP[$type])) {
            abort(404, "Unknown enum type: {$type}");
        }

        return response()->json(self::formatCases(self::ENUM_MAP[$type]::cases()));
    }

    /**
     * @param  array<FoodCategory|WeightUnit|ClothingType|ClothingSeason|ClothingOccasion|MealType|DietaryRestriction|StylePreference>  $cases
     * @return list<array{value: string, label: string}>
     */
    private static function formatCases(array $cases): array
    {
        $result = [];

        foreach ($cases as $case) {
            $result[] = [
                'value' => $case->value,
                'label' => $case->label(),
            ];
        }

        return $result;
    }
}
