#!/usr/bin/env python3
"""Folie 2 – Grenzen der Vault-Integration (adesso-Styling).
Kette: Auftrag -> Auftragsannahme -> Schattenbuchhaltung (SPOT) -> Vault ->
Zusammenführung."""

import os
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.enum.shapes import MSO_SHAPE
from adesso_style import (WHITE, INK, CORAL, LIGHT, RECF, F, FC,
                          alpha, line_alpha, paint_background, chrome)

prs = Presentation()
prs.slide_width  = Inches(13.333)
prs.slide_height = Inches(7.5)
s = prs.slides.add_slide(prs.slide_layouts[6])


def shp(kind, x, y, w, h, fill=None, line=None, lw=Pt(1), rot=0):
    sh = s.shapes.add_shape(kind, x, y, w, h)
    if fill is None: sh.fill.background()
    else: sh.fill.solid(); sh.fill.fore_color.rgb = fill
    if line is None: sh.line.fill.background()
    else: sh.line.color.rgb = line; sh.line.width = lw
    if rot: sh.rotation = rot
    sh.shadow.inherit = False
    return sh


def boxtext(sh, t, size=14, color=INK, bold=False, font=F):
    tf = sh.text_frame; tf.word_wrap = True
    tf.vertical_anchor = MSO_ANCHOR.MIDDLE
    tf.margin_left = tf.margin_right = Pt(4); tf.margin_top = tf.margin_bottom = Pt(1)
    p = tf.paragraphs[0]; p.alignment = PP_ALIGN.CENTER
    r = p.add_run(); r.text = t
    r.font.size = Pt(size); r.font.bold = bold; r.font.name = font; r.font.color.rgb = color


def txt(x, y, w, h, t, size=14, color=INK, bold=False, italic=False,
        align=PP_ALIGN.LEFT, spc=None, lead=1.1, anchor=MSO_ANCHOR.TOP, font=F):
    tb = s.shapes.add_textbox(x, y, w, h); tf = tb.text_frame
    tf.word_wrap = True; tf.vertical_anchor = anchor
    tf.margin_left = tf.margin_right = tf.margin_top = tf.margin_bottom = Pt(0)
    p = tf.paragraphs[0]; p.alignment = align; p.line_spacing = lead
    r = p.add_run(); r.text = t
    r.font.size = Pt(size); r.font.bold = bold; r.font.italic = italic
    r.font.name = font; r.font.color.rgb = color
    if spc: r.font._rPr.set('spc', str(spc))
    return tb


paint_background(s, prs.slide_width, prs.slide_height)
chrome(s, "Grenzen der Vault-Integration?", 2)

# ---- Linke Spalte ----------------------------------------------------------
def claim(y, t, bold=False, sub=None):
    shp(MSO_SHAPE.RECTANGLE, Inches(0.95), y + Inches(0.08), Inches(0.14), Inches(0.14), fill=CORAL)
    txt(Inches(1.28), y, Inches(4.5), Inches(0.4), t, size=16.5, color=WHITE, bold=bold)
    if sub:
        txt(Inches(1.28), y + Inches(0.34), Inches(4.5), Inches(0.32), sub,
            size=12.5, color=LIGHT, italic=True)


claim(Inches(2.2), "Middleware kapselt den Rand – den Kern nicht", bold=True)
claim(Inches(2.95), "Mischaufträge nur als Schattenmodell",
      sub="aufwändig · fragil · fehleranfällig")
claim(Inches(3.95), "die Fachwahrheit verlässt den Kern")

shp(MSO_SHAPE.RECTANGLE, Inches(0.95), Inches(4.95), Inches(0.5), Pt(3.5), fill=CORAL)
txt(Inches(0.95), Inches(5.12), Inches(4.85), Inches(1.1),
    "Die Schattenbuchhaltung wird zum Single Point of Truth – nicht Vault.",
    size=19, color=WHITE, bold=True, lead=1.08, font=FC)
txt(Inches(0.95), Inches(6.45), Inches(4.85), Inches(0.4),
    "Genau die Rolle, die Vault halten müsste.", size=13.5, color=LIGHT, italic=True)

# ===========================================================================
# RECHTS: Kette  Auftrag -> Auftragsannahme -> SPOT -> Vault -> Zusammenführung
# ===========================================================================
fx, fy, fw, fh = Inches(6.25), Inches(2.1), Inches(5.9), Inches(4.72)
panel = shp(MSO_SHAPE.ROUNDED_RECTANGLE, fx, fy, fw, fh, fill=WHITE)
alpha(panel, 10)
panel.line.color.rgb = WHITE; panel.line.width = Pt(1)
line_alpha(panel, 35)
txt(fx + Inches(0.3), fy + Inches(0.12), fw - Inches(0.6), Inches(0.3),
    "Auftragsweg – hin und zurück durch den Layer", size=12.5, color=WHITE, bold=True)

cxc = fx + Inches(2.05)
bw = Inches(2.7)
bx = cxc - bw / 2


def down(y, label=None):
    shp(MSO_SHAPE.DOWN_ARROW, cxc - Inches(0.11), y, Inches(0.22), Inches(0.22), fill=LIGHT)
    if label:
        txt(cxc + Inches(0.22), y - Inches(0.02), Inches(1.7), Inches(0.3), label,
            size=11, color=LIGHT, italic=True, anchor=MSO_ANCHOR.MIDDLE)


