<?php

namespace App\Http\Controllers\Api\V1\Admin\Currency;

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

final class AdminCurrencyIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $items = Currency::query()
            ->orderByDesc('is_enabled')
            ->orderByDesc('is_bettable')
            ->orderBy('code')
            ->get()
            ->map(fn (Currency $currency): array => $this->serializeCurrency($currency))
            ->all();

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

    /** @return array<string, mixed> */
    private function serializeCurrency(Currency $currency): array
    {
        return [
            'id' => (int) $currency->id,
            'code' => $currency->code,
            'name' => $currency->name,
            'decimal_places' => (int) $currency->decimal_places,
            'is_enabled' => (bool) $currency->is_enabled,
            'is_bettable' => (bool) $currency->is_bettable,
            'created_at' => $currency->created_at?->toIso8601String(),
            'updated_at' => $currency->updated_at?->toIso8601String(),
        ];
    }
}
