<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class EnumController extends Controller
{
    use ApiResponseTrait;

    private const ENUM_MAP = [
        'member_status'           => \App\Enums\MemberStatusEnum::class,
        'booking_status'          => \App\Enums\BookingStatusEnum::class,
        'status'                  => \App\Enums\StatusEnum::class,
        'booking_participant_status' => \App\Enums\BookingParticipantStatusEnum::class,
        'notification_type'      => \App\Enums\NotificationTypeEnum::class,
        'member_type'             => \App\Enums\MemberTypeEnum::class,
        'qr_code_status'          => \App\Enums\QrCodeStatusEnum::class,
        'gender'                  => \App\Enums\GenderEnum::class,
        'contract_request_type'   => \App\Enums\ContractRequestTypeEnum::class,
        'relation'                => \App\Enums\RelationEnum::class,
        'person_gender'           => \App\Enums\PersonGenderEnum::class,
        'required_attachment'     => \App\Enums\RequiredAttachmentEnum::class,
        'day_of_week'             => \App\Enums\DayOfWeekEnum::class,
        'championship_status'     => \App\Enums\ChampionshipStatusEnum::class,
        'championship_match_stage' => \App\Enums\ChampionshipMatchStageEnum::class,
        'championship_match_status' => \App\Enums\ChampionshipMatchStatusEnum::class,
    ];


    public function index(Request $request): JsonResponse
    {
        $data = [];
        foreach (self::ENUM_MAP as $key => $class) {
            $data[$key] = $this->enumToOptions($class);
        }
        return $this->ApiResponse([
            'enums' => $data,
        ], __('Success'), 200);
    }

    
    public function show(Request $request, string $name): JsonResponse
    {
        $key = str_replace('-', '_', $name);
        if (! isset(self::ENUM_MAP[$key])) {
            return $this->errorResponse(__('Enum not found.'), 404);
        }
        $class = self::ENUM_MAP[$key];
        $options = $this->enumToOptions($class);
        return $this->ApiResponse([
            'name'    => $key,
            'options' => $options,
        ], __('Success'), 200);
    }

    private function enumToOptions(string $class): array
    {
        $options = method_exists($class, 'options') ? $class::options() : $this->buildOptionsFromCases($class);
        $hasColor = method_exists($class, 'color');
        if (! $hasColor) {
            return $options;
        }
        return array_map(function ($item) use ($class) {
            $item['color'] = $class::color($item['value']);
            return $item;
        }, $options);
    }

    private function buildOptionsFromCases(string $class): array
    {
        $cases = $class::cases();
        $labels = method_exists($class, 'labels') ? $class::labels() : [];
        $result = [];
        foreach ($cases as $case) {
            $result[] = [
                'value' => $case->value,
                'label' => $labels[$case->value] ?? (string) $case->value,
            ];
        }
        return $result;
    }
}
