import pandas as pd
import fdb
import pymysql

# ---------------------------------
# FIREBIRD CONNECTION
# ---------------------------------
fb = fdb.connect(
    host="192.168.1.4",
    database=r"z:\Medicus 3\data\MEDICUS.FDB",
    user="SYSDBA",
    password="masterkey",
    charset="WIN1250"
)
cur = fb.cursor()

sql_fb = """
SELECT kar.rodcis
FROM registr
JOIN kar ON registr.idpac = kar.idpac
WHERE registr.datum_zruseni IS NULL
  AND registr.priznak IN ('A','D','V')
"""

cur.execute(sql_fb)
rows_fb = cur.fetchall()

df_fb = pd.DataFrame(rows_fb, columns=["rc"])
print("FB count:", len(df_fb))

# ---------------------------------
# MYSQL CONNECTION
# ---------------------------------
mysql = pymysql.connect(
    host="192.168.1.76",
    port=3307,
    user="root",
    password="Vlado9674+",
    database="medevio",
    charset="utf8mb4"
)

sql_mysql = """
SELECT rc
FROM vzp_stav_pojisteni AS v
WHERE v.k_datu = CURDATE()
  AND v.id = (
        SELECT MAX(id)
        FROM vzp_stav_pojisteni
        WHERE rc = v.rc
          AND k_datu = CURDATE()
      );
"""

df_mysql = pd.read_sql(sql_mysql, mysql)
print("MySQL count:", len(df_mysql))

# ---------------------------------
# FIND MISSING RC
# ---------------------------------
df_missing = df_fb[~df_fb["rc"].isin(df_mysql["rc"])]

print("\nMissing patients:")
print(df_missing)

fb.close()
mysql.close()
