#!/usr/bin/env python3
"""Sign this home in to brnrd.dev: an approval link, then the gate written for the relay.

The handshake is ``brnrd account connect``'s own (``cloud.start_pair`` /
``cloud.await_pair``), bound to the home's self repo. Its state lands in
``<home>/loom/gates/``, and ``relay_state`` is set to ``<home>/loom`` so the
relay's in/out read it. A home already connected (``gates.state_dir``: a
``relay_state``, or a gate migrated into ``loom/gates``) is left alone.
"""
import os
import socket
import sys
from pathlib import Path

from brr.gates import cloud
from brr.loom.runtime import gates as home_gates
from brr.loom.runtime.config import load_config, seed_config, set_value

home = Path(os.environ['BRNRD_CONFIG']).parent.parent
if home_gates.state_dir(home, load_config(home)) is not None:
    sys.exit(0)
url = os.environ.get('BRNRD_URL', 'https://brnrd.dev').rstrip('/')
self_brr = home / 'self' / '.brr'
caps = cloud._repo_capabilities(self_brr)
body = {k: caps[k] for k in ('repo_full_name', 'git_remote', 'branch', 'default_branch', 'forge')
        if isinstance(caps.get(k), str) and caps[k]}
body.update(cloud.requester())
# The page asks to approve this computer; the self repo stays plumbing.
body['kind'] = 'loom'
try:
    pair = cloud.start_pair(url, body)
except Exception as exc:
    sys.exit(f'Could not reach {url}: {exc}')
# A web page opens in this computer's browser: a QR code is only for a link into an app.
print(f"  Open {pair['pair_url']} and sign in to approve this computer.", flush=True)
print(f"  Only approve a code you started yourself: the page shows {pair['pair_code']}"
      f" for {body.get('os_user', '?')} on {body.get('hostname', '?')}.", flush=True)
try:
    status = cloud.await_pair(url, pair, timeout_s=float(os.environ.get('BRNRD_PAIR_TIMEOUT', 600)))
except TimeoutError:
    sys.exit('The approval link was not approved in time; nothing was saved. Run brnrd setup again.')
state = dict(brnrd_url=url, token=status['daemon_token'], account_id=status.get('account_id'),
             repo_id=status.get('repo_id'), daemon_name=socket.gethostname() or 'loom',
             capabilities=caps, since=0)
gates = home / 'loom'
cloud._save_state_to_dir(gates, state)
# The publish endpoints bind the token to the Daemon row /register creates.
cloud._register(self_brr, state)
seed_config(home)
set_value(home, 'relay_state', str(gates))
print(f"  Signed in to brnrd.dev ✓ (account {status.get('account_id')})", flush=True)
