#!/usr/bin/env python3
"""Real cluster check — run on CMS after full reload + post_full_reload_cluster.sh."""
from __future__ import print_function

import base64
import json
import os
import subprocess
import sys
from itertools import cycle

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from cluster_ip import lb_ssh_host

BASE = "/home/xtreamcodes/iptv_xtream_codes"
XOR = "5709650b0d7806074842c6de575025b1"
TEST_USER = os.environ.get("XTREAM_TEST_USER", "I8eGyJ5g9n")
TEST_PASS = os.environ.get("XTREAM_TEST_PASS", "yS3PsmjREa")
STREAMS = list(range(23, 32))


def decrypt_config():
    try:
        raw = open(BASE + "/config", "rb").read().decode("ascii")
        plain = "".join(
            chr(ord(c) ^ ord(k)) for c, k in zip(base64.b64decode(raw).decode("latin-1"), cycle(XOR))
        )
        return json.loads(plain)
    except (OSError, ValueError, json.JSONDecodeError):
        return None


def mysql_query(cfg, sql):
    env = dict(os.environ, MYSQL_PWD=cfg.get("db_pass", ""))
    proc = subprocess.run(
        [
            "mysql",
            "-h",
            cfg["host"],
            "-P",
            str(cfg.get("db_port", 7999)),
            "-u",
            cfg["db_user"],
            cfg["db_name"],
            "-N",
            "-B",
            "-e",
            sql,
        ],
        env=env,
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        text=True,
    )
    return proc.stdout if proc.returncode == 0 else ""


