from conjuror.plans.truebeam import OpenField, MLCLeafBoundaryAlignmentMode, TrueBeamMachine
from plotly import graph_objects as go

# EXACT: Field edges must align exactly with MLC boundaries
# This will raise an error since y1=-51, y2=51 don't align with MLC boundaries
try:
    procedure = OpenField(x1=-10, x2=10, y1=-51, y2=51, mlc_mode=MLCLeafBoundaryAlignmentMode.EXACT, beam_name="EXACT")
    machine = TrueBeamMachine(mlc_is_hd=False)
    procedure.compute(machine)
except ValueError as e:
    # Show error message on a plot similar to the others
    import textwrap
    fig = go.Figure()
    error_text = f"Error: {str(e)}"
    # Wrap text to fit within the figure (approximately 50 characters per line)
    wrapped_text = "<br>".join(textwrap.wrap(error_text, width=50))
    fig.add_annotation(
        text=wrapped_text,
        showarrow=False,
        font=dict(size=12, color="red"),
    )
    fig.update_layout(
        title="Beam: EXACT",
        plot_bgcolor="white",
        xaxis=dict(showticklabels=False),
        yaxis=dict(showticklabels=False),
    )
    fig