ADVANCED_SKILLS = {
    "docker", "kubernetes", "aws", "gcp", "azure", "machine learning",
    "deep learning", "tensorflow", "pytorch", "graphql", "redis", "kafka",
    "microservices", "ci/cd", "system design",
}

FRONTEND_SKILLS = {"html", "css", "react", "vue", "angular", "javascript", "typescript"}
BACKEND_SKILLS = {"node.js", "python", "django", "flask", "express", "java", "spring", "go"}
DATA_SKILLS = {"python", "pandas", "numpy", "sql", "machine learning", "tensorflow", "pytorch"}
DESIGN_SKILLS = {"figma", "photoshop", "ui/ux", "illustrator", "blender"}

def compute_hackathon_score(parsed: dict) -> dict:
    skills = [s.lower() for s in parsed.get("skills", [])]
    projects = parsed.get("projects", [])
    certifications = parsed.get("certifications", [])

    # Technical skills score (40 pts)
    breadth = min(len(skills), 10) / 10 * 20
    advanced_count = sum(1 for s in skills if s in ADVANCED_SKILLS)
    depth_bonus = min(advanced_count * 2, 10)
    domains = sum([
        any(s in skills for s in FRONTEND_SKILLS),
        any(s in skills for s in BACKEND_SKILLS),
        any(s in skills for s in DATA_SKILLS),
    ])
    diversity = min(domains * 3, 9) + (1 if len(skills) > 0 else 0)
    tech_score = min(breadth + depth_bonus + diversity, 40)

    # Projects score (40 pts)
    project_base = min(len(projects), 4) / 4 * 20
    complexity_scores = []
    for p in projects:
        stack_len = len(p.get("techStack", []))
        desc_len = len(p.get("description", "").split())
        complexity_scores.append(min((stack_len * 2 + desc_len // 10), 10))
    complexity_avg = (sum(complexity_scores) / len(complexity_scores)) if complexity_scores else 0
    has_frontend = any(s in skills for s in FRONTEND_SKILLS)
    has_backend = any(s in skills for s in BACKEND_SKILLS)
    fullstack_bonus = 10 if (has_frontend and has_backend) else 0
    project_score = min(project_base + complexity_avg + fullstack_bonus, 40)

    # Certifications score (20 pts)
    cert_score = min(len(certifications), 4) / 4 * 20

    total = int(tech_score + project_score + cert_score)

    # Recommended role
    if any(s in skills for s in DATA_SKILLS) and len([s for s in skills if s in DATA_SKILLS]) >= 3:
        role = "Data/ML Specialist"
    elif any(s in skills for s in DESIGN_SKILLS) and any(s in skills for s in FRONTEND_SKILLS):
        role = "Designer/Frontend Dev"
    elif total >= 75 and len(projects) >= 3:
        role = "Team Lead"
    elif total >= 50:
        role = "Developer"
    elif any(s in skills for s in FRONTEND_SKILLS):
        role = "Frontend Developer"
    else:
        role = "Presenter"

    return {
        "score": total,
        "recommendedRole": role,
        "breakdown": {
            "technicalSkills": int(tech_score),
            "projects": int(project_score),
            "certifications": int(cert_score),
        },
    }
