from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import PP_ALIGN, MSO_AUTO_SIZE
from pathlib import Path
import shutil

src=Path('/home/agent/260603_GaM_Gemeinsam_erfolgreich_dank_klar_definierten_Prozessen_Rev01.pptx')
out_dir=Path('/home/agent/erne_presentation_review')
out=out_dir/'260603_GaM_Gemeinsam_erfolgreich_dank_klar_definierten_Prozessen_Rev02_MG_optimiert.pptx'
shutil.copy2(src,out)
prs=Presentation(str(out))
W,H=prs.slide_width, prs.slide_height
BLUE=RGBColor(0,80,145); DARK=RGBColor(31,45,61); GREY=RGBColor(102,112,128); WHITE=RGBColor(255,255,255)

def set_textbox(shape, text, size=18, bold=False, color=DARK, align=None):
    shape.text=text
    tf=shape.text_frame; tf.word_wrap=True
    try: tf.auto_size=MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
    except Exception: pass
    for p in tf.paragraphs:
        if align is not None: p.alignment=align
        for r in p.runs:
            r.font.name='Aptos'; r.font.size=Pt(size); r.font.bold=bold; r.font.color.rgb=color

def add_text(slide,x,y,w,h,text,size=10,bold=False,color=DARK,align=None,margin=0.04):
    sh=slide.shapes.add_textbox(x,y,w,h)
    tf=sh.text_frame; tf.clear(); tf.word_wrap=True
    tf.margin_left=Inches(margin); tf.margin_right=Inches(margin); tf.margin_top=0; tf.margin_bottom=0
    for i,line in enumerate(text.split('\n')):
        p=tf.paragraphs[0] if i==0 else tf.add_paragraph(); p.text=line
        if align: p.alignment=align
        for r in p.runs:
            r.font.name='Aptos'; r.font.size=Pt(size); r.font.bold=bold; r.font.color.rgb=color
    sh.line.fill.background()
    return sh

def add_rect(slide,x,y,w,h,fill=BLUE,radius=True):
    shp=slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE if radius else MSO_SHAPE.RECTANGLE,x,y,w,h)
    shp.fill.solid(); shp.fill.fore_color.rgb=fill
    shp.line.fill.background()
    return shp

def add_badge(slide,label,step=None):
    # compact, stays inside existing beige CI area on most slides
    x=W-Inches(2.85); y=Inches(0.22); w=Inches(2.55); h=Inches(0.38)
    add_rect(slide,x,y,w,h,BLUE,True)
    txt=(f'{step}  ' if step else '')+label
    add_text(slide,x+Inches(0.08),y+Inches(0.08),w-Inches(0.16),Inches(0.13),txt,size=8.5,bold=True,color=WHITE,align=PP_ALIGN.CENTER,margin=0)

def replace_all_text(slide,repls):
    for sh in slide.shapes:
        if not hasattr(sh,'text_frame'): continue
        for p in sh.text_frame.paragraphs:
            for r in p.runs:
                t=r.text
                for old,new in repls.items(): t=t.replace(old,new)
                r.text=t

repls={'Beschaffun':'Beschaffung','durch das Ganze Projekte':'durch das ganze Projekt','ev Technische':'evtl. technische','LV versand':'LV-Versand','zum beachten':'zu beachten','Fehlkalkulation Budget !':'Budget-Fehlkalkulation!','Ab Gebote':'Abgebote'}
for i in range(19): replace_all_text(prs.slides[i],repls)

# Slide 1: strong but CI-compatible thesis strip in free lower area
s=prs.slides[0]
add_rect(s,Inches(0.55),Inches(6.08),Inches(11.8),Inches(0.40),BLUE,True)
add_text(s,Inches(0.82),Inches(6.18),Inches(11.25),Inches(0.12),'Klar definierte Prozesse sichern Preis, Termin und Qualität.',size=14,bold=True,color=WHITE,align=PP_ALIGN.CENTER,margin=0)
add_badge(s,'Marcel Gasser','Teil 1/2')

# Slide 2: cleaner objectives only, no overlay over existing bottom claim
s=prs.slides[1]
for sh in s.shapes:
    if hasattr(sh,'text') and 'Gemeinsames Verständnis' in sh.text:
        set_textbox(sh,'• Gemeinsames Verständnis für den aktuellen Beschaffungsprozess\n\n• Aufzeigen, wo Beschaffung den Projekterfolg beeinflusst\n\n• Grundsätze guter Vergaben: Preis, Termin und Qualität\n\n• Schnittstellen PL / Backoffice / Beschaffung kennen\n\n• Präsentation als späteres Nachschlagewerk nutzbar machen',size=17,color=DARK)
add_badge(s,'Zielbild','01')

# Light-touch section badges for orientation. No additional bottom overlays; process details remain fully visible.
labels=[None,None,('Grundsätze','02'),('Leitfaden','03'),('Praxishebel','04'),('Ausschreibung und Vergabe','05'),
        ('Grundlagen klären','P1'),('Kontrollfragen','P2'),('Anfragen','P3'),('Varianten und Verhandlung','P4'),('Auswertung und Vergabe','P5'),('Absage und Vertrag','P6'),
        ('Weitere Prozesse','06'),('Backoffice-Schnittstelle','07'),('Rollen klären','B1'),('Bearbeitung','B2'),('Prozessdetail','B3'),('Signatur / Abschluss','B4'),('Werkvertrag','08')]
for i,item in enumerate(labels):
    if item and i not in [0,1]: add_badge(prs.slides[i],item[0],item[1])

# Slide 19: small decision reminder placed in existing free lower-right white area
s=prs.slides[18]
add_rect(s,Inches(7.50),Inches(5.08),Inches(4.22),Inches(0.55),RGBColor(245,248,251),True)
box=s.shapes[-1]; box.line.color.rgb=BLUE
add_text(s,Inches(7.70),Inches(5.18),Inches(3.82),Inches(0.18),'Merksatz: Sicherheiten früh prüfen – nicht erst beim unterschriftsreifen Werkvertrag.',size=9.5,bold=True,color=BLUE,align=PP_ALIGN.CENTER,margin=0)

prs.save(str(out))
print(out)
