"""
s14 - While we were writing this paper (intro), then Copy Fail detail.

Sub-slide 1: "While we were writing this paper..." with both CVEs as one-line
            teasers (Copy Fail + Dirty Frag).
Sub-slide 2: Rich Copy Fail spec sheet (the one with detail).
"""

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 S142026(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        intro_title = slide_title(
            "While we were writing this paper...",
            font_size=36, with_accent_rule=True,
        )
        intro_title.move_to((0.0, 2.20, 0.0))

        intro_sub = Text(
            "...two more memory-corruption CVEs landed in the Linux kernel.",
            font=Fonts.SERIF, font_size=Sizes.SUBTITLE - 10,
            color=SecurityPalette.FG_MUTED,
        )
        intro_sub.next_to(intro_title, DOWN, buff=0.40)

        def teaser(name, cve, when, oneliner, color):
            head = Text(f"{name}  -  {cve}",
                        font=Fonts.SANS, font_size=Sizes.BODY - 6,
                        color=color, weight="BOLD")
            when_t = Text(when, font=Fonts.MONO, font_size=Sizes.CAPTION - 2,
                          color=SecurityPalette.FG_MUTED)
            body = Text(oneliner, font=Fonts.SANS, font_size=Sizes.CAPTION - 1,
                        color=SecurityPalette.FG)
            inner = VGroup(head, when_t, body).arrange(DOWN, buff=0.15,
                                                       aligned_edge=LEFT)
            box = Rectangle(width=11.0, height=inner.height + 0.55,
                            fill_color=color, fill_opacity=0.10,
                            stroke_color=color, stroke_width=2)
            inner.move_to(box.get_center()).align_to(box, LEFT).shift(RIGHT * 0.35)
            return VGroup(box, inner)

        copy_teaser = teaser(
            "Copy Fail",  "CVE-2026-31431",
            "AF_ALG / algif_aead  -  01 May 2026",
            "732-byte script  ->  4-byte page-cache write  ->  root via setuid",
            SecurityPalette.WARN,
        )
        frag_teaser = teaser(
            "Dirty Frag", "CVE-2026-43284 + CVE-2026-43500",
            "xfrm-ESP + RxRPC  -  07 May 2026  (Hyunwoo Kim)",
            "Page-cache writes via IPsec ESP and RxRPC paths  ->  root",
            SecurityPalette.DANGER,
        )
        teasers = VGroup(copy_teaser, frag_teaser).arrange(DOWN, buff=0.30)
        teasers.move_to((0.0, -0.85, 0.0))

        intro_tag = Text(
            "Same CWE-787 class.  Same architectural cause.  Next: Copy Fail in detail.",
            font=Fonts.SANS, font_size=Sizes.CAPTION - 2,
            color=SecurityPalette.WARN, weight="MEDIUM",
        )
        intro_tag.move_to((0.0, Layout.BOTTOM_EDGE + 0.85, 0.0))

        self.play(FadeIn(intro_title, shift=0.2 * UP), run_time=0.6)
        self.play(FadeIn(intro_sub), run_time=0.4)
        self.next_slide()
        self.play(FadeIn(copy_teaser, shift=0.1 * UP), run_time=0.5)
        self.next_slide()
        self.play(FadeIn(frag_teaser, shift=0.1 * UP), run_time=0.5)
        self.play(FadeIn(intro_tag), run_time=0.4)
        self.wait(0.4)
        self.next_slide()

        # Clear intro before drawing detail sub-slide.
        self.remove(intro_title, intro_sub, copy_teaser, frag_teaser, intro_tag)

        eyebrow_t = Text(
            "2026  -  PATTERN CONFIRMED",
            font=Fonts.SANS, font_size=Sizes.EYEBROW - 4,
            color=SecurityPalette.SAFE, weight="MEDIUM",
        )
        eyebrow_t.move_to(
            (Layout.LEFT_EDGE + Layout.MARGIN_X + eyebrow_t.width / 2,
             Layout.TOP_EDGE - 0.30, 0.0)
        )

        # Date badge top-right
        date_text = Text(
            "01 May 2026",
            font=Fonts.MONO, font_size=Sizes.CAPTION - 2,
            color=SecurityPalette.FG, weight="BOLD",
        )
        date_box = Rectangle(
            width=date_text.width + 0.55, height=date_text.height + 0.30,
            fill_color=SecurityPalette.DANGER, fill_opacity=0.85,
            stroke_color=SecurityPalette.DANGER, stroke_width=0,
        )
        date_text.move_to(date_box.get_center())
        date_badge = VGroup(date_box, date_text)
        date_badge.move_to(
            (Layout.RIGHT_EDGE - Layout.MARGIN_X - date_badge.width / 2,
             Layout.TOP_EDGE - 0.30, 0.0)
        )

        title = slide_title(
            "Copy Fail (CVE-2026-31431):  732 bytes to root on every Linux distro",
            font_size=24, with_accent_rule=False,
        )
        title.move_to((0.0, Layout.TOP_EDGE - 1.10, 0.0))
        title.align_to(eyebrow_t, LEFT)

        def spec_row(label, value, value_color):
            lbl = Text(label, font=Fonts.MONO, font_size=Sizes.CAPTION - 6,
                       color=SecurityPalette.FG_MUTED, weight="MEDIUM")
            val = Text(value, font=Fonts.SANS, font_size=Sizes.CAPTION - 5,
                       color=value_color, weight="MEDIUM")
            return lbl, val

        left_rows = [
            spec_row("Component:", "Linux kernel algif_aead crypto template",
                     SecurityPalette.FG),
            spec_row("Bug:",       "4-byte write into page cache (any readable file)",
                     SecurityPalette.DANGER),
            spec_row("Impact:",    "Unprivileged local user  ->  root",
                     SecurityPalette.DANGER),
            spec_row("CVSS:",      "7.8  High",
                     SecurityPalette.WARN),
            spec_row("Affected:",  "Linux 4.14 (2017)  ->  6.19.12  (all distros)",
                     SecurityPalette.FG),
            spec_row("PoC:",       "Public 732-byte Python script",
                     SecurityPalette.SAFE),
            spec_row("Patch:",     "Available (Ubuntu, RHEL, kernel.org)",
                     SecurityPalette.SAFE),
            spec_row("Discovery:", "AI-assisted static analysis (Xint Code)",
                     SecurityPalette.FG),
        ]

        spec_rows = VGroup()
        for lbl, val in left_rows:
            row = VGroup(lbl, val).arrange(RIGHT, buff=0.20)
            spec_rows.add(row)
        spec_rows.arrange(DOWN, buff=0.16, aligned_edge=LEFT)
        spec_box = Rectangle(
            width=spec_rows.width + 0.7, height=spec_rows.height + 0.55,
            fill_color=SecurityPalette.BG_PANEL, fill_opacity=0.55,
            stroke_color=SecurityPalette.RULE, stroke_width=1,
        )
        spec_rows.move_to(spec_box.get_center()).align_to(spec_box, LEFT).shift(RIGHT * 0.30)
        left_card = VGroup(spec_box, spec_rows)

        quote = Text(
            '"Discovered by AI static analysis -\nsame pattern, new subsystem"',
            font=Fonts.SERIF, font_size=Sizes.SUBTITLE - 14,
            color=SecurityPalette.FG, weight="BOLD", slant="ITALIC",
        )

        b1 = Text("Same CWE-787 class: write that\nshould not happen",
                  font=Fonts.SANS, font_size=Sizes.CAPTION - 4,
                  color=SecurityPalette.FG)
        b2 = Text("Page cache corrupted immediately\nno disk write needed",
                  font=Fonts.SANS, font_size=Sizes.CAPTION - 4,
                  color=SecurityPalette.WARN)
        b3 = Text("Complex crypto subsystem in C\nunchecked copy  ->  privesc",
                  font=Fonts.SANS, font_size=Sizes.CAPTION - 4,
                  color=SecurityPalette.DANGER)
        src = Text("Source: Microsoft Security Blog, 1 May 2026",
                   font=Fonts.SANS, font_size=Sizes.CAPTION - 6,
                   color=SecurityPalette.FG_DIM, slant="ITALIC")

        right_body = VGroup(quote, b1, b2, b3, src).arrange(
            DOWN, buff=0.30, aligned_edge=LEFT)
        right_box = Rectangle(
            width=right_body.width + 0.6, height=right_body.height + 0.55,
            fill_color=SecurityPalette.BG_PANEL, fill_opacity=0.55,
            stroke_color=SecurityPalette.RULE, stroke_width=1,
        )
        right_body.move_to(right_box.get_center()).align_to(right_box, LEFT).shift(RIGHT * 0.30)
        right_card = VGroup(right_box, right_body)

        cards = VGroup(left_card, right_card).arrange(RIGHT, buff=0.40,
                                                       aligned_edge=UP)
        cards.move_to((0.0, -0.3, 0.0))

        footer_tag = Text(
            "Microsoft Security Blog  |  Xint Code  |  1 May 2026  |  kernel.org patch available",
            font=Fonts.MONO, font_size=Sizes.CAPTION - 4,
            color=SecurityPalette.FG_DIM,
        )
        footer_tag.move_to((0.0, Layout.BOTTOM_EDGE + 0.85, 0.0))

        self.play(FadeIn(eyebrow_t, shift=0.1 * UP),
                  FadeIn(date_badge, shift=0.1 * DOWN), run_time=0.4)
        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.next_slide()

        self.play(FadeIn(left_card, shift=0.1 * LEFT), run_time=0.6)
        self.next_slide()

        self.play(FadeIn(right_card, shift=0.1 * RIGHT), run_time=0.6)
        self.play(FadeIn(footer_tag), run_time=0.3)
        self.wait(0.5)
        self.next_slide()
