#!/bin/bash
# test-ai-act-output.sh — Validate ai-act-assessor agent output structure
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/lib/e2e-helpers.sh"

FIXTURE="$SCRIPT_DIR/fixtures/ai-act/fixture.md"
FIXTURE_HR="$SCRIPT_DIR/fixtures/ai-act/fixture-high-risk.md"

if [ ! -f "$FIXTURE" ]; then
  echo "ERROR: Fixture not found: $FIXTURE"
  echo "Run: bash tests/capture-fixture.sh to generate fixtures"
  exit 1
fi

if [ ! -f "$FIXTURE_HR" ]; then
  echo "ERROR: High-risk fixture not found: $FIXTURE_HR"
  exit 1
fi

init_suite "AI Act Assessor Agent"

# === Minimal risk fixture ===
echo ""
echo "  --- Minimal Risk Fixture ---"

# Structure checks
assert_has_section "$FIXTURE" "Risikoklassifisering" "Has risk classification section"
assert_has_section "$FIXTURE" "Rolle" "Has role section"
assert_has_section "$FIXTURE" "Forpliktelser" "Has obligations section"

# Content quality
assert_min_lines "$FIXTURE" 20 "Minimum 20 lines"

# Encoding
assert_encoding_ok "$FIXTURE" "UTF-8 encoding valid (minimal)"
assert_no_ascii_approximation "$FIXTURE" "No ASCII approximation (minimal)"

# Domain-specific: classification result
assert_matches_pattern "$FIXTURE" "(Minimal risiko|Begrenset risiko|Høyrisiko|Forbudt)" "Contains risk level classification"
assert_matches_pattern "$FIXTURE" "(Provider|Deployer)" "Contains role determination"

# Domain-specific: article references
assert_matches_pattern "$FIXTURE" "Art\. [0-9]" "References specific AI Act articles"

# === High risk fixture ===
echo ""
echo "  --- High Risk Fixture ---"

# Structure checks
assert_has_section "$FIXTURE_HR" "Risikoklassifisering" "HR: Has risk classification section"
assert_has_section "$FIXTURE_HR" "Rolle" "HR: Has role section"
assert_has_section "$FIXTURE_HR" "Forpliktelser" "HR: Has obligations section"
assert_has_section "$FIXTURE_HR" "Tiltaksplan" "HR: Has action plan section"
assert_has_section "$FIXTURE_HR" "Neste steg" "HR: Has next steps section"
assert_has_section "$FIXTURE_HR" "Viktige frister" "HR: Has deadline section"

# Content quality
assert_min_lines "$FIXTURE_HR" 40 "HR: Minimum 40 lines"
assert_min_tables "$FIXTURE_HR" 1 "HR: Has at least 1 table"

# Encoding
assert_encoding_ok "$FIXTURE_HR" "HR: UTF-8 encoding valid"
assert_no_ascii_approximation "$FIXTURE_HR" "HR: No ASCII approximation"

# Domain-specific: high-risk classification
assert_matches_pattern "$FIXTURE_HR" "Høyrisiko" "HR: Classified as high-risk"
assert_matches_pattern "$FIXTURE_HR" "Annex III" "HR: References Annex III"
assert_matches_pattern "$FIXTURE_HR" "FRIA" "HR: References FRIA requirement"

# Domain-specific: deadlines
assert_matches_pattern "$FIXTURE_HR" "202[5-7]-[0-9]{2}-[0-9]{2}" "HR: Contains compliance deadline dates"

# Domain-specific: article references
assert_matches_pattern "$FIXTURE_HR" "Art\. (26|27|9|13|14|50)" "HR: References key deployer/provider articles"

# Domain-specific: action plan
assert_matches_pattern "$FIXTURE_HR" "(Kritisk|Høy|Middels|Lav)" "HR: Action plan has priority levels"

print_summary
