import json, subprocess

TOKEN = open("/Users/harvey/.openclaw/secrets/slack-token.txt").read().strip()

import urllib.request, urllib.parse
url = "https://slack.com/api/conversations.list?limit=200&types=public_channel,private_channel"
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {TOKEN}"})
data = json.loads(urllib.request.urlopen(req).read())

# Find contractor team channels
contractor_names = ["emily", "elba", "jessica", "samira", "vadim", "helen", "livia", "sharon", "paula", "lena", "jackie", "michelle", "taheela", "joan", "irais", "ravinder", "meron", "ashley", "isa", "julia", "pahulpreet"]
found = {}
for c in data.get("channels", []):
    if any(x in c["name"] for x in contractor_names) or c["name"].startswith("team-"):
        found[c["id"]] = c["name"]
        print(f"{c['id']} #{c['name']}")

# Now update config
path = "/Users/harvey/.openclaw/openclaw.json"
with open(path) as f:
    cfg = json.load(f)

slack_channels = cfg["channels"]["slack"]["channels"]

# Fix emilys-team (C02NFPKNZ1U) to monitor-only
if "C02NFPKNZ1U" in slack_channels:
    slack_channels["C02NFPKNZ1U"]["requireMention"] = True
    print("Fixed #emilys-team -> monitor only")

# Add/fix all contractor channels found
for cid, name in found.items():
    slack_channels[cid] = {"enabled": True, "requireMention": True}
    print(f"Set #{name} -> monitor only")

with open(path, "w") as f:
    json.dump(cfg, f, indent=2)

print(f"\nDone. Total Slack channels: {len(slack_channels)}")