def ssh_cmd(host, pwd, port, cmd, timeout=30):
    proc = subprocess.run(
        [
            "sshpass",
            "-p",
            pwd,
            "ssh",
            "-o",
            "StrictHostKeyChecking=no",
            "-p",
            str(port),
            "root@%s" % host,
            cmd,
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        text=True,
        timeout=timeout,
    )
    return (proc.stdout or "").strip()


def cache_test(ip):
    proc = subprocess.run(
        [
            "sshpass",
            "-p",
            os.environ.get("CACHE_SSH_PASS", "123852"),
            "ssh",
            "-o",
            "StrictHostKeyChecking=no",
            "root@%s" % ip,
            "curl -sI -m 6 'http://127.0.0.1:8880/live/%s/%s/29.m3u8' | head -1; "
            "ss -tln | grep -c ':8880 ' || echo 0" % (TEST_USER, TEST_PASS),
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        text=True,
        timeout=20,
    )
    lines = (proc.stdout or "").strip().splitlines()
    http = lines[0] if lines else "FAIL"
    port = lines[1] if len(lines) > 1 else "?"
    return http, port


def main():
    cfg = decrypt_config()
    if not cfg:
        print("FAIL: cannot read CMS config")
        return 1

    print("=" * 60)
    print("CLUSTER ABR VERIFY (real checks)")
    print("=" * 60)

    print("\n[LB nodes]")
    lb_sql = (
        "SELECT id, server_ip, domain_name, vpn_ip, ssh_password, ssh_port "
        "FROM streaming_servers "
        "WHERE can_delete=1 AND id NOT IN (1) ORDER BY id"
    )
    lb_ok = 0
    for line in mysql_query(cfg, lb_sql).splitlines():
        parts = line.split("\t")
        if len(parts) < 5:
            continue
        sid = parts[0]
        host = lb_ssh_host(
            {"server_ip": parts[1], "domain_name": parts[2], "vpn_ip": parts[3]}
        )
        pwd = parts[4]
        port = int(parts[5] or 22) if len(parts) > 5 else 22
        cron = ssh_cmd(
            host,
            pwd,
            port,
            "grep -E 'stream_supervisor|lb_supervisor_guard' /etc/crontab 2>/dev/null || echo NONE",
        )
        marker = ssh_cmd(
            host,
            pwd,
            port,
            "test -f %s/.lb_supervisor_only && echo BAD || echo native" % BASE,
        )
        signal = ssh_cmd(
            host,
            pwd,
            port,
            "pgrep -f signal_receiver.php >/dev/null && echo ON || echo OFF",
        )
        ffmpeg = ssh_cmd(host, pwd, port, "pgrep -c ffmpeg 2>/dev/null || echo 0")
        play = ssh_cmd(host, pwd, port, "grep -c play_live.php %s/nginx/conf/nginx.conf 2>/dev/null || echo 0" % BASE)
        native = "OK" if "NONE" in cron and "native" in marker else "BAD"
        print(
            "  LB#%-3s %-16s native=%-3s signal=%-3s ffmpeg=%-3s play_live=%s"
            % (sid, host, native, signal.strip(), ffmpeg, play)
        )
        if native == "OK":
            lb_ok += 1

    print("\n[Cache]")
    cache_sql = (
        "SELECT c.id, c.server_ip, cs.ssh_password FROM cache_servers c "
        "JOIN streaming_servers cs ON cs.id=c.streaming_server_id WHERE c.enabled=1"
    )
    for line in mysql_query(cfg, cache_sql).splitlines():
        parts = line.split("\t")
        if len(parts) < 2:
            continue
        cid, ip = parts[0], parts[1]
        pwd = parts[2] if len(parts) > 2 else os.environ.get("CACHE_SSH_PASS", "123852")
        auth = ssh_cmd(
            ip,
            pwd,
            22,
            "grep 194.76.0.147 %s/nginx/conf/cache_panel_auth_upstream.conf 2>/dev/null | head -1 || echo BAD" % BASE,
        )
        http, port8880 = cache_test(ip)
        print("  CACHE#%-2s %-16s 8880=%-2s auth=%s" % (cid, ip, port8880, "OK" if "194.76.0.147" in auth else "BAD"))

    print("\n[Streams DB + cache playlists]")
    st_sql = (
        "SELECT ss.server_id, ss.stream_id, ss.stream_status, ss.pid "
        "FROM streams_sys ss WHERE ss.stream_id IN (%s) ORDER BY ss.stream_id"
        % ",".join(str(s) for s in STREAMS)
    )
    cache_ip = mysql_query(
        cfg,
        "SELECT server_ip FROM cache_servers WHERE enabled=1 LIMIT 1",
    ).strip() or "179.237.68.219"
    cache_pwd = os.environ.get("CACHE_SSH_PASS", "123852")
    for line in mysql_query(cfg, st_sql).splitlines():
        parts = line.split("\t")
        if len(parts) < 3:
            continue
        sid, st, status, pid = parts[0], parts[1], parts[2], parts[3] if len(parts) > 3 else ""
        plen = ssh_cmd(
            cache_ip,
            cache_pwd,
            22,
            "curl -s -m 6 'http://127.0.0.1:8880/live/%s/%s/%s.m3u8' | wc -c" % (TEST_USER, TEST_PASS, st),
        )
        live = "ON" if status == "1" and pid and pid != "NULL" else "OFF"
        print("  stream %-2s server=%-2s panel=%-3s playlist_bytes=%-4s" % (st, sid, live, plen or "0"))

    print("\n[MAIN auth upstream]")
    main_host = os.environ.get("MAIN_PUBLIC", "194.76.0.147")
    main_pass = os.environ.get("XTREAM_MAIN_SSH_PASS", os.environ.get("MAIN_SSH_PASS", "123852"))
    main_auth = ssh_cmd(
        main_host,
        main_pass,
        22,
        "test -f %s/wwwdir/streaming/cache_auth.php && echo OK || echo MISSING" % BASE,
    )
    print("  MAIN %s cache_auth.php=%s" % (main_host, main_auth))

    print("\n" + "=" * 60)
    print("PASS criteria after panel Start:")
    print("  - native=OK on all LBs (no supervisor cron, signal_receiver ON)")
    print("  - 8880=1 on cache")
    print("  - playlist_bytes > 100 for each stream")
    print("=" * 60)
    return 0


if __name__ == "__main__":
    sys.exit(main())
