"""
s15 - Why mitigations keep failing.

2x2 grid: each mitigation gets its real description and why it doesn't apply
to this chain. Bottom punch quote nails the structural point.
"""

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

from manim import (
    Text, Rectangle, VGroup, FadeIn,
    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 S15MitigationsFail(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        title = slide_title(
            "Why mitigations keep failing",
            font_size=34, with_accent_rule=True,
        )
        title.move_to((0.0, 3.05, 0.0))

        def card(name, sub, body_text, color):
            head = Text(name, font=Fonts.SANS, font_size=Sizes.BODY - 4,
                        color=color, weight="BOLD")
            tag = Text(sub, font=Fonts.SERIF, font_size=Sizes.CAPTION - 2,
                       color=SecurityPalette.FG_MUTED, slant="ITALIC")
            txt = Text(body_text, font=Fonts.SANS, font_size=Sizes.CAPTION - 3,
                       color=SecurityPalette.FG)
            body = VGroup(head, tag, txt).arrange(DOWN, buff=0.18,
                                                  aligned_edge=LEFT)
            box = Rectangle(width=6.2, height=body.height + 0.55,
                            fill_color=color, fill_opacity=0.08,
                            stroke_color=color, stroke_width=2)
            body.move_to(box.get_center()).align_to(box, LEFT).shift(RIGHT * 0.35)
            return VGroup(box, body)

        c1 = card("ASLR", "Randomizes load addresses",
                  "Does NOT prevent the write itself.\nBypassed via a secondary info-leak\nbug in the same process.",
                  SecurityPalette.INFO)
        c2 = card("Stack Canaries", "Detects stack-frame tampering",
                  "Protects saved RIP on the stack.\nIrrelevant here - overflow targets\na heap-resident function pointer.",
                  SecurityPalette.WARN)
        c3 = card("Sandboxing", "Limits post-RCE damage",
                  "Does not prevent the initial RCE.\nSandbox escape is the next stage\nof a real chain.",
                  SecurityPalette.SAFE)
        c4 = card("PAC", "Pointer Authentication (ARM)",
                  "Signs pointers with a process secret.\nBypassed via pointer reuse or\nPAC-unenforced paths in ImageIO.",
                  SecurityPalette.DANGER)

        row1 = VGroup(c1, c2).arrange(RIGHT, buff=0.40)
        row2 = VGroup(c3, c4).arrange(RIGHT, buff=0.40)
        grid = VGroup(row1, row2).arrange(DOWN, buff=0.30)
        grid.move_to((0.0, 0.25, 0.0))

        punch_text = Text(
            "Each mitigation addresses WHERE or HOW  -  not WHY writing past a buffer is allowed.",
            font=Fonts.SANS, font_size=Sizes.CAPTION - 2,
            color=SecurityPalette.DANGER, weight="BOLD",
        )
        punch_box = Rectangle(
            width=punch_text.width + 0.6, height=punch_text.height + 0.40,
            fill_color=SecurityPalette.DANGER, fill_opacity=0.10,
            stroke_color=SecurityPalette.DANGER, stroke_width=2,
        )
        punch_text.move_to(punch_box.get_center())
        punch = VGroup(punch_box, punch_text)
        punch.move_to((0.0, Layout.BOTTOM_EDGE + 0.85, 0.0))

        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.play(FadeIn(row1, shift=0.1 * UP), run_time=0.5)
        self.play(FadeIn(row2, shift=0.1 * UP), run_time=0.5)
        self.play(FadeIn(punch, shift=0.2 * UP), run_time=0.5)
        self.wait(0.5)
        self.next_slide()
