"""
Simple test file for ai-qa-sdk
Run with: python test_ai_qa.py
"""

from ai_qa import validate, list_checks
from ai_qa.cli.formatters import format_json, format_table
import json

print("=" * 80)
print("AI-QA-SDK TEST FILE")
print("=" * 80)

# ============================================================================
# TEST 1: List all available checks
# ============================================================================
print("\n[TEST 1] Available Checks")
print("-" * 80)

checks = list_checks()
print(f"Total checks available: {len(checks)}\n")
for i, check in enumerate(checks, 1):
    print(f"  {i:2d}. {check}")

# ============================================================================
# TEST 2: Valid JSON Detection
# ============================================================================
print("\n[TEST 2] Valid JSON Detection")
print("-" * 80)

test_output = '{"name": "John Doe", "age": 30, "email": "john@example.com"}'
result = validate(
    input="Generate a JSON object for a user",
    output=test_output,
    checks=["json_format"]
)

print(f"Input:  'Generate a JSON object for a user'")
print(f"Output: {test_output}")
print(f"\nResult: {result.checks[0].name}")
print(f"  Score: {result.checks[0].score:.2f}")
print(f"  Status: {'✓ PASS' if result.checks[0].passed else '✗ FAIL'}")
print(f"  Evidence: {result.checks[0].evidence}")

# ============================================================================
# TEST 3: Invalid JSON Detection
# ============================================================================
print("\n[TEST 3] Invalid JSON Detection")
print("-" * 80)

invalid_output = '{"name": "John Doe", "age": 30, invalid}'
result = validate(
    input="Generate JSON",
    output=invalid_output,
    checks=["json_format"]
)

print(f"Input:  'Generate JSON'")
print(f"Output: {invalid_output}")
print(f"\nResult: {result.checks[0].name}")
print(f"  Score: {result.checks[0].score:.2f}")
print(f"  Status: {'✓ PASS' if result.checks[0].passed else '✗ FAIL'}")
print(f"  Evidence: {result.checks[0].evidence[:80]}...")

# ============================================================================
# TEST 4: Multiple Tests
# ============================================================================
print("\n[TEST 4] Multiple JSON Tests")
print("-" * 80)

test_cases = [
    ("Simple object", '{"key": "value"}'),
    ("Array", '[1, 2, 3, 4, 5]'),
    ("Nested", '{"user": {"name": "Alice", "age": 30}}'),
    ("Invalid", '{not json}'),
    ("Empty object", '{}'),
]

print(f"\n{'Test Case':<20} {'Status':<10} {'Score':<10}")
print("-" * 40)

for name, output in test_cases:
    result = validate(input="Test", output=output, checks=["json_format"])
    check = result.checks[0]
    status = "✓ PASS" if check.passed else "✗ FAIL"
    print(f"{name:<20} {status:<10} {check.score:<10.2f}")

# ============================================================================
# TEST 5: Detailed Check Information
# ============================================================================
print("\n[TEST 5] Detailed Check Information")
print("-" * 80)

result = validate(
    input="Convert to JSON",
    output='{"status": "success", "code": 200}',
    checks=["json_format"]
)

check = result.checks[0]
print(f"\nCheck: {check.name}")
print(f"  Score: {check.score:.2f} / 1.00")
print(f"  Threshold: {check.threshold:.2f}")
print(f"  Passed: {check.passed}")
print(f"  Evidence: {check.evidence}")
print(f"  Cost: ${check.cost_usd:.6f}")
print(f"  Tokens: {check.tokens_used}")
print(f"  Version: {check.check_version}")
print(f"  Cached: {check.cached}")

# ============================================================================
# TEST 6: JSON Export
# ============================================================================
print("\n[TEST 6] Export to JSON Format")
print("-" * 80)

result = validate(
    input="Test export",
    output='{"test": true}',
    checks=["json_format"]
)

json_export = format_json(result)
parsed = json.loads(json_export)

