"""VVT template generator — creates VVT activity drafts per processing system."""


def generate_vvt_drafts(ctx: dict) -> list[dict]:
    """Generate VVT activity drafts, one per processing system.

    Args:
        ctx: Flat dict from company-profile/template-context

    Returns:
        List of VVT activity dicts ready for creation
    """
    systems = ctx.get("processing_systems", [])
    company = ctx.get("company_name", "Unbekannt")
    dpo = ctx.get("dpo_name", "")
    activities = []

    for i, system in enumerate(systems, 1):
        name = system.get("name", f"System {i}")
        vendor = system.get("vendor", "")
        hosting = system.get("hosting", "on-premise")
        categories = system.get("personal_data_categories", [])

        activity = {
            "vvt_id": f"VVT-AUTO-{i:03d}",
            "name": f"Verarbeitung in {name}",
            "description": f"Automatisch generierter VVT-Eintrag für das System '{name}'"
                           + (f" (Anbieter: {vendor})" if vendor else ""),
            "purposes": [f"Datenverarbeitung via {name}"],
            "legal_bases": ["Art. 6 Abs. 1b DSGVO — Vertragserfüllung"],
            "data_subject_categories": [],
            "personal_data_categories": categories,
            "recipient_categories": [vendor] if vendor else [],
            "third_country_transfers": _assess_third_country(hosting),
            "retention_period": {"default": "Gemäß Löschfristenkatalog"},
            "tom_description": f"Siehe TOM-Katalog für {name}",
            "business_function": "IT",
            "systems": [name],
            "deployment_model": hosting,
            "protection_level": "HIGH" if categories else "MEDIUM",
            "dpia_required": len(categories) > 3,
            "status": "DRAFT",
            "responsible": dpo or company,
        }
        activities.append(activity)

    return activities


def _assess_third_country(hosting: str) -> list:
    if hosting in ("us-cloud", "international"):
        return [{"country": "USA", "mechanism": "EU-US Data Privacy Framework"}]
    return []
