package de.avatic.taric.controller;

import de.avatic.taric.model.Nomenclature;
import de.avatic.taric.service.NomenclatureService;
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.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/v1/nomenclature")
@Tag(name = "Nomenclature", description = "API endpoints for HS code nomenclature and tariff classification hierarchy")
public class NomenclatureController {

    private final NomenclatureService nomenclatureService;

    public NomenclatureController(NomenclatureService nomenclatureService) {
        this.nomenclatureService = nomenclatureService;
    }

    @Operation(
            summary = "Get nomenclature by HS code",
            description = "Retrieves a specific nomenclature entry by its Harmonized System (HS) code"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved nomenclature",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = Nomenclature.class))
            ),
            @ApiResponse(
                    responseCode = "404",
                    description = "HS code not found",
                    content = @Content
            )
    })
    @GetMapping("")
    public Optional<Nomenclature> getNomenclature(
            @Parameter(description = "Harmonized System code (e.g., 0101, 010121)", required = true)
            @RequestParam String hscode) {
        return nomenclatureService.getNomenclature(hscode);
    }

    @Operation(
            summary = "Get declarable child nomenclatures",
            description = "Retrieves all declarable (leaf-level) child nomenclatures under a given HS code. These are codes that can be used for customs declarations."
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved declarable nomenclatures",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = Nomenclature.class))
            )
    })
    @GetMapping("/declarable")
    public List<Nomenclature> getDeclarable(
            @Parameter(description = "Parent HS code to retrieve declarable children from", required = true)
            @RequestParam String hscode) {
        return nomenclatureService.getDeclarableChildren(hscode);
    }

    @Operation(
            summary = "Get nomenclature cascade",
            description = "Retrieves the complete hierarchical cascade of a nomenclature, including all parent and child relationships in the tariff classification tree"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Successfully retrieved nomenclature cascade",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = Nomenclature.class))
            )
    })
    @GetMapping("/cascade")
    public List<Nomenclature> getCascade(
            @Parameter(description = "HS code to retrieve the complete hierarchy for", required = true)
            @RequestParam String hscode) {
        var found = nomenclatureService.getNomenclatureCascade(hscode);
        return found;
    }
}