package de.effigenix.infrastructure.shared.web;

import de.effigenix.application.shared.ListCountries;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/countries")
@SecurityRequirement(name = "Bearer Authentication")
@Tag(name = "Countries", description = "ISO 3166-1 country reference data")
public class CountryController {

    private final ListCountries listCountries;

    public CountryController(ListCountries listCountries) {
        this.listCountries = listCountries;
    }

    public record CountryResponse(String code, String name) {}

    @GetMapping
    public ResponseEntity<List<CountryResponse>> search(
            @RequestParam(name = "q", required = false, defaultValue = "") String query) {
        var countries = listCountries.execute(query);
        var response = countries.stream()
                .map(c -> new CountryResponse(c.code(), c.name()))
                .toList();
        return ResponseEntity.ok(response);
    }
}
