"""
mark_webs.py — #Synology/.../#SERVER/Y/WEBS (starý web Buzalka.cz/.com):
  galerijní alba (g1/Albums, g2data/albums) -> Rodina (potvrzeno uživatelem)
  zbytek (web šablony, moduly, skiny, e-shop) -> Odpad-cache
"""
import sys
import psycopg2

sys.stdout.reconfigure(encoding="utf-8")

DB = dict(host="192.168.1.76", port=5432, user="vladimir.buzalka",
          password="Vlado7309208104++", database="fotky_buzalkovi")

WEBS = "/mnt/user/ZalohaVsechObrazku/Tower1/#Synology/Public/#SERVER/Y/WEBS/%"

conn = psycopg2.connect(**DB)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()

# Galerijní alba -> Rodina
cur.execute("""
    UPDATE photos p SET wanted=TRUE, category='Rodina'
    FROM zaloha_obrazku z
    WHERE p.zaloha_id=z.id
      AND z.cesta_zalohy LIKE %s
      AND (z.cesta_zalohy LIKE '%%/Albums/%%' OR z.cesta_zalohy LIKE '%%/g2data/albums/%%')
      AND (p.wanted=FALSE OR p.category IS DISTINCT FROM 'Rodina')
""", (WEBS,))
print(f"  WEBS galerie -> Rodina: {cur.rowcount:,}")

# Zbytek WEBS -> Odpad-cache
cur.execute("""
    UPDATE photos p SET wanted=FALSE, category='Odpad-cache'
    FROM zaloha_obrazku z
    WHERE p.zaloha_id=z.id
      AND z.cesta_zalohy LIKE %s
      AND z.cesta_zalohy NOT LIKE '%%/Albums/%%'
      AND z.cesta_zalohy NOT LIKE '%%/g2data/albums/%%'
      AND p.category IS DISTINCT FROM 'Odpad-cache'
""", (WEBS,))
print(f"  WEBS zbytek  -> Odpad-cache: {cur.rowcount:,}")

conn.close()
print("Hotovo.")
