#!/usr/bin/env python3
"""
Sitemap Generator for OnlineConvert
Generates XML sitemaps with 100 URLs per file, supports multiple languages and subdomains.
"""

import os
import sys
import sqlite3
import math
import shutil
from datetime import datetime
from xml.etree import ElementTree as ET
from xml.dom import minidom

# Add parent directory to path so we can import configs
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PROJECT_ROOT)

# Define dir_path here to avoid import issues
dir_path = PROJECT_ROOT

# Now import app-specific modules
try:
    from app import website_url, ALLOWED_DOMAIN
    from configs.filetypes import available_filetypes, available_hashtypes
    from configs.languages import supported_languages
    from configs.seo import seo
except ImportError as e:
    print(f"Error importing modules: {e}")
    print(f"Python path: {sys.path}")
    print(f"Project root: {PROJECT_ROOT}")
    sys.exit(1)


class SitemapGenerator:
    """Generate XML sitemaps with 100 URLs per file, supporting multiple languages and subdomains."""
    
    def __init__(self):
        self.base_url = f'https://{ALLOWED_DOMAIN}'
        self.static_dir = os.path.join(dir_path, 'static', 'sitemaps')
        self.subdomains_enabled = self.check_subdomain_setting()
        self.urls_per_sitemap = 100
        self.supported_languages = self.get_supported_languages()
        
        print(f"\n?? INITIALIZING:")
        print(f"  - subdomains_enabled: {self.subdomains_enabled}")
        print(f"  - supported_languages: {self.supported_languages}")
        
        os.makedirs(self.static_dir, exist_ok=True)
        
    def get_supported_languages(self):
        """Return only the languages that are toggled ON in the active_languages DB table.
        Falls back to English-only if the table doesn't exist yet."""
        db_path = os.path.join(dir_path, "storage", "sqlite.db")
        try:
            with sqlite3.connect(db_path) as conn:
                rows = conn.execute(
                    "SELECT lang_code FROM active_languages WHERE is_active = 1"
                ).fetchall()
                codes = [r[0] for r in rows]
        except Exception as e:
            print(f"Warning: could not read active_languages from DB ({e}), using English only.")
            codes = []

        if 'en' not in codes:
            codes.insert(0, 'en')

        print(f"  Active languages from DB: {codes}")
        return codes
    
    def check_subdomain_setting(self):
        """Check if subdomains are enabled from database"""
        try:
            db_path = os.path.join(dir_path, "storage", "sqlite.db")
            print(f"?? Checking database at: {db_path}")
            
            if not os.path.exists(db_path):
                print(f"  ?? Database not found at {db_path}, assuming subdomains disabled")
                return False
                
            with sqlite3.connect(db_path) as conn:
                cursor = conn.cursor()
                cursor.execute("SELECT value FROM site_settings WHERE key='subdomains_enabled'")
                result = cursor.fetchone()
                value = result[0] if result else '0'
                print(f"  ?? subdomains_enabled value from DB: '{value}'")
                return value == '1'
        except Exception as e:
            print(f"  ? Error reading subdomain setting: {e}")
            return False
    
    def cleanup_old_sitemaps(self):
        """Remove all existing sitemaps before generating new ones"""
        print("\n?? Cleaning up old sitemaps...")
        
        if not os.path.exists(self.static_dir):
            print("Sitemaps directory doesn't exist, skipping cleanup.")
            return
        
        deleted_count = 0
        for filename in os.listdir(self.static_dir):
            file_path = os.path.join(self.static_dir, filename)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
                    deleted_count += 1
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
                    deleted_count += 1
            except Exception as e:
                print(f"?? Error deleting {file_path}: {e}")
        
        print(f"? Cleaned up {deleted_count} old sitemap files/directories.")
    
    def get_all_blogs(self):
        """Fetch all published blog posts from database"""
        try:
            db_path = os.path.join(dir_path, "storage", "sqlite.db")
            if not os.path.exists(db_path):
                return []
                
            with sqlite3.connect(db_path) as conn:
                conn.row_factory = sqlite3.Row
                cursor = conn.cursor()
                # Remove 'lang' from SELECT since it doesn't exist
                cursor.execute("""
                    SELECT slug, updated_at, created_at 
                    FROM blogs 
                    WHERE status = 'published'
                    ORDER BY created_at DESC
                """)
                return [dict(row) for row in cursor.fetchall()]
        except Exception as e:
            print(f"Error fetching blogs: {e}")
            return []
    
    def slugify(self, text):
        """Convert text to lowercase slug"""
        if not text:
            return ""
        return text.lower().strip().replace(' ', '-').replace('_', '-')
    
    def format_url(self, path, lang='en'):
        """Format URL according to your full_url() function logic"""
        if not path.startswith('/'):
            path = '/' + path
        
        if path != '/' and path.endswith('/'):
            path = path.rstrip('/')
        
        # Handle language prefix (no prefix for English)
        lang_prefix = '' if lang == 'en' else f'/{lang}'
        
        return f"https://{ALLOWED_DOMAIN}{lang_prefix}{path}"
    
    def format_subdomain_url(self, subdomain, path, lang='en'):
        """Format subdomain URL according to your sub_url() function"""
        if not path.startswith('/'):
            path = '/' + path
        
        if path != '/' and path.endswith('/'):
            path = path.rstrip('/')
        
        # Handle language prefix (no prefix for English)
        lang_prefix = '' if lang == 'en' else f'/{lang}'
        
        return f"https://{subdomain}.{ALLOWED_DOMAIN}{lang_prefix}{path}"
    
    def format_converter_url(self, filetype, fileformat, lang='en'):
        """Format converter URL based on your full_url() logic for filetype+fileformat"""
        lang_prefix = '' if lang == 'en' else f'/{lang}'
        
        if self.subdomains_enabled:
            # Subdomain mode
            if filetype == "device":
                path = f"/convert-for-{fileformat}"
            elif filetype == "pdf":
                path = f"/do/{fileformat}"    
            elif filetype == "hash":
                path = f"/{fileformat}-generator"
            elif filetype in ["image-compressor", "video-compressor"]:
                path = f"/compress-{fileformat}"
            else:
                path = f"/convert-to-{fileformat}"
            return f"https://{filetype}.{ALLOWED_DOMAIN}{lang_prefix}{path}"
        else:
            # Main domain mode
            return f"https://{ALLOWED_DOMAIN}{lang_prefix}/converter/{filetype}/{fileformat}"
    
    def format_conversion_url(self, filetype, input_format, output_format, lang='en'):
        """Format conversion URL based on your full_url() logic for format-to-format"""
        lang_prefix = '' if lang == 'en' else f'/{lang}'
        
        if self.subdomains_enabled:
            # Subdomain mode
            return f"https://{filetype}.{ALLOWED_DOMAIN}{lang_prefix}/convert/{input_format}-to-{output_format}"
        else:
            # Main domain mode
            return f"https://{ALLOWED_DOMAIN}{lang_prefix}/converter/{input_format}/{output_format}"
    
    def prettify(self, elem):
        """Return a pretty-printed XML string for the Element."""
        rough_string = ET.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ", encoding='utf-8')
    
    def generate_url_element(self, urlset, loc, lastmod=None, changefreq='weekly', priority='0.5'):
        """Generate a URL element for sitemap"""
        url = ET.SubElement(urlset, 'url')
        
        loc_elem = ET.SubElement(url, 'loc')
        loc_elem.text = loc
        
        if lastmod:
            lastmod_elem = ET.SubElement(url, 'lastmod')
            if isinstance(lastmod, str):
                lastmod_elem.text = lastmod[:10]
            else:
                lastmod_elem.text = datetime.now().strftime('%Y-%m-%d')
        
        changefreq_elem = ET.SubElement(url, 'changefreq')
        changefreq_elem.text = changefreq
        
        priority_elem = ET.SubElement(url, 'priority')
        priority_elem.text = priority
        
        return url
    
    def write_sitemap(self, urls, filepath):
        """Write URLs to sitemap XML file"""
        urlset = ET.Element('urlset')
        urlset.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
        
        for url_data in urls:
            self.generate_url_element(
                urlset,
                url_data['loc'],
                url_data.get('lastmod'),
                url_data.get('changefreq', 'weekly'),
                url_data.get('priority', '0.5')
            )
        
        xml_str = self.prettify(urlset)
        with open(filepath, 'wb') as f:
            f.write(xml_str)
        
        print(f"  ? Generated: {os.path.basename(filepath)} ({len(urls)} URLs)")
    
    def write_sitemap_index(self, sitemaps, filepath):
        """Write sitemap index XML file"""
        sitemapindex = ET.Element('sitemapindex')
        sitemapindex.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
        
        sitemaps.sort(key=lambda x: x['loc'])
        
        for sitemap in sitemaps:
            sm = ET.SubElement(sitemapindex, 'sitemap')
            
            loc = ET.SubElement(sm, 'loc')
            loc.text = sitemap['loc']
            
            lastmod = ET.SubElement(sm, 'lastmod')
            lastmod.text = sitemap.get('lastmod', datetime.now().strftime('%Y-%m-%d'))
        
        xml_str = self.prettify(sitemapindex)
        with open(filepath, 'wb') as f:
            f.write(xml_str)
        
        print(f"  ? Generated index: {os.path.basename(filepath)} pointing to {len(sitemaps)} sitemaps")
    
    def split_urls_into_sitemaps(self, urls, base_name, lang='en', prefix=''):
        """Split URLs into multiple sitemap files if > 100 URLs"""
        if len(urls) <= self.urls_per_sitemap:
            filename = f"{base_name}.xml"
            filepath = os.path.join(self.static_dir, prefix, filename)
            os.makedirs(os.path.dirname(filepath), exist_ok=True)
            self.write_sitemap(urls, filepath)
            return [filename]
        else:
            num_files = math.ceil(len(urls) / self.urls_per_sitemap)
            sitemap_files = []
            
            print(f"  ?? Splitting {len(urls)} URLs into {num_files} sitemaps ({self.urls_per_sitemap} URLs each)")
            
            for i in range(num_files):
                start = i * self.urls_per_sitemap
                end = min(start + self.urls_per_sitemap, len(urls))
                chunk = urls[start:end]
                
                filename = f"{base_name}-{i+1}.xml"
                filepath = os.path.join(self.static_dir, prefix, filename)
                os.makedirs(os.path.dirname(filepath), exist_ok=True)
                
                self.write_sitemap(chunk, filepath)
                sitemap_files.append(filename)
            
            return sitemap_files
    
    def generate_main_domain_urls(self, lang):
        """Generate URLs for main domain (static pages, blogs, SEO pages)"""
        urls = []
        
        print(f"\n    ?? Generating main domain URLs for {lang} (subdomains_enabled={self.subdomains_enabled})")
        
        # 1. Static pages (always included)
        static_pages = [
            ('', 'daily', '1.0'),
            ('about', 'monthly', '0.8'),
            ('contact', 'monthly', '0.8'),
            ('faq', 'monthly', '0.8'),
            ('privacy', 'monthly', '0.5'),
            ('terms', 'monthly', '0.5'),
            ('imprint', 'monthly', '0.5'),
            ('disclaimer', 'monthly', '0.5'),
            ('formats', 'weekly', '0.7'),
            ('currencies', 'weekly', '0.7'),
            ('developers', 'weekly', '0.6'),
            ('pricing', 'weekly', '0.8'),
            ('api', 'weekly', '0.6'),
        ]
        
        static_count = 0
        for page, freq, prio in static_pages:
            path = f"/{page}" if page else "/"
            urls.append({
                'loc': self.format_url(path, lang),
                'lastmod': datetime.now().isoformat(),
                'changefreq': freq,
                'priority': prio
            })
            static_count += 1
        
        # 2. Blog posts (always included)
        blogs = self.get_all_blogs()
        blog_count = 0
        for blog in blogs:
            slug_lower = self.slugify(blog['slug'])
            urls.append({
                'loc': self.format_url(f"/blog/{slug_lower}", lang),
                'lastmod': blog.get('updated_at') or blog.get('created_at') or datetime.now().isoformat(),
                'changefreq': 'weekly',
                'priority': '0.7'
            })
            blog_count += 1
        
        # 3. SEO pages from configs.seo
        seo_count = 0
        filtered_seo_count = 0
        
        # Patterns that indicate converter URLs
        converter_patterns = [
            '-to-',
            'converter/',
            'convert/',
            'compress-',
            'convert-to-',
            'convert-for-',
            'generator',
            '/do/'
        ]
        
        for seo_url in seo.keys():
            seo_url_lower = seo_url.lower()
            
            # Check if this is a converter URL
            is_converter = any(pattern in seo_url_lower for pattern in converter_patterns)
            
            # Skip converter URLs when subdomains are ON
            if self.subdomains_enabled and is_converter:
                filtered_seo_count += 1
                continue
            
            # For main domain, always use format_url (no subdomain)
            urls.append({
                'loc': self.format_url(seo_url_lower, lang),
                'lastmod': datetime.now().isoformat(),
                'changefreq': 'monthly',
                'priority': '0.6'
            })
            seo_count += 1
        
        print(f"      - Static pages: {static_count}")
        print(f"      - Blog posts: {blog_count}")
        print(f"      - SEO pages: {seo_count}")
        if self.subdomains_enabled:
            print(f"      - Filtered out converter URLs: {filtered_seo_count}")
        
        # Verify no converter URLs slipped through
        converter_in_main = []
        for u in urls:
            loc = u['loc'].lower()
            if any(pattern in loc for pattern in converter_patterns):
                converter_in_main.append(u)
        
        if converter_in_main and self.subdomains_enabled:
            print(f"      ? ERROR: Found {len(converter_in_main)} converter URLs in main domain sitemap!")
            for u in converter_in_main[:5]:
                print(f"        - {u['loc']}")
        elif self.subdomains_enabled:
            print(f"      ? Verified: No converter URLs in main domain sitemap")
        
        print(f"      TOTAL URLs for {lang}: {len(urls)}")
        return urls
    
    def generate_subdomain_converter_urls(self, subdomain, lang):
        """Generate all converter URLs for a specific subdomain"""
        urls = []
        subdomain_lower = subdomain.lower()
        
        # Get all file formats for this subdomain
        if subdomain_lower == 'hash':
            # Hash generators
            if subdomain_lower in available_hashtypes:
                for hashtype in available_hashtypes[subdomain_lower]['ext']:
                    hashtype_lower = hashtype.lower()
                    urls.append({
                        'loc': self.format_subdomain_url(subdomain_lower, f"/{hashtype_lower}-generator", lang),
                        'lastmod': datetime.now().isoformat(),
                        'changefreq': 'weekly',
                        'priority': '0.9'
                    })
        
        elif subdomain_lower in available_filetypes:
            config = available_filetypes[subdomain_lower]
            formats = config.get('ext', [])
            
            # Type 1: convert-to-X URLs (e.g., /convert-to-pdf)
            for fmt in formats:
                fmt_lower = fmt.lower()
                
                if subdomain_lower == 'device':
                    path = f"/convert-for-{fmt_lower}"
                elif subdomain_lower in ['image-compressor', 'video-compressor']:
                    path = f"/compress-{fmt_lower}"
                elif subdomain_lower == 'pdf':
                    path = f"/do/{fmt_lower}"
                else:
                    path = f"/convert-to-{fmt_lower}"
                
                urls.append({
                    'loc': self.format_subdomain_url(subdomain_lower, path, lang),
                    'lastmod': datetime.now().isoformat(),
                    'changefreq': 'weekly',
                    'priority': '0.9'
                })
            
            # Type 2: X-to-Y conversion URLs (e.g., /convert/pdf-to-docx)
            if 'allowed' in config:
                for source in config['allowed']:
                    source_lower = source.lower()
                    for target in formats:
                        target_lower = target.lower()
                        if source_lower != target_lower:
                            urls.append({
                                'loc': self.format_subdomain_url(
                                    subdomain_lower, 
                                    f"/convert/{source_lower}-to-{target_lower}", 
                                    lang
                                ),
                                'lastmod': datetime.now().isoformat(),
                                'changefreq': 'weekly',
                                'priority': '0.8'
                            })
        
        return urls
    
    def generate_sitemap_for_subdomains_off(self):
        """Generate single comprehensive sitemap when subdomains are OFF"""
        print("\n" + "=" * 60)
        print("?? MODE: Subdomains OFF - Generating single comprehensive sitemaps...")
        print("   All URLs (including converters) will be in main domain sitemaps")
        print("=" * 60)
        
        for lang in self.supported_languages:
            print(f"\n?? Processing language: {lang}")
            
            # Start with main domain URLs
            urls = self.generate_main_domain_urls(lang)
            
            # Add converter URLs (main domain style)
            print(f"\n    ?? Adding converter URLs for {lang} (main domain mode)...")
            converter_count = 0
            
            # Add converter URLs for all filetypes
            all_filetypes = list(available_filetypes.keys()) + ['hash-generators']
            
            for filetype in all_filetypes:
                if filetype == 'hash-generators':
                    if filetype in available_hashtypes:
                        for hashtype in available_hashtypes[filetype]['ext']:
                            urls.append({
                                'loc': self.format_converter_url(filetype, hashtype, lang),
                                'lastmod': datetime.now().isoformat(),
                                'changefreq': 'weekly',
                                'priority': '0.8'
                            })
                            converter_count += 1
                elif filetype in available_filetypes:
                    config = available_filetypes[filetype]
                    formats = config.get('ext', [])
                    
                    # Add convert-to URLs
                    for fmt in formats:
                        urls.append({
                            'loc': self.format_converter_url(filetype, fmt, lang),
                            'lastmod': datetime.now().isoformat(),
                            'changefreq': 'weekly',
                            'priority': '0.8'
                        })
                        converter_count += 1
                    
                    # Add X-to-Y conversion URLs
                    if 'allowed' in config:
                        for source in config['allowed']:
                            for target in formats:
                                if source != target:
                                    urls.append({
                                        'loc': self.format_conversion_url(filetype, source, target, lang),
                                        'lastmod': datetime.now().isoformat(),
                                        'changefreq': 'weekly',
                                        'priority': '0.7'
                                    })
                                    converter_count += 1
            
            print(f"      Added {converter_count} converter URLs")
            print(f"      TOTAL URLs for {lang}: {len(urls)}")
            
            if not urls:
                print(f"  ?? No URLs generated for {lang}")
                continue
            
            # Split into sitemaps
            base_name = 'sitemap' if lang == 'en' else f'sitemap-{lang}'
            sitemap_files = self.split_urls_into_sitemaps(urls, base_name, lang)
            
            # Create sitemap index if multiple files
            if len(sitemap_files) > 1:
                index_sitemaps = []
                for filename in sitemap_files:
                    index_sitemaps.append({
                        'loc': f"{self.base_url}/{filename}",
                        'lastmod': datetime.now().isoformat()
                    })
                
                index_filename = f"{base_name}.xml"
                index_path = os.path.join(self.static_dir, index_filename)
                self.write_sitemap_index(index_sitemaps, index_path)
        
        print("\n? Subdomains OFF: Sitemap generation complete.")
    
    def generate_sitemap_for_subdomains_on(self):
        """Generate multiple sitemaps when subdomains are ON"""
        print("\n" + "=" * 60)
        print("?? MODE: Subdomains ON - Generating multi-sitemap structure...")
        print("   Main domain sitemaps will ONLY contain static pages, blogs, etc.")
        print("   Converter URLs will be in subdomain sitemaps only")
        print("=" * 60)
        
        # Create subdirectory for subdomain sitemaps
        subdomains_dir = os.path.join(self.static_dir, 'subdomains')
        os.makedirs(subdomains_dir, exist_ok=True)
        
        # ===== MAIN DOMAIN SITEMAPS (NO CONVERTER URLs) =====
        print("\n?? Generating main domain sitemaps (static pages only)...")
        
        main_sitemaps = []
        
        for lang in self.supported_languages:
            print(f"\n  ?? Language: {lang}")
            urls = self.generate_main_domain_urls(lang)
            
            if not urls:
                print(f"    ?? No URLs generated for {lang}")
                continue
            
            base_name = 'sitemap-main'
            if lang != 'en':
                base_name = f'sitemap-main-{lang}'
            
            sitemap_files = self.split_urls_into_sitemaps(urls, base_name, lang)
            
            for filename in sitemap_files:
                main_sitemaps.append({
                    'loc': f"{self.base_url}/{filename}",
                    'lastmod': datetime.now().isoformat()
                })
        
        # ===== SUBDOMAIN SITEMAPS (ALL CONVERTER URLs) =====
        print("\n?? Generating subdomain sitemaps (all converter URLs)...")
        
        all_subdomains = list(available_filetypes.keys()) + ['hash-generators']
        subdomain_sitemaps = []
        
        for subdomain in all_subdomains:
            subdomain_lower = subdomain.lower()
            print(f"\n  ?? Subdomain: {subdomain_lower}")
            
            for lang in self.supported_languages:
                urls = self.generate_subdomain_converter_urls(subdomain_lower, lang)
                
                if not urls:
                    continue
                
                print(f"    Language {lang}: {len(urls)} URLs")
                
                base_name = 'sitemap'
                if lang != 'en':
                    base_name = f'sitemap-{lang}'
                
                sitemap_files = self.split_urls_into_sitemaps(
                    urls, 
                    base_name, 
                    lang,
                    prefix=os.path.join('subdomains', subdomain_lower)
                )
                
                for filename in sitemap_files:
                    subdomain_sitemaps.append({
                        'loc': f"https://{subdomain_lower}.{ALLOWED_DOMAIN}/{filename}",
                        'lastmod': datetime.now().isoformat()
                    })
        
        # ===== CREATE MAIN SITEMAP INDEX =====
        print("\n?? Creating main sitemap index...")
        
        all_sitemaps = main_sitemaps + subdomain_sitemaps
        
        if all_sitemaps:
            index_path = os.path.join(self.static_dir, 'sitemap.xml')
            self.write_sitemap_index(all_sitemaps, index_path)
            print(f"  ? Main index points to {len(main_sitemaps)} main sitemaps and {len(subdomain_sitemaps)} subdomain sitemaps")
        
        print("\n? Subdomains ON: Sitemap generation complete.")
    
    def generate(self):
        """Main generation function"""
        print("=" * 70)
        print("?? SITEMAP GENERATOR")
        print("=" * 70)
        print(f"?? Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"?? subdomains_enabled: {self.subdomains_enabled}")
        print(f"?? URLs per sitemap: {self.urls_per_sitemap}")
        print(f"?? Supported languages: {', '.join(self.supported_languages)}")
        print("=" * 70)
        
        # Clean up all existing sitemaps
        self.cleanup_old_sitemaps()
        
        # Generate new sitemaps based on current setting
        if self.subdomains_enabled:
            self.generate_sitemap_for_subdomains_on()
        else:
            self.generate_sitemap_for_subdomains_off()
        
        print("\n" + "=" * 70)
        print("? SITEMAP GENERATION COMPLETE!")
        print("=" * 70)
        
        self.print_summary()
    
    def print_summary(self):
        """Print summary of generated sitemap files"""
        print("\n?? GENERATED FILES SUMMARY:")
        print("-" * 50)
        
        total_files = 0
        total_urls = 0
        converter_in_main = 0
        
        for root, dirs, files in os.walk(self.static_dir):
            for file in files:
                if file.endswith('.xml'):
                    file_path = os.path.join(root, file)
                    try:
                        with open(file_path, 'r', encoding='utf-8') as f:
                            content = f.read()
                            if '<urlset' in content:
                                url_count = content.count('<url>')
                                total_urls += url_count
                                
                                # Check if this is a main domain sitemap with converter URLs
                                if 'sitemap-main' in file and self.subdomains_enabled:
                                    if any(pattern in content for pattern in ['/converter/', '/convert/', '-to-']):
                                        converter_in_main += 1
                                
                            total_files += 1
                            rel_path = os.path.relpath(file_path, self.static_dir)
                            file_type = "?? SITEMAP" if '<urlset' in content else "?? INDEX"
                            print(f"  {file_type} {rel_path}" + (f" ({url_count} URLs)" if '<urlset' in content else ""))
                    except Exception as e:
                        print(f"  ?? {rel_path}")
        
        print("-" * 50)
        print(f"  Total files: {total_files}")
        print(f"  Total URLs across all sitemaps: {total_urls}")
        if converter_in_main > 0 and self.subdomains_enabled:
            print(f"  ? ERROR: Found {converter_in_main} main domain sitemaps containing converter URLs!")
        elif converter_in_main == 0 and self.subdomains_enabled:
            print(f"  ? VERIFIED: No converter URLs in main domain sitemaps")
        print("-" * 50)


if __name__ == '__main__':
    try:
        generator = SitemapGenerator()
        generator.generate()
    except KeyboardInterrupt:
        print("\n\n?? Sitemap generation interrupted by user.")
        sys.exit(0)
    except Exception as e:
        print(f"\n? Error during sitemap generation: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)