import json, os, urllib.request, urllib.error
from pathlib import Path

PAT = Path('/tmp/at_pat.txt').read_text().strip()
BASE = 'appE43UvTyARe5oJs'
TABLE = 'Actions'  # may need to discover
HEADERS = {
    'Authorization': f'Bearer {PAT}',
    'Content-Type': 'application/json',
}


def req(url, method='GET', payload=None):
    data = json.dumps(payload).encode() if payload else None
    r = urllib.request.Request(url, headers=HEADERS, method=method, data=data)
    try:
        with urllib.request.urlopen(r, timeout=20) as resp:
            return resp.status, json.loads(resp.read().decode())
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode()[:400]


# Try listing tables first to find the Actions table
status, data = req(f'https://api.airtable.com/v0/{BASE}/tables')
print('tables list status:', status)
if status == 200:
    for t in data.get('tables', []):
        if 'action' in t.get('name', '').lower() or 'task' in t.get('name', '').lower() or 'follow' in t.get('name', '').lower():
            print(f"  candidate: {t['id']} - {t['name']}")
    print('all tables:')
    for t in data.get('tables', []):
        print(f"  {t['id']} - {t['name']}")
