File size: 29,708 Bytes
fa58ff0
 
 
7b4d13c
fa58ff0
60dd41c
fa58ff0
 
1026e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6db565
 
 
af9a933
 
 
 
132dee6
 
 
 
 
 
c6db565
 
60dd41c
 
7a80c0b
60dd41c
 
 
 
 
c6db565
 
 
 
 
132dee6
60dd41c
af9a933
c6db565
 
 
60dd41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6db565
 
 
 
 
 
60dd41c
 
 
 
 
132dee6
c6db565
 
 
43b4d0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6db565
 
 
 
 
 
 
43b4d0f
 
 
 
 
 
 
c6db565
43b4d0f
 
 
 
 
 
 
 
46436ba
 
 
132dee6
 
 
46436ba
 
60dd41c
 
 
7a80c0b
60dd41c
 
 
46436ba
 
 
132dee6
60dd41c
af9a933
 
 
 
c6db565
 
af9a933
46436ba
 
60dd41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46436ba
 
 
 
 
 
af9a933
60dd41c
46436ba
c6db565
 
46436ba
 
132dee6
af9a933
132dee6
 
 
 
 
46436ba
60dd41c
 
 
46436ba
60dd41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46436ba
 
 
 
 
7b4d13c
 
 
46436ba
 
60dd41c
 
46436ba
 
 
 
 
 
60dd41c
 
46436ba
 
 
 
c6db565
46436ba
 
c6db565
fa58ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6db565
 
 
 
 
 
 
43b4d0f
46436ba
 
 
43b4d0f
 
1026e02
 
fa58ff0
 
 
 
 
1026e02
 
 
 
 
fa58ff0
1026e02
fa58ff0
 
1026e02
 
 
 
 
 
c6db565
 
1026e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa58ff0
 
 
 
 
358d27e
fa58ff0
 
 
 
 
 
 
1026e02
 
 
 
 
 
 
 
 
 
 
c6db565
1026e02
 
fa58ff0
1026e02
 
 
 
 
 
 
 
fa58ff0
 
1026e02
 
 
fa58ff0
 
 
 
 
 
1026e02
c6db565
 
1026e02
fa58ff0
 
 
c6db565
d3a0799
1026e02
d3a0799
1026e02
fa58ff0
 
 
 
 
 
 
 
 
 
 
 
 
1026e02
fa58ff0
 
 
 
 
1026e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa58ff0
 
 
 
 
 
 
1026e02
fa58ff0
1026e02
 
fa58ff0
 
 
1026e02
 
d3a0799
fa58ff0
1026e02
fa58ff0
 
 
 
396f68b
 
c6db565
396f68b
 
 
 
 
 
 
 
 
 
 
 
fa58ff0
1026e02
396f68b
 
1026e02
fa58ff0
396f68b
fa58ff0
c6db565
396f68b
1026e02
 
c6db565
46436ba
396f68b
 
7e4de95
46436ba
 
 
 
 
 
c6db565
 
 
 
 
396f68b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1026e02
 
 
 
 
 
 
 
 
 
c6db565
 
 
 
 
1026e02
 
 
 
 
 
 
 
c6db565
 
 
1026e02
 
 
 
 
 
 
 
 
 
c6db565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa58ff0
 
 
 
 
 
 
 
 
 
 
7b4d13c
fa58ff0
7b4d13c
fa58ff0
eb09d9d
7b4d13c
fa58ff0
 
 
c6db565
 
 
 
 
1026e02
 
 
 
 
 
 
 
 
 
c6db565
1026e02
 
c6db565
1026e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa58ff0
 
 
 
 
 
 
1026e02
 
 
 
 
 
c6db565
1026e02
 
c6db565
1026e02
fa58ff0
20f0fcc
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import pandas as pd
import streamlit as st

from config.config import CO2_UNIT, method_groups, method_order, method_tooltips, method_notes, secondary_resources, hidden_resource_names, section_intros
from utils.ui_helpers import show_bar
from utils.data_utils import strip_unit_suffix, get_other_method_variants, make_variant_name, default_constraint, get_resource_to_group


def _make_slider_cb(m):
    """on_change for the percent-cap slider: write the new value into constraints."""
    def _cb():
        st.session_state.constraints[m]["cap_value"] = st.session_state[f"slider_{m}"]
    return _cb


