"""
Main HTML export orchestration
"""

from typing import Dict

from .structure import _generate_header, _generate_metadata_cards, _generate_metadata_section, _generate_column_summary_table
from .insights import _generate_column_insights
from .checks import _generate_issues_section


def export_to_html(results: Dict, file_path: str = "audit_report.html", thousand_separator: str = ",", decimal_places: int = 1) -> str:
    """
    Export audit results to a beautiful HTML report

    Args:
        results: Audit results dictionary
        file_path: Path to save HTML file
        thousand_separator: Separator for thousands (default: ",")
        decimal_places: Number of decimal places to display (default: 1)

    Returns:
        Path to saved HTML file
    """
    has_issues = any(col_data['issues'] for col_data in results['columns'].values())

    # Assemble the HTML report from components
    html = _generate_header(results)

    # Tabs navigation
    html += """
    <nav class="tabs">
        <div class="tab active" data-tab="summary">Summary</div>
        <div class="tab" data-tab="insights">Insights</div>
        <div class="tab" data-tab="checks">Quality Checks</div>
        <div class="tab" data-tab="metadata">Metadata</div>
    </nav>
    """

    # Summary tab (metadata cards and column summary)
    html += _generate_metadata_cards(results, has_issues)
    html += _generate_column_summary_table(results)
    html += """
    </section>
    """

    # Insights tab (column insights only)
    html += f"""
    <section id="insights" class="tab-content">
        {_generate_column_insights(results, thousand_separator, decimal_places)}
    </section>
    """

    # Quality Checks tab
    html += f"""
    <section id="checks" class="tab-content">
        {_generate_issues_section(results, has_issues)}
    </section>
    """

    # Metadata tab
    html += _generate_metadata_section(results)

    # Footer
    html += """
    <div class="footer">
        <p>Generated by SecureTableAuditor</p>
    </div>
</body>
</html>
"""

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html)

    print(f"📄 HTML report saved to: {file_path}")
    return file_path
