package de.avatic.lcc.model.taric;

import java.util.List;
import java.util.Map;

/**
 * Wrapper for the TARIC API tariff response
 * Structure: { "measureType": { "additionalCode": [ AppliedMeasure... ] } }
 */
public class TariffResponse {

    // Outer map: MeasureType -> Inner map
    // Inner map: AdditionalCode -> List of AppliedMeasures
    private Map<String, Map<String, List<AppliedMeasure>>> measures;

    public TariffResponse() {
    }

    public TariffResponse(Map<String, Map<String, List<AppliedMeasure>>> measures) {
        this.measures = measures;
    }

    public Map<String, Map<String, List<AppliedMeasure>>> getMeasures() {
        return measures;
    }

    public void setMeasures(Map<String, Map<String, List<AppliedMeasure>>> measures) {
        this.measures = measures;
    }

    /**
     * Get all measures as a flat list
     */
    public List<AppliedMeasure> getAllMeasures() {
        if (measures == null) {
            return List.of();
        }

        return measures.values().stream()
                .flatMap(innerMap -> innerMap.values().stream())
                .flatMap(List::stream)
                .toList();
    }

    /**
     * Get measures by measure type
     */
    public List<AppliedMeasure> getMeasuresByType(String measureType) {
        if (measures == null || !measures.containsKey(measureType)) {
            return List.of();
        }

        return measures.get(measureType).values().stream()
                .flatMap(List::stream)
                .toList();
    }

    /**
     * Get measures by measure type and additional code
     */
    public List<AppliedMeasure> getMeasuresByTypeAndCode(String measureType, String additionalCode) {
        if (measures == null || !measures.containsKey(measureType)) {
            return List.of();
        }

        Map<String, List<AppliedMeasure>> innerMap = measures.get(measureType);
        return innerMap.getOrDefault(additionalCode, List.of());
    }
}