def _make_manual_cb(m):
    """on_change for the absolute-cap number_input: write the new value into constraints."""
    def _cb():
        v = st.session_state.get(f"manual_{m}")
        st.session_state.constraints[m]["cap_value"] = float(v) if v is not None else 0.0
    return _cb


def get_hidden_resources_for_method(m, method_to_primary_resources, cost_sliders_norm):
    """Return (required, optional) lists of hidden resource names for method m.

    Returns (None, None) if the method has at least one non-hidden primary resource
    or a custom batch already assigned.
    """
    primary = method_to_primary_resources.get(m, [])
    if not primary or not all(r in hidden_resource_names for r in primary):
        return None, None
    custom_resources = st.session_state.get("custom_resources", [])
    if any(m in b.get("methods", {}) for b in custom_resources):
        return None, None
    required = [r for r in primary if r in hidden_resource_names]
    optional = [
        r for r in cost_sliders_norm.get(m, {})
        if r in hidden_resource_names and (m, r) in secondary_resources
    ]
    return required, optional


def get_all_missing_required(m, cost_sliders_norm):
    """Return all required resources for method m that are not yet available.

    A resource (standard or hidden) is available if resource_caps.get(r, 0.0) > 0
    OR if a custom batch with group == r is assigned to m β€” a custom batch
    substitutes for its reference resource regardless of whether that resource
    is a generic "Other X" (hidden) one or a specifically named standard one
    (e.g. a "Basalt A" batch referencing "Basalt mineral"). A resource the user
    has explicitly disabled for this method via the "Related CDR Methods"
    popover (Tab 1) is treated as missing too, unless a custom batch covers it β€”
    otherwise tab4 silently zeroes its coefficient (via enabled_methods) while
    this tab still reports the method as active, a discrepancy that can lead to
    "No active methods" once every resource of a method is disabled this way.
    Secondary resources and resources with median <= 0 are excluded.

    Family pooling: when two or more of the method's resources share the same
    resource family (e.g. "Arable land" and "Other land" are both "Land" β€” see
    build_family_pools in optimisation.py), they are OR-substitutable rather
    than all-required. A resource in such a family is only reported as missing
    if every member of that family is unavailable; a family member currently
    enabled as a secondary resource (its slider toggled on in Tab 3) also
    counts as available, since it becomes a real substitute in the LP.

    Returns:
        list[str]: missing required resource names (standard and hidden combined).
    """
    resource_caps = st.session_state.get("resource_caps", {})
    custom_resources = st.session_state.get("custom_resources", [])
    enabled_methods = st.session_state.get("enabled_methods", {})
    method_costs = st.session_state.get("costs", {}).get(m, {})
    covered_by_batch = {
        b["group"] for b in custom_resources
        if m in b.get("methods", {})
    }
    resource_to_group = get_resource_to_group()

    def _available(r):
        if r in covered_by_batch:
            return True
        if resource_caps.get(r, 0.0) <= 0:
            return False
        return enabled_methods.get(r, {}).get(m, True)

    # Family membership includes this method's primary resources plus any
    # secondary resource currently switched on (cost > 0) β€” both act as real
    # substitutes for one another in the LP once active.
    family_members: dict[str, list[str]] = {}
    for r in cost_sliders_norm.get(m, {}):
        is_secondary = (m, r) in secondary_resources
        if is_secondary and method_costs.get(r, 0.0) <= 0:
            continue
        family_members.setdefault(resource_to_group.get(r, r), []).append(r)

    missing = []
    for r, coeff_dict in cost_sliders_norm.get(m, {}).items():
        if (m, r) in secondary_resources:
            continue
        if coeff_dict.get("median", 0.0) <= 0:
            continue
        siblings = family_members.get(resource_to_group.get(r, r), [r])
        if len(siblings) > 1:
            if not any(_available(s) for s in siblings):
                missing.append(r)
        elif not _available(r):
            missing.append(r)
    return missing


