import os
import re
from typing import Dict, List, Any

class SiteAudit:
    """
    The Quality Controller of Genesis Web Architect.
    Verifies SEO, Performance markers, and SGE compliance.
    """

    def audit_page(self, file_path: str) -> Dict[str, Any]:
        if not os.path.exists(file_path):
            return {"status": "error", "message": "File not found"}

        with open(file_path, "r", encoding="utf-8") as f:
            content = f.read()

        report = {
            "seo_title": self._check_tag(content, "title"),
            "meta_description": self._check_meta(content, "description"),
            "json_ld": "application/ld+json" in content,
            "semantic_h1": "<h1>" in content,
            "viewport_meta": 'name="viewport"' in content,
            "errors": []
        }

        # SGE Check
        if not report["json_ld"]:
            report["errors"].append("Missing JSON-LD for Search Generative Experience")
        
        if not report["seo_title"]:
            report["errors"].append("Missing Title Tag")

        report["score"] = self._calculate_score(report)
        return report

    def _check_tag(self, content: str, tag: str) -> bool:
        return f"<{tag}>" in content and f"</{tag}>" in content

    def _check_meta(self, content: str, name: str) -> bool:
        return f'name="{name}"' in content

    def _calculate_score(self, report: Dict) -> int:
        score = 0
        if report["seo_title"]: score += 20
        if report["meta_description"]: score += 20
        if report["json_ld"]: score += 20
        if report["semantic_h1"]: score += 20
        if report["viewport_meta"]: score += 20
        return score

if __name__ == "__main__":
    auditor = SiteAudit()
    test_file = "dist/web/plumbing_index.html"
    if os.path.exists(test_file):
        report = auditor.audit_page(test_file)
        print(f"Audit Report for {test_file}:")
        for k, v in report.items():
            print(f"- {k}: {v}")
    else:
        print(f"Test file not found: {test_file}")
