#!/usr/bin/env python3
path = '/var/www/runmystore/products.php'
with open(path, 'r', encoding='utf-8') as f:
    code = f.read()

changes = 0

# 1. SVG backgrounds - добави след <body>
old1 = '<body>'
new1 = """<body>

<!-- SVG Backgrounds -->
<div style="position:fixed;inset:0;pointer-events:none;z-index:0;overflow:hidden" aria-hidden="true">
  <img style="position:absolute;left:50%;top:0;transform:translateX(-25%);max-width:none;width:846px;height:594px" src="./images/page-illustration.svg" alt="">
  <img style="position:absolute;left:50%;top:400px;transform:translateX(-100%);max-width:none;width:760px;height:668px;opacity:.5" src="./images/blurred-shape-gray.svg" alt="">
  <img style="position:absolute;left:50%;top:440px;transform:translateX(-33%);max-width:none;width:760px;height:668px" src="./images/blurred-shape.svg" alt="">
</div>"""
if old1 in code:
    code = code.replace(old1, new1, 1); changes += 1; print("1. SVG backgrounds OK")
else:
    print("1. SKIP")

# 2. Добави 4 бутона долу (Камера/AI/Скенер/Добави) - ПРЕДИ bottom nav
old2 = '<nav class="bnav">'
new2 = """<!-- BOTTOM ACTION BAR -->
<div style="position:fixed;bottom:var(--nav-h);left:0;right:0;background:rgba(3,7,18,.95);backdrop-filter:blur(20px);border-top:1px solid rgba(99,102,241,.1);z-index:90;padding:10px 16px 8px">
  <div style="display:flex;align-items:center;justify-content:space-around;gap:8px">
    <div style="display:flex;flex-direction:column;align-items:center;gap:4px;cursor:pointer" onclick="openCamera('search')">
      <div style="width:50px;height:50px;border-radius:50%;background:rgba(99,102,241,.1);border:1.5px solid rgba(99,102,241,.25);display:flex;align-items:center;justify-content:center">
        <svg width="22" height="22" fill="none" viewBox="0 0 24 24" stroke="#a5b4fc" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M3 9a2 2 0 012-2h.93a2 2 0 001.664-.89l.812-1.22A2 2 0 0110.07 4h3.86a2 2 0 011.664.89l.812 1.22A2 2 0 0018.07 7H19a2 2 0 012 2v9a2 2 0 01-2 2H5a2 2 0 01-2-2V9z"/><circle cx="12" cy="13" r="3"/></svg>
      </div>
      <span style="font-size:9px;font-weight:700;color:#a5b4fc">Камера</span>
    </div>
    <div style="display:flex;flex-direction:column;align-items:center;gap:4px;cursor:pointer" onclick="openAIModal()">
      <div style="width:50px;height:50px;border-radius:50%;background:linear-gradient(135deg,#6366f1,#8b5cf6);display:flex;align-items:center;justify-content:center;box-shadow:0 0 18px rgba(99,102,241,.5)">
        <div style="display:flex;gap:2px;align-items:center;height:18px">
          <div style="width:3px;border-radius:2px;background:#fff;height:6px;animation:barD 1s ease-in-out infinite"></div>
          <div style="width:3px;border-radius:2px;background:#fff;height:14px;animation:barD 1s ease-in-out infinite .15s"></div>
          <div style="width:3px;border-radius:2px;background:#fff;height:18px;animation:barD 1s ease-in-out infinite .3s"></div>
          <div style="width:3px;border-radius:2px;background:#fff;height:10px;animation:barD 1s ease-in-out infinite .45s"></div>
          <div style="width:3px;border-radius:2px;background:#fff;height:6px;animation:barD 1s ease-in-out infinite .6s"></div>
        </div>
      </div>
      <span style="font-size:9px;font-weight:700;color:#a5b4fc">✦ AI</span>
    </div>
    <div style="display:flex;flex-direction:column;align-items:center;gap:4px;cursor:pointer" onclick="openCamera('scanner')">
      <div style="width:50px;height:50px;border-radius:50%;background:rgba(99,102,241,.1);border:1.5px solid rgba(99,102,241,.25);display:flex;align-items:center;justify-content:center">
        <svg width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="#a5b4fc" stroke-width="1.8"><rect x="2" y="2" width="5" height="5" rx="1"/><rect x="17" y="2" width="5" height="5" rx="1"/><rect x="2" y="17" width="5" height="5" rx="1"/><path d="M17 17h5v5"/><path d="M7 12H2m5 0v5M12 2v5m0 0h5"/></svg>
      </div>
      <span style="font-size:9px;font-weight:700;color:#a5b4fc">Скенер</span>
    </div>
    <?php if($can_add): ?>
    <div style="display:flex;flex-direction:column;align-items:center;gap:4px;cursor:pointer" onclick="openModal()">
      <div style="width:50px;height:50px;border-radius:50%;background:linear-gradient(135deg,#6366f1,#4f46e5);display:flex;align-items:center;justify-content:center;box-shadow:0 4px 14px rgba(99,102,241,.4)">
        <svg width="24" height="24" fill="none" viewBox="0 0 24 24" stroke="#fff" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/></svg>
      </div>
      <span style="font-size:9px;font-weight:700;color:#a5b4fc">Добави</span>
    </div>
    <?php endif; ?>
  </div>
</div>

<style>
@keyframes barD{0%,100%{transform:scaleY(1)}50%{transform:scaleY(.25)}}
</style>

<nav class="bnav">"""
if old2 in code:
    code = code.replace(old2, new2, 1); changes += 1; print("2. 4 bottom buttons OK")
else:
    print("2. SKIP")

# 3. Увеличи padding-bottom на body за да побере бутоните
old3 = "body{background:#030712;color:#e2e8f0;font-family:'Montserrat',sans-serif;margin:0;overflow-x:hidden;padding-bottom:80px}"
new3 = "body{background:#030712;color:#e2e8f0;font-family:'Montserrat',sans-serif;margin:0;overflow-x:hidden;padding-bottom:160px}"
if old3 in code:
    code = code.replace(old3, new3, 1); changes += 1; print("3. padding-bottom OK")
else:
    print("3. SKIP")

# 4. Добави AI modal функция ако я няма
if 'function openAIModal' not in code:
    old4 = 'function showToast(msg) {'
    new4 = """function openAIModal() {
  const d = document.getElementById('aiDrawer');
  if (d) { document.getElementById('ovl').classList.add('open'); d.classList.add('open'); }
}

function showToast(msg) {"""
    if old4 in code:
        code = code.replace(old4, new4, 1); changes += 1; print("4. openAIModal function OK")
    else:
        print("4. SKIP")
else:
    print("4. openAIModal already exists")

with open(path, 'w', encoding='utf-8') as f:
    f.write(code)
print(f"\nDone: {changes} patches applied")
