from pathlib import Path
import json, os, socket,fdb

def get_dropbox_path() -> Path:
    r"""
    Return the absolute Dropbox folder path.
    Works across your computers (U:, Z:, etc.).
    Always reads from LOCALAPPDATA\Dropbox\info.json.
    Falls back to searching for a folder literally named 'Dropbox'.
    """
    info_path = Path(os.environ["LOCALAPPDATA"]) / "Dropbox" / "info.json"
    if info_path.exists():
        try:
            with open(info_path, encoding="utf-8") as f:
                info = json.load(f)
                return Path(info["personal"]["path"])
        except Exception as e:
            print(f"⚠️ Could not read Dropbox info.json: {e}")

    # fallback if JSON missing
    for drive in "CDEFGHIJKLMNOPQRSTUVWXYZ":
        candidate = Path(f"{drive}:\\Dropbox")
        if candidate.exists():
            return candidate

    raise FileNotFoundError("Dropbox folder not found on any drive.")




def get_medicus_connection():
    """
    Connect to Firebird 'medicus.fdb' depending on computer name.
    Returns fdb.Connection or raises RuntimeError if unknown or connection fails.
    """
    computer_name = socket.gethostname().upper()
    try:
        if computer_name == "Z230":
            print("Computer name is Z230")
            return fdb.connect(dsn=r"localhost:c:\medicus 3\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250")
        elif computer_name == "LEKAR":
            print("Computer name is SESTRALEKAR")
            return fdb.connect(dsn=r"localhost:m:\medicus\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250")
        elif computer_name in ("SESTRA", "POHODA"):
            print("Computer name is SESTRA or POHODA")
            return fdb.connect(dsn=r"192.168.1.10:m:\medicus\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250")
        else:
            raise RuntimeError(f"❌ Unknown computer name: {computer_name}")

    except Exception as e:
        print(f"⚠️ Error connecting to Medicus on {computer_name}: {e}")
        raise


