import os
import json
from typing import Any, Dict

def generate_teaser(scan_data: Dict[str, Any]) -> str:
    """
    Generates a 'Teaser' Markdown draft with placeholders.
    This is the 'Public' version of the report.
    """
    libs = scan_data.get("annex_iv_technical_documentation", {}) \
                    .get("section_2_b_design_specifications", {}) \
                    .get("general_logic", {}) \
                    .get("detected_libraries", [])
    
    risks = scan_data.get("annex_iv_technical_documentation", {}) \
                     .get("section_2_b_design_specifications", {}) \
                     .get("general_logic", {}) \
                     .get("risk_classification_detected", [])
    
    version = scan_data.get("project_metadata", {}).get("version", "Unknown")

    # Basic Heuristics for "Intended Purpose" (Dumb Logic)
    intended = "[USER INPUT REQUIRED]"
    if any(l.startswith("face_recognition") or l.startswith("dlib") or l.startswith("cv2") for l in libs):
        intended = "Biometric identification (facial recognition) [CONFIRMATION REQUIRED]"
    elif any("sklearn" in l or "spacy" in l or "nltk" in l for l in libs):
        intended = "General ML / Text processing [CONFIRMATION REQUIRED]"

    comps = "\n".join(f"- {l}" for l in libs) if libs else "- None detected"
    risk_line = risks[0] if risks else "Low Risk / No Annex III match detected"

    draft = f"""# Annex IV Technical Documentation (DRAFT)
**Generated by ai-act-check (Open Source)**

## Section 1: System Identification
- **Project Version**: {version}
- **Provider Name**: [USER INPUT REQUIRED]
- **Date**: [CURRENT DATE]

## Section 2(b): Design Specifications

### Intended Purpose
The system is intended for: {intended}.

> **NOTE**: The specific intended purpose is critical for Article 6 classification. Please verify if this falls under Annex III High-Risk use cases.

### Software Components Detected
{comps}

### Risk Determination (Preliminary)
Based on the detected software components, the assessed classification is: **{risk_line}**.

---
**Want a full legal analysis?**
Upgrade to the Sovereign Platform to generate a certified, digitally signed PDF with:
- Full Article 6 & Article 27 Analysis
- Conformity Assessment Procedures
- Automatic Updates
- Liability Shield

[Get Certified](https://sovereign-code.eu)
"""
    return draft

if __name__ == "__main__":
    # quick smoke test
    sample = {
        "annex_iv_technical_documentation": {
            "section_2_b_design_specifications": {
                "general_logic": {
                    "detected_libraries": ["face_recognition.api", "sklearn", "cv2", "dlib"],
                    "risk_classification_detected": ["High Risk: Biometrics (Annex III.1)"]
                }
            }
        },
        "project_metadata": {"version": "1.0.0"}
    }
    print(generate_teaser(sample))