# src/remoteRF/core/remoterf_config.py

from __future__ import annotations

import argparse
import os
import sys
from pathlib import Path
from typing import Tuple

from .cert_fetcher import fetch_and_save_ca_cert  # your earlier module

# -----------------------------
# Local config locations
# -----------------------------
def _config_root() -> Path:
    # Keep it simple and consistent with earlier suggestions.
    return Path(os.path.expanduser("~")) / ".config" / "remoterf"

def _env_path() -> Path:
    return _config_root() / ".env"

def _certs_dir() -> Path:
    return _config_root() / "certs"

def _parse_hostport(s: str) -> Tuple[str, int]:
    s = s.strip()
    if "://" in s:
        s = s.split("://", 1)[1]

    if ":" not in s:
        raise ValueError("Expected format host:port")

    host, port_str = s.rsplit(":", 1)
    host = host.strip()
    port = int(port_str.strip())
    if not host:
        raise ValueError("Host is empty")
    if port <= 0 or port > 65535:
        raise ValueError("Port out of range")
    return host, port


def _write_env_kv(path: Path, kv: dict[str, str]) -> None:
    """
    Writes/overwrites a simple dotenv-style file.
    (We overwrite the whole file for simplicity and correctness.)
    """
    path.parent.mkdir(parents=True, exist_ok=True)
    lines = []
    for k, v in kv.items():
        # Quote only if needed; keep simple and safe.
        if any(c.isspace() for c in v) or any(c in v for c in ['"', "'"]):
            v = v.replace('"', '\\"')
            lines.append(f'{k}="{v}"')
        else:
            lines.append(f"{k}={v}")
    path.write_text("\n".join(lines) + "\n", encoding="utf-8")


def main() -> None:
    parser = argparse.ArgumentParser(
        prog="remoterf-config",
        description="Configure RemoteRF client to talk to a specific server (fetch CA cert + save target)."
    )
    parser.add_argument(
        "addr",
        help="Server gRPC address in host:port form (this is the TLS gRPC port)."
    )
    parser.add_argument(
        "--cert-port",
        type=int,
        default=None,
        help="Bootstrap cert-provider port. If omitted, defaults to <grpc_port + 1>."
    )
    parser.add_argument(
        "--profile",
        default="default",
        help="Profile name (controls CA cert filename). Default: default"
    )
    parser.add_argument(
        "--timeout",
        type=float,
        default=3.0,
        help="Network timeout seconds. Default: 3.0"
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Overwrite existing cert/config if present."
    )
    args = parser.parse_args()

    try:
        host, grpc_port = _parse_hostport(args.addr)
    except Exception as e:
        print(f"Error: invalid addr '{args.addr}': {e}", file=sys.stderr)
        sys.exit(2)

    cert_port = args.cert_port if args.cert_port is not None else (grpc_port + 1)

    # Destination CA cert file
    certs_dir = _certs_dir()
    certs_dir.mkdir(parents=True, exist_ok=True)
    ca_out = certs_dir / f"{args.profile}.crt"

    if ca_out.exists() and not (args.overwrite):
        # Treat as OK; still (re)write env target.
        fetched_ok = True
    else:
        fetched_ok = fetch_and_save_ca_cert(
            host,
            cert_port,
            out_path=ca_out,
            timeout_sec=args.timeout,
            overwrite=True,
        )

    if not fetched_ok:
        print(
            f"Failed to fetch CA cert from {host}:{cert_port}.\n"
            f"Expected cert provider at: http://{host}:{cert_port}/ca.crt (or raw TCP).\n"
            f"Server must be running cert_provider on that port.",
            file=sys.stderr
        )
        sys.exit(1)

    # Write local .env (or config) with chosen server and CA path
    env_file = _env_path()
    _write_env_kv(env_file, {
        "REMOTERF_ADDR": f"{host}:{grpc_port}",
        "REMOTERF_CA_CERT": str(ca_out),
        "REMOTERF_PROFILE": args.profile,
    })

    print("RemoteRF configured successfully.")
    print(f"  gRPC target : {host}:{grpc_port}")
    print(f"  cert port   : {host}:{cert_port}")
    print(f"  CA cert     : {ca_out}")
    print(f"  env file    : {env_file}")

if __name__ == "__main__":
    main()
