import json, sys
sys.path.insert(0, "scratchpad/exf")
from build import api, BASE, CARDS, TAGS, dq

COLLECTION = 583  # "Claude" (junto a 1094)

CUR = {"number_style":"currency","currency":"EUR","currency_style":"symbol","decimals":0}

def viz(kind):
    if kind == "total":
        return {"column_settings":{'["name","venta_web_eur"]':CUR},
                "scalar.field":"venta_web_eur"}
    if kind == "canal":
        return {"graph.dimensions":["canal"],"graph.metrics":["venta_eur"],
                "graph.x_axis.title_text":"Canal","graph.y_axis.title_text":"Venta web (EUR)",
                "column_settings":{'["name","venta_eur"]':CUR},"graph.show_values":True}
    if kind == "pago":
        return {"graph.dimensions":["forma_pago"],"graph.metrics":["venta_eur"],
                "column_settings":{'["name","venta_eur"]':CUR},"graph.show_values":True}
    if kind == "matriz":
        return {"column_settings":{'["name","venta_eur"]':CUR},
                "table.columns":[
                  {"name":"canal","enabled":True},{"name":"forma_pago","enabled":True},
                  {"name":"venta_eur","enabled":True},{"name":"documentos","enabled":True}]}
    if kind == "evolutivo":
        return {"graph.dimensions":["mes","canal"],"graph.metrics":["venta_eur"],
                "stackable.stack_type":"stacked","column_settings":{'["name","venta_eur"]':CUR},
                "graph.x_axis.title_text":"Mes","graph.y_axis.title_text":"Venta web (EUR)"}
    return {}

created = {}
for k, c in CARDS.items():
    body = {"name": c["name"], "display": c["display"],
            "dataset_query": dq(c["sql"]),
            "visualization_settings": viz(k),
            "collection_id": COLLECTION}
    r = api("POST", "/api/card", body)
    created[k] = r["id"]
    print(f"card {k}: id {r['id']}  {c['name']}")

json.dump(created, open("scratchpad/exf/cards.json","w"))
print("CARDS:", created)
