#!/usr/bin/env python3
"""Copy WowPlatter's already-downloaded release-cover WebPs into RecordGod's hashed image tree.

WowPlatter downloaded 60k+ covers to wp-content/uploads/wowplatter/release/<NN>/release-<id>.webp
(all WebP, ~150px, tiny). RecordGod serves /img/r/<release_id> from DISC_IMAGE_DIR/<h[:2]>/<h>.webp
where h = sha256(release_id) — so we stage them under that scheme, then rsync to the VPS image dir.

    python disc_images_backfill.py            # build /tmp/rg_disc_images
    rsync -a /tmp/rg_disc_images/ root@<vps>:/opt/recordgod/disc_images/

Server-side i.discogs fetch is Cloudflare-403'd, so these local copies are how RecordGod self-hosts.
"""
import hashlib
import pathlib
import re
import shutil
import sys

import pymysql

WP = "/opt/homebrew/var/www/monsterrobot.localsite"
UPLOADS = WP + "/wp-content/uploads/"
STAGE = pathlib.Path("/tmp/rg_disc_images")


def creds():
    t = pathlib.Path(WP + "/wp-config.php").read_text()
    g = lambda k: re.search(rf"'{k}',\s*'([^']*)'", t).group(1)
    return g("DB_NAME"), g("DB_USER"), g("DB_PASSWORD")


def main():
    name, user, pw = creds()
    c = pymysql.connect(unix_socket="/opt/homebrew/var/mysql/mysql.sock", user=user, password=pw,
                        database=name).cursor()
    # one primary cover per release (fall back to any image if no primary)
    c.execute("""SELECT release_id, local_url FROM wp_rmp_disc_release_image
                 WHERE local_url IS NOT NULL ORDER BY release_id, (type='primary') DESC""")
    seen, copied, missing = set(), 0, 0
    for release_id, local_url in c.fetchall():
        if release_id in seen:
            continue
        seen.add(release_id)
        m = re.search(r"/wp-content/uploads/(.+)$", local_url)
        if not m:
            continue
        src = pathlib.Path(UPLOADS + m.group(1))
        if not src.exists():
            missing += 1
            continue
        h = hashlib.sha256(str(release_id).encode()).hexdigest()
        dst = STAGE / h[:2] / f"{h}.webp"
        dst.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy(src, dst)
        copied += 1
        if copied % 5000 == 0:
            print(f"  {copied}…", file=sys.stderr)
    print(f"staged {copied} covers to {STAGE} ({missing} files missing on disk)", file=sys.stderr)


if __name__ == "__main__":
    main()
