import socket,fdb,pymysql
from pymysql.cursors import DictCursor

import pymysql
from pymysql.cursors import DictCursor
import socket

def get_path_ciselniky():
    hostname = socket.gethostname().strip().upper()
    if hostname in ("NTBVBHP470G10", "Z230"):
        return r"u:\Dropbox\!!!Days\Downloads Z230\Pracuji_na\Import"
    elif hostname == "SESTRA":
        return r"z:\Dropbox\!!!Days\Downloads Z230\Pracuji_na\Import"
    else:
        print(f"Unknown host: {hostname}")
        return None

def get_reports_folder():
    hostname = socket.gethostname().strip().upper()
    if hostname in ("NTBVBHP470G10", "Z230"):
        return r"u:\Dropbox\!!!Days\Downloads Z230"
    elif hostname in ["SESTRA","POHODA","LEKAR"]:
        return r"z:\Dropbox\Ordinace\Reporty"
    else:
        print(f"Unknown host: {hostname}")
        return None

def get_mysql_connection(cursor_mode=None):
    """
    Return a PyMySQL connection.
    If cursor_mode == "DICT", return connection with DictCursor.
    Otherwise, return default tuple cursor connection.
    """
    hostname = socket.gethostname().strip()

    # decide cursor class
    cursor_cls = DictCursor if cursor_mode == "DICT" else None

    if hostname in ("NTBVBHP470G10", "Z230"):
        MYSQL_CFG = dict(
            host="192.168.1.76",
            port=3307,
            user="root",
            password="Vlado9674+",
            database="medevio",
            autocommit=True,
        )
    elif hostname == "SESTRA":
        MYSQL_CFG = dict(
            host="127.0.0.1",
            port=3307,
            user="root",
            password="Vlado9674+",
            database="medevio",
            autocommit=True,
        )
    else:
        print(f"Unknown host: {hostname}")
        return None

    # include cursorclass only if we want a dict cursor
    if cursor_cls is not None:
        MYSQL_CFG["cursorclass"] = cursor_cls

    try:
        return pymysql.connect(**MYSQL_CFG)
    except pymysql.MySQLError as e:
        print(f"MySQL connection failed: {e}")
        return None


def get_medicus_connection():
    """
    Attempt to create a Firebird connection to the Medicus database.
    Returns:
        fdb.Connection object on success
        None on failure
    """
    if socket.gethostname().strip() in ("NTBVBHP470G10","Z230"):
        MEDICUS_CFG = dict(
            dsn=r"192.168.1.4:z:\medicus 3\data\medicus.fdb",
            user="SYSDBA",
            password="masterkey",
            charset="win1250",
        )
    elif socket.gethostname().strip()=="SESTRA":
        MEDICUS_CFG = dict(
            dsn=r"192.168.1.10:m:\medicus\data\medicus.fdb",
            user="SYSDBA",
            password="masterkey",
            charset="win1250",
        )

    try:
        return fdb.connect(**MEDICUS_CFG)
    except fdb.fbcore.DatabaseError as e:
        print(f"Medicus DB connection failed: {e}")
        return None