#!/usr/bin/env python3
import uuid, hashlib, base64, subprocess

groups = {
    "DD": "da915d4f72fa42f68509",
    "KPT": "adba740446b0413b89ab",
    "Personal": "cc9168e7b8d7437499c2",
}

acct = "d898edfkclh0009hc94g"

# Delete old proxmox-lxc setup key
subprocess.run(["docker", "exec", "-i", "netbird-server", "sqlite3", "/var/lib/netbird/store.db",
    "DELETE FROM setup_keys WHERE name = 'proxmox-lxc';"], check=True)

for gname, gid in groups.items():
    plain = str(uuid.uuid4()).upper()
    h = hashlib.sha256(plain.encode()).digest()
    encoded = base64.b64encode(h).decode()

    key_id = uuid.uuid4().hex[:20]

    sql = f"""INSERT INTO setup_keys (id, account_id, key, key_secret, name, type, created_at, expires_at, updated_at, revoked, used_times, usage_limit, ephemeral, auto_groups)
VALUES ('{key_id}', '{acct}', '{encoded}', '{plain[:4]}****', '{gname}-setup', 'reusable', datetime('now'), datetime('now', '+10 years'), datetime('now'), 0, 0, 0, 0, '["{gid}"]');"""

    subprocess.run(["docker", "exec", "-i", "netbird-server", "sqlite3", "/var/lib/netbird/store.db", sql], check=True)
    print(f"=== {gname} ===")
    print(f"Setup-Key: {plain}")
    print()

subprocess.run(["docker", "exec", "-i", "netbird-server", "sqlite3", "-line", "/var/lib/netbird/store.db",
    "SELECT name, key, auto_groups, used_times FROM setup_keys WHERE name LIKE '%-setup';"], check=True)