print(f"\nExported JSON Structure:")
print(f"  passed_rate: {parsed['passed_rate']:.2f}")
print(f"  total_checks: {parsed['total_checks']}")
print(f"  passed: {parsed['passed']}")
print(f"  failed: {parsed['failed']}")
print(f"  total_cost_usd: ${parsed['total_cost_usd']:.6f}")
print(f"  checks: {len(parsed['checks'])} check result(s)")

# ============================================================================
# TEST 7: Table Export
# ============================================================================
print("\n[TEST 7] Export to ASCII Table")
print("-" * 80)

result = validate(
    input="Table export test",
    output='{"data": [1,2,3]}',
    checks=["json_format"]
)

table_export = format_table(result)
print("\n" + table_export)

# ============================================================================
# TEST 8: HTML Export Sample
# ============================================================================
print("\n[TEST 8] Export Formats Available")
print("-" * 80)

from ai_qa.cli.formatters import format_html

result = validate(
    input="Export test",
    output='{"result": "success"}',
    checks=["json_format"]
)

html_export = format_html(result)
print(f"\nHTML Report Generated: {len(html_export)} characters")
print("  Includes: Summary, check details, styling, responsive design")
print("  Filename: Can save to .html file for viewing in browser")

# ============================================================================
# FINAL SUMMARY
# ============================================================================
print("\n" + "=" * 80)
print("✅ ALL TESTS COMPLETED SUCCESSFULLY!")
print("=" * 80)

summary_text = """
🎉 You've successfully tested ai-qa-sdk!

✓ Test 1:  Listed 22 available checks
✓ Test 2:  Validated valid JSON format
✓ Test 3:  Detected invalid JSON
✓ Test 4:  Ran 5 JSON test cases (4 valid, 1 invalid)
✓ Test 5:  Showed detailed check information
✓ Test 6:  Exported results to JSON format
✓ Test 7:  Exported results to ASCII table
✓ Test 8:  Generated HTML report

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

📦 INSTALLATION:

  # Basic install (deterministic checks only)
  pip install ai-qa-sdk
  
  # With Anthropic (Claude LLM)
  pip install ai-qa-sdk[anthropic]
  
  # With OpenAI (GPT LLM)
  pip install ai-qa-sdk[openai]
  
  # With all providers
  pip install ai-qa-sdk[all-providers]

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

🔑 SETUP (For LLM Checks):

  from ai_qa import configure, validate
  
  # Configure Anthropic
  configure(provider="anthropic", api_key="sk-ant-...")
  
  # Or configure OpenAI
  configure(provider="openai", api_key="sk-...")
  
  # Or use local (Ollama)
  configure(provider="local", base_url="http://localhost:11434")

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

🎯 COMMON USAGE:

  from ai_qa import validate
  
  # Simple validation
  result = validate(
      input="What is AI?",
      output="AI is artificial intelligence...",
      checks=["json_format"]  # or ["hallucination", "factuality"]
  )
  
  print(result.passed)  # True/False
  print(result.total_cost)  # $ spent
  
  for check in result.checks:
      print(f"{check.name}: {check.score}")

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

📊 AVAILABLE CHECKS (22 total):

  Deterministic (no API needed):
    ✓ json_format        - Validate JSON syntax
    ✓ regex              - Pattern matching
    ✓ schema             - JSON schema validation
    ✓ length             - Output length bounds
  
  LLM-Powered (need API key):
    ✓ hallucination      - Detects made-up facts
    ✓ factuality         - Checks factual accuracy
    ✓ relevance          - Relevance to input
    ✓ toxicity           - Harmful content detection
    ✓ bias               - Bias/fairness check
    ✓ pii                - Personal info detection
    ✓ consistency        - Internal consistency
    ✓ contradiction      - Logical contradictions
    ... and more!

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

🌐 RESOURCES:

  GitHub:  https://github.com/Nagam-Naidu/ai-qa-sdk
  PyPI:    https://pypi.org/project/ai-qa-sdk
  CLI:     ai-qa --help
  Docs:    Check README.md in repository

🚀 Ready to use! Questions? Check the GitHub issues or documentation.
"""

print(summary_text)
print("=" * 80)
