#!/usr/bin/env python3
"""
Update Outdated Dates Script
Updates dates in files that are marked as outdated
"""

import re
import json
from pathlib import Path
from datetime import datetime

def load_inconsistencies():
    """Load content inconsistencies"""
    with open('CONTENT_INCONSISTENCIES.json', 'r') as f:
        return json.load(f)

def update_outdated_dates():
    """Update outdated dates in files"""
    data = load_inconsistencies()
    
    old_dates = [inc for inc in data['inconsistencies'] if inc['type'] == 'old_date']
    
    updated_count = 0
    
    print("📅 Updating outdated dates...")
    print("")
    
    for item in old_dates:
        file_path = Path(item['file'])
        
        if not file_path.exists():
            continue
        
        try:
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                content = f.read()
            
            original_content = content
            
            # Update date patterns
            today = datetime.now().strftime('%Y-%m-%d')
            
            # Replace old dates with today's date
            # Pattern: Date: YYYY-MM-DD or Last Updated: YYYY-MM-DD
            content = re.sub(
                r'(Date|Last Updated|Generated)[:\s]+(\d{4}-\d{2}-\d{2})',
                rf'\1: {today}',
                content,
                flags=re.IGNORECASE
            )
            
            # Replace standalone dates in headers
            content = re.sub(
                r'\*\*Date\*\*[:\s]+(\d{4}-\d{2}-\d{2})',
                f'**Date**: {today}',
                content,
                flags=re.IGNORECASE
            )
            
            if content != original_content:
                with open(file_path, 'w', encoding='utf-8') as f:
                    f.write(content)
                print(f"✅ Updated: {file_path}")
                updated_count += 1
        
        except Exception as e:
            print(f"⚠️  Error updating {file_path}: {e}")
    
    print("")
    print(f"✅ Updated {updated_count} files")

if __name__ == '__main__':
    update_outdated_dates()
