import os

cmass_dirs = [
    "CMass0", "CMass2", "CMassK1", "CMassM1", 
    "CMassP1", "CMassY1", "CMassY2", "CMassY3"
]

base_path = "/Volumes/Scratch/Sites"

def check_site(site_path):
    issues = []
    
    # Check index.html
    index_path = os.path.join(site_path, "index.html")
    if not os.path.exists(index_path):
        issues.append("Missing index.html")
    else:
        size = os.path.getsize(index_path)
        if size < 500:
            issues.append(f"index.html very small ({size} bytes)")
        
        try:
            with open(index_path, 'r', encoding='utf-8') as f:
                content = f.read().lower()
                if "todo" in content:
                    issues.append("index.html contains TODO")
                if "lorem ipsum" in content:
                    issues.append("index.html contains Lorem Ipsum")
                if "placeholder" in content:
                    issues.append("index.html contains Placeholder")
                if "<body" not in content or "</body>" not in content:
                    issues.append("index.html missing body tags")
                elif "</body>" in content and content.index("</body>") - content.index("<body") < 50:
                    issues.append("index.html body seems empty")
        except Exception as e:
            issues.append(f"Error reading index.html: {str(e)}")

    # Check styles.css
    css_path = os.path.join(site_path, "styles.css")
    if not os.path.exists(css_path):
        issues.append("Missing styles.css")
    else:
        size = os.path.getsize(css_path)
        if size < 100:
            issues.append(f"styles.css very small ({size} bytes)")

    return issues

results = []

for cmass in cmass_dirs:
    cmass_path = os.path.join(base_path, cmass)
    sites_dir = os.path.join(cmass_path, "sites")
    
    # Special case for CMass0 as observed
    if cmass == "CMass0":
        sites_dir = os.path.join(cmass_path, "tools", "sites")
    
    if not os.path.exists(sites_dir):
        print(f"Skipping {cmass}: sites directory not found at {sites_dir}")
        continue
        
    print(f"Checking sites in {cmass}...")
    for site_name in os.listdir(sites_dir):
        site_path = os.path.join(sites_dir, site_name)
        if os.path.isdir(site_path):
            issues = check_site(site_path)
            if issues:
                results.append((cmass, site_name, issues))

# Sort results by CMass and then site name
results.sort()

if not results:
    print("No issues found.")
else:
    print(f"Found issues in {len(results)} sites:")
    for cmass, site, issues in results:
        print(f"[{cmass}] {site}: {', '.join(issues)}")