# 1) Auftrag
b = shp(MSO_SHAPE.ROUNDED_RECTANGLE, bx, fy + Inches(0.44), bw, Inches(0.40), fill=WHITE)
boxtext(b, "Auftrag · Mischauftrag", size=12.5, color=INK, bold=True)
down(fy + Inches(0.86))

# 2) Auftragsannahme (ersetzt altes LIS)
b = shp(MSO_SHAPE.ROUNDED_RECTANGLE, bx, fy + Inches(1.06), bw, Inches(0.40), fill=WHITE)
boxtext(b, "Auftragsannahme", size=13, color=INK, bold=True)
down(fy + Inches(1.48), "Magic")

# 3) Schattenbuchhaltung = Rahmen um den Vault-Kasten
frx, fry = bx - Inches(0.22), fy + Inches(1.72)
frw, frh = bw + Inches(0.50), Inches(2.22)
frame = shp(MSO_SHAPE.ROUNDED_RECTANGLE, frx, fry, frw, frh,
            fill=RGBColor(0xFF, 0xE3, 0x9B))
alpha(frame, 22)
frame.line.color.rgb = CORAL; frame.line.width = Pt(2)

# Überschrift + Positions-Mapping (oben im Rahmen)
txt(frx, fy + Inches(1.78), frw, Inches(0.28), "Schattenbuchhaltung",
    size=13, color=CORAL, bold=True, align=PP_ALIGN.CENTER)
txt(frx, fy + Inches(2.07), frw, Inches(0.24), "Positions-Mapping",
    size=11.5, color=WHITE, align=PP_ALIGN.CENTER)

# aktiver SPOT-Stern (rechts neben dem Rahmen)
shp(MSO_SHAPE.STAR_5_POINT, Inches(10.75), fy + Inches(1.80), Inches(0.5), Inches(0.5), fill=CORAL)
txt(Inches(9.95), fy + Inches(2.32), Inches(2.1), Inches(0.28), "Single Point of Truth",
    size=10.5, color=CORAL, bold=True, align=PP_ALIGN.CENTER)
down(fy + Inches(2.33))

# 4) Vault speichert (aufgesplittet) – im Rahmen
b = shp(MSO_SHAPE.ROUNDED_RECTANGLE, bx, fy + Inches(2.50), bw, Inches(0.70), fill=WHITE)
txt(bx, fy + Inches(2.55), bw, Inches(0.3), "Vault  ·  1:1  ·  Speicher",
    size=13, color=INK, bold=True, align=PP_ALIGN.CENTER)
for k in range(3):
    shp(MSO_SHAPE.ROUNDED_RECTANGLE, cxc - Inches(1.08) + k * Inches(0.78),
        fy + Inches(2.87), Inches(0.62), Inches(0.3), fill=RECF)
    txt(cxc - Inches(1.08) + k * Inches(0.78), fy + Inches(2.87), Inches(0.62), Inches(0.3),
        "Rec", size=9.5, color=INK, align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE)
# Geist-Stern (durchgestrichen) – die Wahrheit müsste hier liegen
gx = Inches(10.79)
gs = shp(MSO_SHAPE.STAR_5_POINT, gx, fy + Inches(2.55), Inches(0.42), Inches(0.42))
gs.fill.background(); gs.line.color.rgb = LIGHT; gs.line.width = Pt(1.25)
shp(MSO_SHAPE.RECTANGLE, gx - Inches(0.02), fy + Inches(2.73), Inches(0.46), Pt(2),
    fill=CORAL, rot=20)
txt(Inches(9.95), fy + Inches(3.02), Inches(2.1), Inches(0.3), "müsste hier liegen",
    size=10, color=LIGHT, align=PP_ALIGN.CENTER)
down(fy + Inches(3.22))

# 5) Daten zusammenführen (unten im Rahmen)
txt(frx, fy + Inches(3.42), frw, Inches(0.28), "Daten Zusammenführen",
    size=12.5, color=WHITE, bold=True, align=PP_ALIGN.CENTER)

# 6) Weiterverarbeitung – grauer Pfeil + weißer Kasten unter dem Rahmen
shp(MSO_SHAPE.DOWN_ARROW, cxc - Inches(0.11), fy + Inches(4.00), Inches(0.22), Inches(0.22),
    fill=RGBColor(0x8C, 0x8C, 0x8C))
b = shp(MSO_SHAPE.ROUNDED_RECTANGLE, bx, fy + Inches(4.26), bw, Inches(0.40), fill=WHITE)
boxtext(b, "Weiterverarbeitung", size=12.5, color=INK, bold=True)

# Rückweg (Lesen) – Pfeil links nach oben, Label vertikal daneben
ua = shp(MSO_SHAPE.UP_ARROW, fx + Inches(0.08), fy + Inches(1.55), Inches(0.26), Inches(2.30),
         fill=LIGHT)
alpha(ua, 55)
lt = s.shapes.add_textbox(fx - Inches(0.95), fy + Inches(2.40), Inches(2.2), Inches(0.3))
ltf = lt.text_frame; ltf.word_wrap = False
ltf.margin_left = ltf.margin_right = ltf.margin_top = ltf.margin_bottom = Pt(0)
lp = ltf.paragraphs[0]; lp.alignment = PP_ALIGN.CENTER
lr = lp.add_run(); lr.text = "Lesen: zurück durch den Layer"
lr.font.size = Pt(9.5); lr.font.name = F; lr.font.color.rgb = CORAL
lt.rotation = 270

out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Folie2.pptx")
prs.save(out)
print("saved", out)
