"""Shared MSSQL connection helper (evidence store runs on Microsoft SQL Server,
not SQLite — explicit project requirement). Credentials come from .env."""
import os

import pyodbc
from dotenv import load_dotenv

load_dotenv(os.path.join(os.path.dirname(__file__), "..", ".env"))


def connect() -> pyodbc.Connection:
    return pyodbc.connect(
        "DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s;TrustServerCertificate=yes"
        % (
            os.environ["MSSQL_DRIVER"],
            os.environ["MSSQL_SERVER"],
            os.environ["MSSQL_DATABASE"],
            os.environ["MSSQL_USER"],
            os.environ["MSSQL_PASSWORD"],
        ),
        timeout=30,
        autocommit=False,
    )


if __name__ == "__main__":
    cn = connect()
    print("MSSQL OK:", cn.execute("SELECT DB_NAME(), @@VERSION").fetchone()[0])
    cn.close()