def get_uncovered_hidden_resources(m, cost_sliders_norm):
    """Return (required, optional) hidden resources for method m that leave it unconstrained.

    A hidden resource is "uncovered" only when the user has provided no resource
    at all from the same group β€” neither a standard cap (> 0 in tab1) nor a
    custom batch assigned to this method.  If at least one standard resource in
    the group has a positive cap, the method is already constrained by it and the
    missing hidden resource does not inflate results.

    Returns:
        tuple[list[str], list[str]]: (required, optional) uncovered hidden resource names.
            required β€” must be added for the method to run (not in secondary_resources).
            optional β€” in secondary_resources, shown as informational only.
            Returns ([], []) when nothing is uncovered.
    """
    custom_resources = st.session_state.get("custom_resources", [])
    # A hidden resource r (e.g. "Other mineral") is covered when any custom batch
    # whose group == r is assigned to this method β€” the batch name differs from
    # the group name (e.g. "Limestone" covers "Other mineral").
    covered_groups = {
        b["group"] for b in custom_resources
        if m in b.get("methods", {})
    }

    required, optional = [], []
    for r, coeff_dict in cost_sliders_norm.get(m, {}).items():
        if r not in hidden_resource_names:
            continue
        if coeff_dict.get("median", 0.0) <= 0:
            continue
        if r in covered_groups:
            continue
        if (m, r) in secondary_resources:
            optional.append(r)
        else:
            required.append(r)
    return required, optional


def _render_resource_status(m, cost_sliders_norm):
    """Render an expander showing all resources of method m with their availability status.

    Standard resources: βœ… if cap > 0 and not disabled for this method via the
        "Related CDR Methods" popover (Tab 1), ❌ otherwise β€” unless a custom
        batch covers it.
    Hidden resources:   βœ… if covered by a custom batch, ❌ if not.
    Secondary resources are shown in a separate section.

    Family pooling: when 2+ of the method's required resources share the same
    resource family (e.g. "Forest biomass" / "Other biomass" are both
    "Biomass" β€” see build_family_pools in optimisation.py), only one of them
    is actually needed. A family member that isn't set is shown as βœ…
    "optional" (not a blocking ❌) once a sibling already satisfies the
    family, since adding it is a bonus, not a requirement.
    """
    resource_caps   = st.session_state.get("resource_caps", {})
    custom_resources = st.session_state.get("custom_resources", [])
    enabled_methods = st.session_state.get("enabled_methods", {})
    resource_to_group = get_resource_to_group()
    # Map resource group β†’ list of batch names covering it for method m (any
    # named standard resource or hidden "Other X" resource can be substituted
    # by a custom batch referencing it).
    covered_by_batch: dict[str, list[str]] = {}
    for b in custom_resources:
        if m in b.get("methods", {}):
            covered_by_batch.setdefault(b["group"], []).append(b["name"])

    all_res = cost_sliders_norm.get(m, {})

    def _is_available(r):
        batch_names = covered_by_batch.get(r)
        if r in hidden_resource_names:
            return bool(batch_names)
        disabled_for_method = not enabled_methods.get(r, {}).get(m, True)
        return (resource_caps.get(r, 0.0) > 0 and not disabled_for_method) or bool(batch_names)

    # Group this method's required (non-secondary) resources by family so
    # siblings can be recognised as OR-substitutable below.
    primary_by_family: dict[str, list[str]] = {}
    for r, coeff_dict in all_res.items():
        if coeff_dict.get("median", 0.0) <= 0 or (m, r) in secondary_resources:
            continue
        primary_by_family.setdefault(resource_to_group.get(r, r), []).append(r)

    required_rows = []
    optional_rows = []

    for r, coeff_dict in all_res.items():
        if coeff_dict.get("median", 0.0) <= 0:
            continue
        batch_names = covered_by_batch.get(r)
        available = _is_available(r)
        if r in hidden_resource_names:
            # Show actual batch name(s) instead of the generic "Other X" group
            display_r = ", ".join(batch_names) if batch_names else r
            source = "Add as new resource in Resource inputs"
        else:
            disabled_for_method = not enabled_methods.get(r, {}).get(m, True)
            display_r = ", ".join(batch_names) if (not resource_caps.get(r, 0.0) > 0 and batch_names) else r
            source = (
                "Re-enable this method in the Related CDR Methods popover (Resource inputs tab)"
                if disabled_for_method and resource_caps.get(r, 0.0) > 0
                else "Set quantity in Resource inputs"
            )

        is_secondary = (m, r) in secondary_resources
        siblings = primary_by_family.get(resource_to_group.get(r, r), [r]) if not is_secondary else [r]
        available_siblings = [s for s in siblings if s != r and _is_available(s)]

        if available and len(siblings) > 1:
            icon, hint, is_blocking = "βœ…", "", False
        elif not available and available_siblings:
            # Family already satisfied by a sibling β€” not a blocker, just a bonus.
            icon, is_blocking = "βœ…", False
            hint = (
                f"{', '.join(available_siblings)} already covers this "
                f"(only one of {', '.join(siblings)} is required). You can still add "
                f"{r} too."
            )
        elif not available and len(siblings) > 1:
            icon, is_blocking = "❌", True
            hint = f"{source} (only one of {', '.join(siblings)} is required)"
        else:
            icon, is_blocking = ("βœ…", False) if available else ("❌", True)
            hint = "" if available else source

        row = (icon, display_r, hint, is_blocking)

        if is_secondary:
            optional_rows.append(row)
        else:
            required_rows.append(row)

    with st.expander("Why is this method disabled?", expanded=False):
        note = method_notes.get(m)
        if note:
            st.info(note, icon=":material/lightbulb:")
        if required_rows:
            st.markdown("**Required resources**")
            for _, r, hint, is_blocking in required_rows:
                color = "#FF5F4D" if is_blocking else "#55C95B"
                label = f'<span style="color:{color};font-weight:600"><strong>{r}</strong></span>'
                if hint:
                    label += f' <span style="color:#888;font-style:italic;font-weight:400">: {hint}</span>'
                st.markdown(label, unsafe_allow_html=True)
        if optional_rows:
            st.markdown("**Optional resources**")
            for _, r, hint, is_blocking in optional_rows:
                color = "#FF5F4D" if is_blocking else "#55C95B"
                label = f'<span style="color:{color};font-weight:600"><strong>{r}</strong></span>'
                if hint:
                    label += f' <span style="color:#888;font-style:italic;font-weight:400">: {hint}</span>'
                st.markdown(label, unsafe_allow_html=True)
            st.caption("Optional resources can be added in the **Resource coefficients** tab (tab 3).")


