import plotly.graph_objs as go

# Defines the preferred font stack for text in reports.
# The list is ordered by preference. "sans-serif" is a widely available
# generic fallback. Specific fonts can be prepended to this list if desired
# for more control over typography, assuming those fonts are available on the
# system rendering the report.
preferred_fonts = [
    "sans-serif",
]

# Provides the default color palette used for charts and other themed visual elements
# within reports generated by the report_creator library.
# These colors are applied to Plotly charts via the `get_rc_theme` template's colorway.
# The palette is designed for good visual distinction and aesthetic appeal.
report_creator_colors = [
    "#ffdd00",  # Vivid Yellow
    "#5fd83d",  # Bright Green
    "#ffb617",  # Warm Orange
    "#44a124",  # Forest Green
    "#f31a7c",  # Strong Pink/Magenta
    "#d18f06",  # Dark Ochre/Gold
    "#1f6f05",  # Deep Green
    "#AFEEEE",  # Pale Turquoise (Light Cyan/Blue)
    "#1a1100",  # Very Dark Brown (near black for contrast)
]


def get_rc_theme() -> go.layout.Template:
    """
    Returns a Plotly layout template (`plotly.graph_objs.layout.Template`)
    for the "Report Creator" (rc) theme, ensuring a consistent and clean
    visual style for charts.

    This theme defines several key visual aspects for Plotly charts:
    -   **Layout Settings:**
        -   `paper_bgcolor`: Sets the background of the area around the plot to "white".
        -   `plot_bgcolor`: Sets the background of the plotting area itself to "white".
        -   `colorway`: Uses the `report_creator_colors` list as the default color
            sequence for chart traces (lines, bars, markers, etc.).
        -   `autotypenumbers`: Set to "strict" to ensure data types (especially numbers)
            are strictly interpreted by Plotly, preventing potential misinterpretations.
        -   `hovermode`: Set to "closest", meaning that hover interactions will display
            information for the data point closest to the cursor.
        -   `font`:
            -   `family`: Applies the first font from `preferred_fonts` (typically "sans-serif").
            -   `size`: Sets a base font size of 12.
            -   `color`: Sets the default font color to "black".
        -   `xaxis` & `yaxis`:
            -   `automargin`: Set to True, allowing Plotly to automatically adjust margins
                to fit axis titles, tick labels, etc., preventing them from being cut off.
            -   `title.standoff`: Adds a 30-pixel standoff for axis titles to avoid
                overlap with tick labels.
        -   `title`:
            -   `x`: Sets the default chart title alignment to the left (x=0.05, relative
                to the plot area).
    -   **Data Specifics:**
        -   `pie`: Includes `automargin: True` specifically for pie charts to ensure
            that slice labels fit well within the chart area.

    Returns:
        go.layout.Template: A Plotly template object that can be applied to figures
                            or set as a default theme (e.g., `pio.templates.default = get_rc_theme()`).
    """
    return go.layout.Template(
        layout={
            "paper_bgcolor": "white",
            "plot_bgcolor": "white",
            "colorway": report_creator_colors,
            "autotypenumbers": "strict",  # Ensures strict data type interpretation
            "hovermode": "closest",  # Shows hover information for the nearest data point
            "font": {
                "family": preferred_fonts[0] if preferred_fonts else "sans-serif",
                "size": 12,
                "color": "black",
            },
            "xaxis": {
                "automargin": True,  # Automatically adjust margins for x-axis elements
                "title": {"standoff": 30},  # Space between axis title and tick labels
            },
            "yaxis": {
                "automargin": True,  # Automatically adjust margins for y-axis elements
                "title": {"standoff": 30},  # Space between axis title and tick labels
            },
            "title": {
                "x": 0.05,  # Align title to the left
                # 'xanchor': 'left' # Could be added for more precise left alignment
            },
        },
        data={
            # Specific settings for pie charts
            "pie": [{"automargin": True}]  # Ensure pie chart labels fit
        },
    )
