import re

# Update index.html
with open('/var/www/html/undangan_digital/index.html', 'r') as f:
    html = f.read()

# Replace full names first
html = html.replace("Ahmad Ridwan", "Robert")
html = html.replace("Siti Nurhayati", "Jaenab")

# Replace short names
html = html.replace("Ahmad", "Robert")
html = html.replace("Siti", "Jaenab")

with open('/var/www/html/undangan_digital/index.html', 'w') as f:
    f.write(html)

# Update script.js
with open('/var/www/html/undangan_digital/script.js', 'r') as f:
    script = f.read()

script = script.replace("Ahmad Fauzi", "Budi Fauzi") # fix if "Ahmad" got replaced

# Implement localStorage for wishes
old_wishes_code = """const wishes = [
  { name: "Ibu Sari Dewi", attend: "Hadir", msg: "Selamat ya, semoga rumah tangganya sakinah mawaddah warahmah. Bahagia selalu! <iconify-icon icon=\\"mdi:flower\\" style=\\"vertical-align:-2px;\\"></iconify-icon>", time: "Baru saja" },
  { name: "Ahmad Fauzi", attend: "Hadir", msg: "Wah selamat! Semoga jadi keluarga yang harmonis dan penuh berkah dari Allah SWT.", time: "5 menit lalu" },
  { name: "Keluarga Besar Hasan", attend: "Hadir", msg: "Alhamdulillah, akhirnya hari yang ditunggu-tunggu tiba. Semoga langgeng dan bahagia dunia akhirat! <iconify-icon icon=\\"mdi:hands-pray\\" style=\\"vertical-align:-2px;\\"></iconify-icon>", time: "12 menit lalu" },
];"""

new_wishes_code = """const defaultWishes = [
  { name: "Ibu Sari Dewi", attend: "Hadir", msg: "Selamat ya, semoga rumah tangganya sakinah mawaddah warahmah. Bahagia selalu! <iconify-icon icon=\\"mdi:flower\\" style=\\"vertical-align:-2px;\\"></iconify-icon>", time: "Baru saja" },
  { name: "Budi Fauzi", attend: "Hadir", msg: "Wah selamat! Semoga jadi keluarga yang harmonis dan penuh berkah dari Allah SWT.", time: "5 menit lalu" },
  { name: "Keluarga Besar Hasan", attend: "Hadir", msg: "Alhamdulillah, akhirnya hari yang ditunggu-tunggu tiba. Semoga langgeng dan bahagia dunia akhirat! <iconify-icon icon=\\"mdi:hands-pray\\" style=\\"vertical-align:-2px;\\"></iconify-icon>", time: "12 menit lalu" },
];
let wishes = JSON.parse(localStorage.getItem('betawi_wishes')) || defaultWishes;"""

script = script.replace(old_wishes_code, new_wishes_code)

# Add localStorage.setItem to form submit
submit_code_old = "wishes.unshift({ name, attend, msg: msg || '(Tanpa pesan)', time: 'Baru saja' });\n  renderWishes();"
submit_code_new = "wishes.unshift({ name, attend, msg: msg || '(Tanpa pesan)', time: 'Baru saja' });\n  localStorage.setItem('betawi_wishes', JSON.stringify(wishes));\n  renderWishes();"
script = script.replace(submit_code_old, submit_code_new)

with open('/var/www/html/undangan_digital/script.js', 'w') as f:
    f.write(script)

print("Updated names and added localStorage to RSVP.")
