<?php

namespace App\Http\Controllers\Api\V1\BetProvider;

use App\Models\BetProvider;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

/** GET /api/v1/bet-providers — 玩家端可选开注商。 */
final class BetProviderIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $items = BetProvider::query()
            ->where('is_enabled', true)
            ->orderBy('sort_order')
            ->orderBy('id')
            ->get(['code', 'name', 'short_code', 'sort_order'])
            ->map(fn (BetProvider $provider): array => [
                'code' => $provider->code,
                'name' => $provider->name,
                'short_code' => $provider->short_code ?: strtoupper(substr((string) $provider->code, 0, 1)),
                'sort_order' => (int) $provider->sort_order,
                'is_default' => $provider->code === BetProvider::DEFAULT_CODE,
            ])
            ->values()
            ->all();

        return ApiResponse::success(['items' => $items]);
    }
}
