package de.avatic.taric.controller;

import de.avatic.taric.model.AppliedMeasure;
import de.avatic.taric.service.TariffService;
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.ExampleObject;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/tariff")
@Tag(name = "Tariff", description = "API endpoints for calculating and retrieving applied tariff rates and measures")
public class TariffController {

    private final TariffService tariffService;

    public TariffController(TariffService tariffService) {
        this.tariffService = tariffService;
    }

    @Operation(
            summary = "Get applied tariff measures",
            description = "Retrieves all applicable tariff measures for a specific HS code and country combination. " +
                    "Returns a nested map structure grouping measures by measure type and additional code."
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved applied measures",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Map.class),
                            examples = @ExampleObject(
                                    value = "{ \"103\": { \"default\": [ {...} ] }, \"142\": { \"C999\": [ {...} ] } }"
                            )
                    )
            ),
            @ApiResponse(
                    responseCode = "404",
                    description = "HS code or country not found",
                    content = @Content
            )
    })
    @GetMapping("")
    public Map<String, Map<String, List<AppliedMeasure>>> getTariffRate(
            @Parameter(description = "Harmonized System code (e.g., 0101210000)", required = true, example = "0101210000")
            @RequestParam String hsCode,
            @Parameter(description = "ISO country code (e.g., US, CN, GB)", required = true, example = "US")
            @RequestParam String countryCode) {
        return tariffService.getAppliedMeasures(hsCode, countryCode);
    }
}