package de.avatic.taric.controller;

import de.avatic.taric.model.Geo;
import de.avatic.taric.model.GeoGroup;
import de.avatic.taric.service.GeoService;
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.Collections;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/v1/geo")
public class GeoController {


    private final GeoService geoService;

    public GeoController(GeoService geoService) {
        this.geoService = geoService;
    }

    @GetMapping("")
    public Optional<Geo> getGeo(@RequestParam String countryCode) {
        return geoService.getGeo(countryCode);
    }

    @GetMapping("/group")
    public List<GeoGroup> getGeoGroup(@RequestParam(required = false) String countryCode, @RequestParam(required = false) String abbr) {

        if (countryCode != null && abbr == null)
            return geoService.getGeoGroupByCountryCode(countryCode);

        if (countryCode == null && abbr != null)
            return geoService.getGeoGroup(abbr);

        return Collections.emptyList();
    }
}
