#!/usr/bin/env python3
"""
Script to update all report templates with enhanced styling
"""

import os
import re

def update_report_template(file_path):
    """Update a single report template with enhanced styling"""
    
    if not os.path.exists(file_path):
        print(f"File not found: {file_path}")
        return
    
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Skip if already updated
    if 'reports-enhanced.css' in content:
        print(f"Already updated: {file_path}")
        return
    
    # Update extends and add CSS
    content = re.sub(
        r'{% extends \'base\.html\' %}\s*{% load humanize %}',
        '{% extends \'base.html\' %}\n{% load humanize %}\n{% load static %}',
        content
    )
    
    # Add CSS block
    content = re.sub(
        r'{% block title %}([^%]+){% endblock %}',
        r'{% block title %}\1{% endblock %}\n\n{% block extra_css %}\n<link rel="stylesheet" href="{% static \'css/reports-enhanced.css\' %}">\n{% endblock %}',
        content
    )
    
    # Update content wrapper
    content = re.sub(
        r'{% block content %}\s*<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6">',
        '{% block content %}\n<div class="report-container">\n<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6 fade-in">',
        content
    )
    
    # Update page header
    content = re.sub(
        r'<!-- Page Header -->\s*<div class="mb-8">\s*<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">\s*<div>\s*<h1 class="text-3xl font-bold text-gray-900 dark:text-white">([^<]+)</h1>\s*<p class="text-gray-600 dark:text-gray-400 mt-1">([^<]+)</p>\s*</div>',
        r'<!-- Enhanced Page Header -->\n    <div class="report-card slide-up">\n        <div class="report-header">\n            <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">\n                <div>\n                    <h1 class="report-title">\1</h1>\n                    <p class="report-subtitle">\2</p>\n                </div>',
        content,
        flags=re.DOTALL
    )
    
    # Update export buttons
    content = re.sub(
        r'<div class="flex gap-2">\s*<a href="{% url \'reports:reports_dashboard\' %}"\s*class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200[^"]*">\s*<i class="fas fa-arrow-left mr-2"></i> Back to Dashboard\s*</a>\s*<a href="\?format=pdf"\s*class="inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-gradient-to-r from-blue-600 to-purple-600 rounded-lg[^"]*">\s*<i class="fas fa-download mr-2"></i> Export PDF\s*</a>\s*</div>',
        '<div class="export-buttons">\n                    <a href="{% url \'reports:reports_dashboard\' %}" class="action-button secondary">\n                        <i class="fas fa-arrow-left"></i> Back to Dashboard\n                    </a>\n                    <a href="?format=pdf" class="export-button pdf">\n                        <i class="fas fa-file-pdf"></i> Export PDF\n                    </a>\n                    <a href="?format=csv" class="export-button csv">\n                        <i class="fas fa-file-csv"></i> Export CSV\n                    </a>\n                </div>',
        content,
        flags=re.DOTALL
    )
    
    # Close the content wrapper properly
    content = re.sub(
        r'</div>\s*{% endblock %}$',
        '</div>\n</div>\n{% endblock %}',
        content
    )
    
    # Write updated content
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f"Updated: {file_path}")

def main():
    """Update all report templates"""
    
    report_templates = [
        'templates/reports/processing_fees_report.html',
        'templates/reports/interest_income_report.html', 
        'templates/reports/registration_fees_report.html',
        'templates/reports/loans_in_arrears_report.html'
    ]
    
    for template in report_templates:
        update_report_template(template)
    
    print("All report templates updated!")

if __name__ == '__main__':
    main()