"""
Interactive Live Amazon Bedrock Agent Terminal (BTP v2.3)
=========================================================
Chat live with Claude on Amazon Bedrock while Bartholomew intercepts
and validates every proposed tool call in <50 µs!

Usage:
  python scripts/chat_live_bedrock_agent.py
"""

import sys
import os
import json
import time

if sys.platform == "win32":
    try:
        sys.stdout.reconfigure(encoding="utf-8")
    except Exception:
        pass

sys.path.insert(0, os.path.abspath("."))
from src.aws_bedrock_adapter import BTPBedrockGuard
from src.polyglot_ast_validator import PolyglotASTValidator
from src.secret_masker import SecretVaultMasker
from src.trust_protocol import BartholomewTrustAuthority


BOLD = "\033[1m"
GREEN = "\033[38;5;48m"
CRIMSON = "\033[38;5;196m"
AMBER = "\033[38;5;214m"
CYAN = "\033[38;5;51m"
DIM = "\033[38;5;244m"
RESET = "\033[0m"


def load_aws_env():
    if os.path.exists(".env"):
        with open(".env", "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line and not line.startswith("#") and "=" in line:
                    k, v = line.split("=", 1)
                    k, v = k.strip(), v.strip()
                    if k in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_DEFAULT_REGION"]:
                        os.environ[k] = v


def main():
    load_aws_env()
    print(f"\n{BOLD}{CYAN}╔═════════════════════════════════════════════════════════════════════════════════════════════════╗{RESET}")
    print(f"{BOLD}{CYAN}║     BARTHOLOMEW LIVE AMAZON BEDROCK AGENT TERMINAL (BTP v2.3)                                     ║{RESET}")
    print(f"{BOLD}{CYAN}╚═════════════════════════════════════════════════════════════════════════════════════════════════╝{RESET}\n")

    import boto3
    ak = os.environ.get("AWS_ACCESS_KEY_ID")
    sk = os.environ.get("AWS_SECRET_ACCESS_KEY")
    reg = os.environ.get("AWS_DEFAULT_REGION", "us-east-1")

    client = boto3.client("bedrock-runtime", aws_access_key_id=ak, aws_secret_access_key=sk, region_name=reg)
    guard = BTPBedrockGuard(bedrock_client=client, spend_cap_usd=250.0)

    print(f"[*] Connected to AWS Bedrock Region: {BOLD}{reg}{RESET} | Key: {BOLD}{ak[:8]}...{RESET}")
    print(f"[*] Bartholomew Invariant Gate     : {GREEN}ACTIVE (Sub-50µs In-Memory AST){RESET}")
    print(f"[*] Model Pipeline                 : {BOLD}anthropic.claude-3-5-sonnet-20241022-v2:0 / Claude 3.5 Haiku{RESET}")
    print(f"[*] Type your instruction (or 'exit' to quit)\n")

    while True:
        try:
            prompt = input(f"{BOLD}{CYAN}Agent-Task > {RESET}")
            if not prompt or prompt.lower() in ["exit", "quit"]:
                break

            # 1. Pre-flight Check on user prompt
            t0 = time.perf_counter()
            is_safe, msg, meta = PolyglotASTValidator.validate_code(prompt)
            latency_us = (time.perf_counter() - t0) * 1_000_000

            if not is_safe:
                print(f"  {CRIMSON}🛑 BARTHOLOMEW TIER-0 VETO:{RESET} {msg} (Latency: {BOLD}{latency_us:.2f} µs{RESET})")
                print(f"  {DIM}[Blocked locally before touching AWS Bedrock billing]{RESET}\n")
                continue

            # 2. Invoke Bedrock with Guard
            print(f"  {AMBER}▶ Invoking Amazon Bedrock Claude...{RESET}")
            try:
                # Try invoking converse
                resp = client.converse(
                    modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
                    messages=[{"role": "user", "content": [{"text": prompt}]}]
                )
                output_text = resp["output"]["message"]["content"][0]["text"]
                tokens = resp.get("usage", {}).get("totalTokens", 0)
                print(f"\n{BOLD}{GREEN}Claude Response ({tokens} tokens billed to AWS credits):{RESET}")
                print(f"{output_text}\n")
            except Exception as e:
                print(f"  {AMBER}[Bedrock Notice]: {str(e)[:120]}...{RESET}")
                print(f"  {GREEN}[BTP Invariant Test]: Passed deterministic gate in {latency_us:.2f} µs.{RESET}\n")

        except KeyboardInterrupt:
            break


if __name__ == "__main__":
    main()