def render_method(m, cols, i, cost_sliders_norm):
    """Render the constraint widget group (toggle + cap slider/input) for one method.

    Writes the result into st.session_state.constraints[m].

    Args:
        m (str): CDR method name.
        cols (list): list of Streamlit column objects.
        i (int): index used to pick the column (i % 3).

    Returns:
        None
    """
    with cols[i % 3]:
        tooltip = method_tooltips.get(m, None)

        if m not in st.session_state.constraints:
            st.session_state.constraints[m] = default_constraint()

        constraint = st.session_state.constraints[m]

        toggle_key = f"toggle_{m}"
        cap_type_key = f"cap_type_{m}"

        # Block the method if any required resource is missing (standard at 0 or
        # hidden resource not covered by a custom batch).  Run this before the
        # regular st.toggle so we never touch toggle_{m} for blocked methods β€”
        # the pre-render sync already set it and Streamlit would raise
        # "cannot be modified after widget is instantiated" otherwise.
        missing_required = get_all_missing_required(m, cost_sliders_norm)
        if missing_required:
            st.session_state.constraints[m]["active"] = False
            st.session_state.pop(toggle_key, None)
            st.toggle(m, value=False, key=toggle_key, disabled=True, help=tooltip)
            _render_resource_status(m, cost_sliders_norm)
            return

        was_active = constraint["active"]
        st.session_state.setdefault(toggle_key, was_active)

        is_active = st.toggle(m, key=toggle_key, help=tooltip)

        st.session_state.constraints[m]["active"] = is_active

        if is_active and not was_active:
            st.session_state.constraints[m]["cap_type"] = "percent"
            st.session_state.constraints[m]["cap_value"] = 100
            st.session_state[cap_type_key] = "percent"

        if not is_active:
            st.session_state.pop(cap_type_key, None)
            return

        # If cap_type is invalid (e.g. "nan" from a corrupted import), show a
        # warning and reset to "percent" so the radio and slider render correctly.
        # The flag is cleared once the user interacts with the radio.
        if constraint.get("_cap_type_invalid") or constraint.get("cap_type") not in ("percent", "absolute"):
            st.warning(
                f"Invalid **cap_type** value (`{constraint.get('cap_type')}`) loaded from file. "
                "Select a constraint type below to fix it.", 
                icon=":material/warning:"
            )
            constraint["cap_type"] = "percent"
            constraint["cap_value"] = 100
            constraint.pop("_cap_type_invalid", None)
            st.session_state.pop(cap_type_key, None)
            st.session_state.pop(f"slider_{m}", None)
            st.session_state.pop(f"manual_{m}", None)

        # cap_type radio keeps a key so the user's choice persists across
        # reruns; slider and number_input are keyless so they always read
        # directly from the constraint β€” no stale session-state value possible.
        st.session_state.setdefault(cap_type_key, constraint["cap_type"])

        # Capture the cap_type that was active in the previous render so we can
        # detect a mode switch (absolute β†’ percent) below.
        old_cap_type = constraint.get("cap_type", "percent")

        cap_type = st.radio(
            "Cap value type",
            ["percent", "absolute"],
            horizontal=True,
            key=cap_type_key,
            format_func=lambda x: "% of its total potential"
            if x == "percent"
            else f"Absolute ({CO2_UNIT}/yr)",
            label_visibility="collapsed",
        )

        st.session_state.constraints[m]["cap_type"] = cap_type

        # When the user switches from absolute β†’ percent the stored cap_value is
        # an absolute number (MtCOβ‚‚/yr) which is meaningless as a percentage.
        # Reset both the constraint and the slider key to the default 100 % so
        # neither value nor widget is "stuck" at whatever number was left over.
        if cap_type == "percent" and old_cap_type == "absolute":
            constraint["cap_value"] = 100
            st.session_state[f"slider_{m}"] = 100
            st.session_state.pop(f"manual_{m}", None)

        if cap_type == "absolute" and old_cap_type == "percent":
            constraint["cap_value"] = 0.0
            st.session_state.pop(f"manual_{m}", None)
            st.session_state.pop(f"slider_{m}", None)

        if cap_type == "percent":
            # Seed the slider key if absent or if it's spuriously 0 while the
            # constraint still records a non-zero intent.  constraint["cap_value"]
            # is only written via on_change callbacks (user-initiated), so it
            # faithfully reflects the user's last explicit choice even across tab
            # navigations β€” making the discrepancy check reliable.
            _cur = st.session_state.get(f"slider_{m}")
            if _cur is None or (_cur == 0 and int(constraint["cap_value"]) > 0):
                st.session_state[f"slider_{m}"] = int(constraint["cap_value"])

            value = st.slider(
                "Max share (%)", 0, 100, step=5,
                key=f"slider_{m}",
                on_change=_make_slider_cb(m),
            )

            if value == 0:
                st.warning("Cap is 0% but method is active.", icon=":material/warning:")

        else:

            if f"manual_{m}" not in st.session_state and constraint.get("cap_value"):
                st.session_state[f"manual_{m}"] = float(constraint["cap_value"])

            value = st.number_input(
                f"Max cap ({CO2_UNIT}/yr)",
                min_value=0.0,
                value=None,
                format="%.4f",
                key=f"manual_{m}",
                placeholder="0.0000",
                on_change=_make_manual_cb(m),
            )

            if not value:
                st.warning(f"Cap is 0 {CO2_UNIT}/yr but method is active.", icon=":material/warning:")

