package de.avatic.taric.controller;

import de.avatic.taric.model.Geo;
import de.avatic.taric.model.GeoGroup;
import de.avatic.taric.service.GeoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
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")
@Tag(name = "Geography", description = "API endpoints for geographical data including countries and country groups")
public class GeoController {

    private final GeoService geoService;

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

    @Operation(
            summary = "Get geographical information by country code",
            description = "Retrieves detailed geographical information for a specific country using its country code"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved geographical information",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = Geo.class))
            ),
            @ApiResponse(
                    responseCode = "404",
                    description = "Country not found",
                    content = @Content
            )
    })
    @GetMapping("")
    public Optional<Geo> getGeo(
            @Parameter(description = "ISO country code (e.g., DE, US, FR)", required = true)
            @RequestParam String countryCode) {
        return geoService.getGeo(countryCode);
    }

    @Operation(
            summary = "Get geographical groups",
            description = "Retrieves geographical groups either by country code or by group abbreviation. Only one parameter should be provided at a time."
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved geographical groups",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = GeoGroup.class))
            )
    })
    @GetMapping("/group")
    public List<GeoGroup> getGeoGroup(
            @Parameter(description = "ISO country code to find all groups containing this country")
            @RequestParam(required = false) String countryCode,
            @Parameter(description = "Group abbreviation to retrieve a specific geographical group")
            @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();
    }
}