package fr.titionfire.ffsaf.rest;

import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.openapi.annotations.Operation;

import java.util.HashMap;
import java.util.Locale;

@Path("api/countries")
public class CountriesEndpoints {

    @GET
    @Path("/{lang}/{code}")
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(hidden = true)
    public Uni<HashMap<String, String>> getCountries(@PathParam("lang") String lang, @PathParam("code") String code) {
        Locale locale = new Locale(lang, code);
        return Uni.createFrom().item(new HashMap<String, String>())
                .invoke(map -> {
                    String[] locales = Locale.getISOCountries();
                    for (String countryCode : locales) {
                        if (countryCode.equals("AN"))
                            continue;
                        Locale obj = new Locale("", countryCode);
                        map.put(countryCode, obj.getDisplayName(locale));
                    }
                });
    }
}
