# -*- coding: utf-8 -*-
# =============================================================================
# Nazev:   store_sipiq_links_v1.0.py
# Verze:   1.0
# Datum:   2026-06-09
# Popis:   Ulozi unikatni SIPIQ odkaz (per-centrum) ke kazdemu investigatorovi
#          do Mongo feasibility.investigators -> pole sipiq.link
#          (+ sipiq.link_stored_at, sipiq.link_source). Zdroj = Trilium note
#          "SIPIQ" (noteId hAMNUnUQdCRn). Mapovani investigator->link je
#          explicitni. 14 lekaru v kroku 5 (CDA podepsano).
# Pouziti: python store_sipiq_links_v1.0.py            (dry-run / nahled)
#          python store_sipiq_links_v1.0.py --apply     (zapise do Mongo)
# =============================================================================

import os
import sys
from pymongo import MongoClient
from bson import ObjectId

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://192.168.1.76:27017")
STORED_AT = "2026-06-09"
SOURCE = "Trilium note SIPIQ (hAMNUnUQdCRn)"
BASE = "https://janssenfeasibility.iad1.qualtrics.com/jfe/form/SV_9AdeNaNyohp5fNQ?Q_CHL=gl&Q_DL=%s&_g_=g"

# investigator_id -> (label, Q_DL token)
MAPPING = [
    ("6a1c4275aa46d8b608065cec", "Lukac Ludovit",        "EMD_t2InaXAmvYtCEIT_9AdeNaNyohp5fNQ_CGC_pcjxC6AEA9hxsHl"),
    ("6a19832b5fc221351825794f", "Hlavaty Tibor",         "EMD_xmPsx66DQKz3z7M_9AdeNaNyohp5fNQ_CGC_wR6sCr6EmyO4oKq"),
    ("6a19832b5fc2213518257954", "Fedurco Miroslav",      "EMD_ZcpPzkNPT9V9pcJ_9AdeNaNyohp5fNQ_CGC_rWQC2OGTV3TaRQx"),
    ("6a198b661218c31ab0f5ba43", "Tichy Michal",          "EMD_Cwv9ZT1PSVn3hMb_9AdeNaNyohp5fNQ_CGC_BaZnCP2pwlmbMpX"),
    ("6a19832b5fc221351825796e", "Falc Matej",            "EMD_eUNAj2vPihHOm7g_9AdeNaNyohp5fNQ_CGC_aLo6pEBRHwMmLUg"),
    ("6a198b661218c31ab0f5ba41", "Pesta Martin",          "EMD_ykhyDJzyGYbwNlV_9AdeNaNyohp5fNQ_CGC_k6q3Vh4womEdc9s"),
    ("6a198b661218c31ab0f5ba45", "Jungwirthova Anna",     "EMD_f41v8N8wB7T5xUa_9AdeNaNyohp5fNQ_CGC_lW8THoTh7nYXYrn"),
    ("6a19832b5fc2213518257950", "Matous Jan",            "EMD_N5N7KrQwv4kI6uS_9AdeNaNyohp5fNQ_CGC_MQXKadWMxdLFhxi"),
    ("6a19832b5fc2213518257958", "Mihalkanin Lubomir",    "EMD_LAfWg2NWL3OpvFT_9AdeNaNyohp5fNQ_CGC_nhe5FXQKw8kOzie"),
    ("6a198b661218c31ab0f5ba4e", "Krizova Viera",         "EMD_rX6VNRycwqE5Gkw_9AdeNaNyohp5fNQ_CGC_P8peeeBOWsriB9e"),
    ("6a19832b5fc221351825795e", "Stepek David",          "EMD_tG1kIyZQVo6m5dX_9AdeNaNyohp5fNQ_CGC_Q1fqrVdGo82akWK"),
    ("6a19832b5fc2213518257972", "Konecny Stefan",        "EMD_AQa8HOUS1lQ61Be_9AdeNaNyohp5fNQ_CGC_v9oCwjfwyGIrAPt"),
    ("6a1987481218c31ab0f5ba33", "Svoboda Pavel",         "EMD_CjqBu34ODPe7o03_9AdeNaNyohp5fNQ_CGC_4FCgUB19X2tF1zK"),
    ("6a198b661218c31ab0f5ba42", "Kojecky Vladimir",      "EMD_zTJJhhRYwb9Z8pO_9AdeNaNyohp5fNQ_CGC_5Z8Ml8qISWSXWFT"),
]


def main():
    apply = "--apply" in sys.argv
    client = MongoClient(MONGO_URI)
    col = client["feasibility"]["investigators"]

    print("=== NAHLED: SIPIQ link -> Mongo (sipiq.link) ===\n")
    n = 0
    for inv_id, label, token in MAPPING:
        link = BASE % token
        doc = col.find_one({"_id": ObjectId(inv_id)}, {"prijmeni": 1, "jmeno": 1, "sipiq.link": 1})
        if not doc:
            print(f"!!! NENALEZEN {label} ({inv_id})")
            continue
        existing = (doc.get("sipiq") or {}).get("link")
        print(f"{label:22s}  token={token[:24]}...  existuje={'ANO' if existing else 'ne'}")
        if apply:
            res = col.update_one(
                {"_id": ObjectId(inv_id)},
                {"$set": {
                    "sipiq.link": link,
                    "sipiq.link_token": token,
                    "sipiq.link_stored_at": STORED_AT,
                    "sipiq.link_source": SOURCE,
                }},
            )
            n += res.modified_count

    print()
    if apply:
        print(f">>> ZAPSANO: {n} zaznamu")
    else:
        print(">>> DRY-RUN. Pro zapis spust s --apply")


if __name__ == "__main__":
    main()
