"""
s16 - The only structural fixes: Rust + ARM MTE.

Two-column: software (Rust in kernel, sudo-rs in Ubuntu 26.04 LTS)
            hardware (ARM MTE catches OOB at trap time)
"""

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from manim import (
    Text, Rectangle, VGroup, FadeIn, Write,
    UP, DOWN, LEFT, RIGHT, ORIGIN,
)
from src.slides._core import SceneSlide
from src.slides._theme import SecurityPalette, Fonts, Sizes, Layout
from src.slides._layout import slide_title


class S16Solutions(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        title = slide_title(
            "Remove the possibility",
            font_size=Sizes.TITLE - 8, with_accent_rule=True,
        )
        title.move_to((0.0, 2.7, 0.0))

        def solution_panel(title_txt, body_lines, color):
            head = Text(title_txt, font=Fonts.SANS, font_size=20,
                        color=color, weight="BOLD")
            lines = []
            for ln in body_lines:
                lines.append(Text(ln, font=Fonts.SANS, font_size=18,
                                  color=SecurityPalette.FG))
            body = VGroup(head, *lines).arrange(DOWN, buff=0.16, aligned_edge=LEFT)
            w = max(body.width, head.width) + 0.55
            box = Rectangle(width=w, height=body.height + 0.55,
                            fill_color=color, fill_opacity=0.12,
                            stroke_color=color, stroke_width=2)
            body.move_to(box.get_center()).align_to(box, LEFT).shift(RIGHT * 0.30)
            return VGroup(box, body)

        software = solution_panel(
            "Software: rewrite in memory-safe languages",
            [
                "Rust - bounds + borrow checker eliminate OOB by construction",
                "Mainline Linux since 6.1 (2022);  Ubuntu 26.04 LTS sudo-rs (Apr 2026)",
                "Open question: ImageIO and friends not yet rewritten",
            ],
            SecurityPalette.SAFE,
        )

        hardware = solution_panel(
            "Hardware: catch OOB at trap time",
            [
                "ARM MTE - 4-bit tag per 16-byte granule; trap on mismatch",
                "Would catch CVE-2025-43300 deterministically",
                "Open question: Apple has not enabled MTE on iOS",
            ],
            SecurityPalette.INFO,
        )

        cols = VGroup(software, hardware).arrange(DOWN, buff=0.40)
        cols.move_to((0.0, -0.55, 0))

        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.play(FadeIn(software), run_time=0.7)
        self.play(FadeIn(hardware), run_time=0.7)
        self.wait(0.5)
        self.next_slide()