def reset_all_constraints():
    """Reset all method constraints to their default values and rerun the app.

    Returns:
        None
    """
    for m in method_order:
        st.session_state.constraints[m] = default_constraint()
        for key in [f"toggle_{m}", f"cap_type_{m}", f"slider_{m}", f"manual_{m}"]:
            st.session_state.pop(key, None)
    st.success("All method constraints reset to default.")
    st.rerun()


def _iter_display_methods(custom_resources):
    """Yield (display_name, original_method_name) for all methods.

    'Other' methods with custom batches are expanded into one entry per batch.
    All other methods yield (m, m).
    """
    for m in method_order:
        variants = get_other_method_variants(m, custom_resources)
        if variants:
            for batch in variants:
                yield make_variant_name(m, batch["name"]), m
        else:
            yield m, m


def build_summary_df():
    """Build a summary DataFrame of currently active methods and their caps.

    Returns:
        pd.DataFrame | None: index = method name, column "Cap value" as a formatted
                             string. Returns None if no method is active.
    """
    custom_resources = st.session_state.get("custom_resources", [])
    rows = {}
    for display_m, _ in _iter_display_methods(custom_resources):
        c = st.session_state.constraints.get(display_m, default_constraint())
        if not c["active"]:
            continue
        if c["cap_type"] == "percent":
            rows[display_m] = {"Cap value": f"{int(c['cap_value'])}%"}
        elif c["cap_type"] == "absolute":
            rows[display_m] = {"Cap value": f"{c['cap_value']:,g} {CO2_UNIT}/yr"}
        else:
            rows[display_m] = {"Cap value": f"⚠️ Invalid type ({c['cap_type']!r}) β€” fix in Tab 2"}
    return pd.DataFrame.from_dict(rows, orient="index") if rows else None



