"""Overview page: List of available result directories with some summary statistics."""

import streamlit as st
from stopuhr import stopwatch

from entropice.dashboard.sections.dataset_statistics import render_dataset_statistics
from entropice.dashboard.sections.experiment_results import (
    render_experiment_results,
    render_training_results_summary,
)
from entropice.dashboard.sections.storage_statistics import render_storage_statistics
from entropice.dashboard.utils.loaders import load_all_training_results
from entropice.dashboard.utils.stats import DatasetStatistics, load_all_default_dataset_statistics


def render_overview_page():
    """Render the Overview page of the dashboard."""
    st.title("🏡 Entropice Overview")
    st.markdown(
        """
        Welcome to the Entropice Dashboard! This overview page provides a summary of your
        training experiments and dataset analyses.

        Use the sections below to explore training results, dataset sample counts,
        and feature configurations.
        """
    )
    # Load training results
    training_results = load_all_training_results()

    if not training_results:
        st.warning("No training results found. Please run some training experiments first.")
    else:
        st.write(f"Found **{len(training_results)}** training result(s)")

        st.divider()

        # Render training results sections
        render_training_results_summary(training_results)

        st.divider()

        render_experiment_results(training_results)

    st.divider()

    # Render dataset analysis section
    all_stats = load_all_default_dataset_statistics()
    training_sample_df = DatasetStatistics.get_target_sample_count_df(all_stats)
    feature_breakdown_df = DatasetStatistics.get_feature_breakdown_df(all_stats)
    comparison_df = DatasetStatistics.get_comparison_df(all_stats)
    inference_sample_df = DatasetStatistics.get_inference_sample_count_df(all_stats)

    render_dataset_statistics(all_stats, training_sample_df, feature_breakdown_df, comparison_df, inference_sample_df)

    st.divider()

    # Render storage statistics section
    render_storage_statistics()

    st.balloons()
    stopwatch.summary()
