from services.vulnerability_intelligence.handlers.base_handler import BaseHandler
from models.vulnerability_intelligence import VulnerabilityIntelligence

class GitHubPoCHandler(BaseHandler):
    def apply(self, vuln_intelligence: VulnerabilityIntelligence):
        try:   
            sorted_data = sorted(self.data, key=lambda entry: entry.get('github_stars', 0), reverse=True)

            for entry in sorted_data:
                github_url = entry.get('github_url', "N/A")
                github_date = entry.get('github_date', "N/A")
                github_description = entry.get('github_description')
                github_tags = entry.get('github_tags', [])
                github_stars = entry.get('github_stars')
                
                if github_description:
                    vuln_intelligence.descriptions.append({
                        "source": self.enrich_source_name("GitHub PoC"),
                        "text": github_description,
                        "date": github_date
                    })
                    
                vuln_intelligence.urls.append({
                    "source": self.enrich_source_name(f"GitHub - PoC Exploit [{github_stars} ⭐]"),
                    "url": github_url,
                    "date": github_date
                })
                
                vuln_intelligence.reference_urls.update([github_url]) 
                vuln_intelligence.tags.update(github_tags)

        except Exception as e:
            print(f"[!] Error applying GitHub JSON PoC enrichment: {e}")