def refresh_active_methods(cost_sliders_norm):
    """Auto-activate/deactivate methods based on real resource availability.

    A method is auto-deactivated when it's missing a required resource, and
    auto-reactivated (only if it was previously auto-deactivated, not
    manually turned off by the user) once that resource becomes available
    again.

    This must run on every app render β€” not just when Tab 2 itself draws β€”
    otherwise a user who goes straight from Tab 1 to Tab 3/4/5 without
    visiting Tab 2 first finds every method stuck at its initial
    "active: False" state (set once at app startup), and the optimisation
    fails with "No active methods" despite correctly configured resources.
    Call this from app.py's shared init in addition to Tab 2's own
    _sync_constraint_widgets (which calls it too, for its widget-key sync).

    Args:
        cost_sliders_norm (dict): {method: {resource: {min, median, max}}} with
            unit suffixes stripped.

    Returns:
        set[str]: method names that were just (re)activated by this call.
    """
    auto_disabled = st.session_state.get("auto_disabled_methods", set())
    just_activated = set()

    for m, c in st.session_state.constraints.items():
        if get_all_missing_required(m, cost_sliders_norm):
            st.session_state.constraints[m]["active"] = False
            # Keep the method in auto_disabled so it gets re-enabled (and
            # fires just_activated) once the resource is added.
            auto_disabled.add(m)
            st.session_state.pop(f"toggle_{m}", None)
            st.session_state.pop(f"cap_type_{m}", None)
            st.session_state.pop(f"slider_{m}", None)
            st.session_state.pop(f"manual_{m}", None)
            continue

        if m in auto_disabled:
            st.session_state.constraints[m]["active"] = True
            auto_disabled.discard(m)
            just_activated.add(m)

    st.session_state["auto_disabled_methods"] = auto_disabled
    return just_activated


def _sync_constraint_widgets(cost_sliders_norm):
    """Drive all constraint widget keys from the constraint dict before any widget is drawn.

    Two modes:
    - force mode: after a file/scenario load (constraints_just_loaded flag) or for
      freshly activated methods (just_activated_methods). All keys are set
      unconditionally from the constraint.
    - normal mode: setdefault only; never overrides a value the user already set.

    Args:
        cost_sliders_norm (dict): {method: {resource: {min, median, max}}} with unit suffixes stripped.

    Returns:
        None
    """
    just_loaded = st.session_state.pop("constraints_just_loaded", False)
    just_activated = st.session_state.pop("just_activated_methods", set())
    just_activated |= refresh_active_methods(cost_sliders_norm)

    for m, c in st.session_state.constraints.items():
        force = just_loaded or (m in just_activated)
        if c["active"]:
            cap_type = c.get("cap_type", "percent")
            if force:
                st.session_state[f"toggle_{m}"] = True
                st.session_state[f"cap_type_{m}"] = cap_type
                if cap_type == "percent":
                    st.session_state[f"slider_{m}"] = int(c.get("cap_value", 100))
                    st.session_state.pop(f"manual_{m}", None)
                else:
                    cap_val = c.get("cap_value", 0.0)
                    if cap_val:
                        st.session_state[f"manual_{m}"] = float(cap_val)
                    else:
                        st.session_state.pop(f"manual_{m}", None)
                    st.session_state.pop(f"slider_{m}", None)
            else:
                st.session_state.setdefault(f"toggle_{m}", True)
                st.session_state.setdefault(f"cap_type_{m}", cap_type)
                if cap_type == "percent":
                    st.session_state.setdefault(f"slider_{m}", int(c.get("cap_value", 100)))
                    st.session_state.pop(f"manual_{m}", None)
                else:
                    cap_val = c.get("cap_value", 0.0)
                    if cap_val and f"manual_{m}" not in st.session_state:
                        st.session_state[f"manual_{m}"] = float(cap_val)
                    st.session_state.pop(f"slider_{m}", None)
        else:
            if force:
                st.session_state[f"toggle_{m}"] = False
            else:
                st.session_state.setdefault(f"toggle_{m}", False)
            st.session_state.pop(f"cap_type_{m}", None)
            st.session_state.pop(f"slider_{m}", None)
            st.session_state.pop(f"manual_{m}", None)


