#!/usr/bin/env python3
import urllib.request
import urllib.error
import xml.etree.ElementTree as ET
import sys

print("=" * 70)
print("TASK 2: SITEMAP VALIDATION")
print("=" * 70)

# Read sitemap
sitemap_path = "/home/sirdrez/arisingmedia-websites/floorithardwoodfloors.com/sitemap.xml"
print(f"\nReading sitemap from {sitemap_path}...")
try:
    tree = ET.parse(sitemap_path)
    root = tree.getroot()
    print("OK - XML parsed successfully")
except Exception as e:
    print(f"FAILED: {e}")
    sys.exit(1)

# Extract namespace
ns = {'': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
if root.tag.startswith('{'):
    ns_uri = root.tag.split('}')[0][1:]
    ns['sm'] = ns_uri
    ns_prefix = 'sm'
else:
    ns_prefix = ''

# Find all <url> entries
urls = []
for url_elem in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}url' if ns_prefix == 'sm' else './url'):
    loc = url_elem.find('.//{http://www.sitemaps.org/schemas/sitemap/0.9}loc' if ns_prefix == 'sm' else 'loc')
    lastmod = url_elem.find('.//{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod' if ns_prefix == 'sm' else 'lastmod')
    changefreq = url_elem.find('.//{http://www.sitemaps.org/schemas/sitemap/0.9}changefreq' if ns_prefix == 'sm' else 'changefreq')
    priority = url_elem.find('.//{http://www.sitemaps.org/schemas/sitemap/0.9}priority' if ns_prefix == 'sm' else 'priority')

    if loc is not None:
        urls.append({
            'loc': loc.text,
            'lastmod': lastmod.text if lastmod is not None else None,
            'changefreq': changefreq.text if changefreq is not None else None,
            'priority': priority.text if priority is not None else None,
        })

print(f"\nFound {len(urls)} URLs in sitemap\n")

# Check XML structure
required_fields = ['loc']
missing_fields = []
for idx, url_elem in enumerate(urls):
    for field in required_fields:
        if url_elem[field] is None or url_elem[field] == '':
            missing_fields.append(f"  URL {idx}: missing {field}")

if missing_fields:
    print("MALFORMED ENTRIES:")
    for msg in missing_fields[:10]:
        print(msg)
else:
    print("XML structure: OK - all entries have required fields")

# Check all URLs return 200
print("\nFetching each URL...", end="")
failed_urls = {}
for url in urls:
    loc = url['loc']
    # Replace domain with localhost
    check_url = loc.replace('https://floorithardwoodfloors.com', 'http://localhost:8096')
    try:
        with urllib.request.urlopen(check_url, timeout=5) as resp:
            if resp.getcode() != 200:
                failed_urls[check_url] = resp.getcode()
    except urllib.error.HTTPError as e:
        failed_urls[check_url] = e.code
    except Exception as e:
        failed_urls[check_url] = str(e)

print(" done")

# Expected routes (17 content routes)
expected_routes = {
    'http://localhost:8096/',
    'http://localhost:8096/about/',
    'http://localhost:8096/reviews/',
    'http://localhost:8096/blog/',
    'http://localhost:8096/contact/',
    'http://localhost:8096/services/',
    'http://localhost:8096/services/floor-installation/',
    'http://localhost:8096/services/floor-refinishing/',
    'http://localhost:8096/services/floor-restoration/',
    'http://localhost:8096/services/floor-sanding/',
    'http://localhost:8096/locations/',
    'http://localhost:8096/locations/buffalo/',
    'http://localhost:8096/locations/amherst/',
    'http://localhost:8096/locations/williamsville/',
    'http://localhost:8096/locations/clarence/',
    'http://localhost:8096/locations/east-amherst/',
    'http://localhost:8096/locations/lancaster/',
}

sitemap_urls = set()
for url in urls:
    check_url = url['loc'].replace('https://floorithardwoodfloors.com', 'http://localhost:8096')
    sitemap_urls.add(check_url)

missing_from_sitemap = expected_routes - sitemap_urls
extra_in_sitemap = sitemap_urls - expected_routes

print("\n" + "=" * 70)
print("VALIDATION RESULTS:")
print("=" * 70)

if missing_from_sitemap:
    print(f"\nMISSING from sitemap ({len(missing_from_sitemap)}):")
    for url in sorted(missing_from_sitemap):
        print(f"  {url}")
else:
    print("\nAll 17 expected routes present in sitemap: OK")

if extra_in_sitemap:
    print(f"\nEXTRA in sitemap ({len(extra_in_sitemap)}):")
    for url in sorted(extra_in_sitemap):
        print(f"  {url}")

if failed_urls:
    print(f"\nNON-200 responses ({len(failed_urls)}):")
    for url, code in sorted(failed_urls.items()):
        print(f"  {url} -> {code}")
else:
    print("\nAll URLs returned 200: OK")

if not missing_fields:
    print("\nMalformed entries: OK")

print("=" * 70)
