#!/usr/bin/env python3
"""
Aggregate runner to generate all example images.

Note: Individual examples are maintained in separate scripts:
- violinplot_examples.py
- boxplot_examples.py
- shiftplot_examples.py
- correlation_matrix_example.py
- alluvial_examples.py

This runner imports and executes their main functions for convenience.
"""

import os
import sys

# Add the parent directory to sys.path to import ggpubpy
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

try:
    import ggpubpy

    print("✓ Successfully imported ggpubpy")
except Exception as e:
    print(f"✗ Failed to import ggpubpy: {e}")
    sys.exit(1)

import matplotlib.pyplot as plt


def main() -> None:
    """Generate all example plots for README."""
    print("Running individual example scripts to generate all images...")
    # Violin
    try:
        import examples.violinplot_examples as vexp

        vexp.main()
    except Exception as e:
        print(f"✗ Violin examples failed: {e}")
    # Violin integration
    try:
        import examples.violinplot_extra_examples as vextra

        vextra.main()
    except Exception as e:
        print(f"✗ Violin extra examples failed: {e}")

    # Boxplot
    try:
        import examples.boxplot_examples as bexp

        bexp.main()
    except Exception as e:
        print(f"✗ Boxplot examples failed: {e}")
    # Boxplot integration
    try:
        import examples.boxplot_extra_examples as bextra

        bextra.main()
    except Exception as e:
        print(f"✗ Boxplot extra examples failed: {e}")

    # Shift plot
    try:
        import examples.shiftplot_examples as shfexp

        shfexp.main()
    except Exception as e:
        print(f"✗ Shift plot examples failed: {e}")
    # Shift plot extra (integration + advanced)
    try:
        import examples.shiftplot_extra_examples as shfextra

        shfextra.main()
    except Exception as e:
        print(f"✗ Shift plot extra examples failed: {e}")

    # Correlation matrices
    try:
        import examples.correlation_matrix_example as cmexp

        cmexp.main()
    except Exception as e:
        print(f"✗ Correlation examples failed: {e}")
    # Correlation integration
    try:
        import examples.correlation_matrix_extra_examples as cmextra

        cmextra.main()
    except Exception as e:
        print(f"✗ Correlation extra examples failed: {e}")

    # Alluvial (generate titanic and iris images)
    try:
        import examples.alluvial_examples as aexp
        import matplotlib.pyplot as plt

        fig, ax = aexp.titanic_example()
        try:
            plt.tight_layout()
            examples_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "examples")
            plt.savefig(os.path.join(examples_dir, "alluvial_titanic_example.png"), dpi=300, bbox_inches="tight")
        except Exception:
            pass
        finally:
            plt.close(fig)

        fig, ax = aexp.iris_example()
        try:
            plt.tight_layout()
            examples_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "examples")
            plt.savefig(os.path.join(examples_dir, "alluvial_iris_example.png"), dpi=300, bbox_inches="tight")
        except Exception:
            pass
        finally:
            plt.close(fig)
    except Exception as e:
        print(f"✗ Alluvial examples failed: {e}")

    print("\nSummary:")
    expected = [
        # Violin
        "violin_example.png",
        "violin_2groups_example.png",
        "violin_3groups_example.png",
        "violin_integration_violin.png",
        "violin_integration_boxplot.png",
        # Boxplot
        "boxplot_example.png",
        "boxplot_2groups_example.png",
        "boxplot_3groups_example.png",
        "boxplot_integration_boxplot.png",
        "boxplot_integration_violin.png",
        # Shift
        "shift_plot_example_basic.png",
        "shift_plot_example.png",
        "shift_plot_with_diff_example.png",
        "shift_plot_integration_shift.png",
        "shift_plot_integration_boxplot.png",
        "shift_plot_advanced_wilcoxon.png",
        # Correlation
        "correlation_matrix_synthetic_example.png",
        "correlation_matrix_iris_3features.png",
        "correlation_matrix_iris_4features.png",
        "correlation_matrix_example.png",
        "correlation_integration_corr.png",
        "correlation_integration_boxplot.png",
        # Alluvial (generated by separate script)
        "alluvial_titanic_example.png",
        "alluvial_iris_example.png",
    ]
    examples_dir = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
    "examples"
    )
    for filename in expected:
        filepath = os.path.join(examples_dir, filename)
        status = "✓" if os.path.exists(filepath) else "✗"
        print(f"  {status} {filename}")


if __name__ == "__main__":
    main()