def render_constraints_summary():
    """Render the summary dataframe of active methods and their caps.

    Returns:
        None
    """
    st.markdown("""
        <p class="crra-sub-heading">Summary of Active methods and caps</p>""",
        unsafe_allow_html=True,
    )
    df = build_summary_df()
    if df is not None:
        df_display = df.reset_index().rename(columns={"index": "CDR Method"})
        df_display = df_display[["CDR Method", "Cap value"]]
        num_rows = len(df_display)
        st.dataframe(
            df_display,
            width='stretch',
            hide_index=True,
            height=(num_rows * 36 + 36),
            column_config={
                "CDR Method": st.column_config.TextColumn("CDR Method", width="large"),
                "Cap value": st.column_config.TextColumn("Cap value"),
            },
            row_height=36,
        )
    else:
        st.warning("No active methods selected.", icon=":material/warning:")


def render_tab(cost_sliders):
    """Render the Method Constraints tab.

    Args:
        cost_sliders (dict): full cost slider config {method: {resource: {min, median, max}}}.

    Returns:
        None
    """
    cost_sliders_norm = {
        m: {strip_unit_suffix(name): conf for name, conf in resources.items()}
        for m, resources in cost_sliders.items()
    }
    _sync_constraint_widgets(cost_sliders_norm)

    st.markdown('<h1 class="crra-heading">Method constraints</h1>', unsafe_allow_html=True)
    show_bar()
    
    st.markdown(
        """
        <p class="objective_title">OBJECTIVE</p>
        """,
        unsafe_allow_html=True,
    )

    st.markdown(
        f"""
        <div class="text_with_border">
            <p>{section_intros.get("tab2_constraints", "").format(CO2_UNIT=CO2_UNIT)}</p>
        </div>
        <br>
        """,
        unsafe_allow_html=True,
    )

    st.markdown('<p class="crra-sub-heading">Controls</p>', unsafe_allow_html=True)

    if st.button("RESET ALL", key="btn-reset", icon=":material/reset_settings:"):
        reset_all_constraints()

    custom_resources = st.session_state.get("custom_resources", [])

    # Build the full list of display names (variants replace "Other" methods with batches)
    all_display = list(_iter_display_methods(custom_resources))

    any_active = any(
        st.session_state.constraints.get(display_m, default_constraint()).get("active", True)
        for display_m, _ in all_display
    )
    # Activatable: variants are always activatable; standard methods only if they
    # A method is activatable when all its required resources are available.
    activatable = []
    for display_m, orig_m in all_display:
        if not get_all_missing_required(orig_m, cost_sliders_norm):
            activatable.append(display_m)

    if any_active:
        if st.button("Deactivate all", icon=":material/remove_done:", key="btn-deactivate-all"):
            for display_m, _ in all_display:
                st.session_state.constraints.setdefault(display_m, default_constraint())["active"] = False
                st.session_state[f"toggle_{display_m}"] = False
            st.rerun()
    else:
        if st.button("Activate all", icon=":material/done_all:", key="btn-activate-all"):
            for m in activatable:
                st.session_state.constraints.setdefault(m, default_constraint())["active"] = True
                st.session_state[f"toggle_{m}"] = True
            st.rerun()

    with st.expander("Manage methods", expanded=True, icon=":material/settings:"):
        for group, method_list in method_groups.items():
            st.markdown(f"""
                <p class="crra-group-name">{group}</p>""",
                unsafe_allow_html=True,
            )
            cols = st.columns(3)
            col_idx = 0
            for m in method_list:
                variants = get_other_method_variants(m, custom_resources)
                if variants:
                    for batch in variants:
                        vname = make_variant_name(m, batch["name"])
                        render_method(vname, cols, col_idx, cost_sliders_norm)
                        col_idx += 1
                else:
                    render_method(m, cols, col_idx, cost_sliders_norm)
                    col_idx += 1

    render_constraints_summary()

    if st.button("Resource coefficients β†’", key="btn-nav-next-constraints", type="primary", help="Optional: adjust resource efficiency coefficients per method."):
        st.session_state["_navigate_to"] = "Resource coefficients"
        st.rerun()