package com.agent.management.rag.structured;

import java.util.List;

public record StructuredRelationship(
        RelationshipType type,
        String sourceTable,
        String sourceColumn,
        String targetTable,
        String targetColumn,
        double confidence,
        List<Condition> conditions,
        List<String> evidence,
        List<String> warnings) {

    public StructuredRelationship {
        conditions = List.copyOf(conditions == null ? List.of() : conditions);
        evidence = List.copyOf(evidence == null ? List.of() : evidence);
        warnings = List.copyOf(warnings == null ? List.of() : warnings);
    }

    public boolean usableForSelection() {
        return type != RelationshipType.AMBIGUOUS && confidence >= 0.78;
    }

    public String id() {
        String condition = conditions.stream().map(item -> item.table() + "." + item.column()
                + item.operator() + String.valueOf(item.value())).reduce((a, b) -> a + "&" + b).orElse("");
        return type + ":" + sourceTable + "." + sourceColumn + "->" + targetTable + "." + targetColumn
                + (condition.isBlank() ? "" : "[" + condition + "]");
    }

    public enum RelationshipType { PHYSICAL_FK, INFERRED, CONDITIONAL, AMBIGUOUS }

    public record Condition(String table, String column, String operator, Object value) {}
}
