import sys
sys.path.append("..")
import logging
from sqlalchemy import text
from manager.MysqlManager import MysqlManager

# Logging-Konfiguration wie in deiner Vorlage für klare Statusmeldungen.
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s: %(message)s"
)

# Liste aller statischen SQL-Update-Befehle, die ausgeführt werden sollen.
# Die Bedingung "and provision1_opti = 0" stellt sicher, dass jeder Befehl
# nur einmal erfolgreich ausgeführt wird.
sql_updates = [
    # Kategorie 1: Rabatte auf den Anschlusspreis
    'update option_opti set provision1_opti = -8 where providercode_opti = "O3150922" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -33.61 where providercode_opti = "O3150921" and provision1_opti = 0;',
    
    # Kategorie 2: Aktionsguthaben
    'update option_opti set provision1_opti = -20.17 where providercode_opti = "O3120740" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -20.17 where providercode_opti = "O3120741" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -40.34 where providercode_opti = "O3120743" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -40.34 where providercode_opti = "O3120744" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -50.42 where providercode_opti = "O3132971" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -50.42 where providercode_opti = "O3132972" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -60.50 where providercode_opti = "O3120748" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -60.50 where providercode_opti = "O3120749" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -80.67 where providercode_opti = "O3120753" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -80.67 where providercode_opti = "O3120754" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -100.84 where providercode_opti = "O3120756" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -100.84 where providercode_opti = "O3120757" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -121.01 where providercode_opti = "O3132974" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -121.01 where providercode_opti = "O3132975" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -141.18 where providercode_opti = "O3132977" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -141.18 where providercode_opti = "O3132978" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -161.34 where providercode_opti = "O3132980" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -161.34 where providercode_opti = "O3132981" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -181.51 where providercode_opti = "O3132983" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -181.51 where providercode_opti = "O3132984" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -201.68 where providercode_opti = "O3120759" and provision1_opti = 0;',
    'update option_opti set provision1_opti = -201.68 where providercode_opti = "O3120760" and provision1_opti = 0;',
    
    # Kategorie 3: Tarif-Rabatte
    'UPDATE option_opti SET provision1_opti = -20 WHERE providercode_opti = "O3357017" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -40 WHERE providercode_opti = "O3357018" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -40 WHERE providercode_opti = "O3708586" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -40 WHERE providercode_opti = "O3708556" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -60 WHERE providercode_opti = "O3357019" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -80 WHERE providercode_opti = "O3357020" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -100 WHERE providercode_opti = "O3708583" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -100 WHERE providercode_opti = "O3708553" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -100 WHERE providercode_opti = "O3357021" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -120 WHERE providercode_opti = "O3357022" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -140 WHERE providercode_opti = "O3357023" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -140 WHERE providercode_opti = "O3708580" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -140 WHERE providercode_opti = "O3708550" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -160 WHERE providercode_opti = "O3357024" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -180 WHERE providercode_opti = "O3357025" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -200 WHERE providercode_opti = "O3708577" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -200 WHERE providercode_opti = "O3357026" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -200 WHERE providercode_opti = "O3708547" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -240 WHERE providercode_opti = "O3708574" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -240 WHERE providercode_opti = "O3708544" AND provision1_opti = 0;',

    # Kategorie 4: Cashback
    'UPDATE option_opti SET provision1_opti = -35 WHERE providercode_opti = "O3501314" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -45 WHERE providercode_opti = "O3501311" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -75 WHERE providercode_opti = "O3501308" AND provision1_opti = 0;',
    'UPDATE option_opti SET provision1_opti = -90 WHERE providercode_opti = "O3501305" AND provision1_opti = 0;'
]


# Die Variable “mysql” stellt die Verbindung zur Datenbank her.
mysql   = MysqlManager()

# Die Variable “session” holt die SQLAlchemy-Session für alle Datenbankoperationen.
session = mysql.getSession()

logging.info("Starte statische Provisions-Updates...")
updated_count = 0
total_count = len(sql_updates)

try:
    # Schleife durch alle SQL-Befehle und führe sie einzeln aus.
    for i, query in enumerate(sql_updates):
        # session.execute() mit text() ist der Standardweg, um Raw SQL sicher auszuführen.
        result = session.execute(text(query))
        
        # result.rowcount gibt an, wie viele Zeilen vom UPDATE betroffen waren.
        # Dies ist nützlich für das Logging, um zu sehen, ob eine Änderung stattgefunden hat.
        if result.rowcount > 0:
            updated_count += result.rowcount
            logging.info(f"Query {i+1}/{total_count}: {result.rowcount} Zeile(n) aktualisiert.")
        
    # Wenn alle Befehle ohne Fehler durchgelaufen sind, werden die Änderungen committed.
    session.commit()
    logging.info(f"Alle {total_count} Updates erfolgreich verarbeitet. Insgesamt wurden {updated_count} Zeilen geändert.")

except Exception as e:
    # Bei einem Fehler wird die gesamte Transaktion zurückgerollt, um inkonsistente Daten zu vermeiden.
    logging.error(f"Ein Fehler ist aufgetreten: {e}")
    logging.error("Die Transaktion wird zurückgerollt (Rollback).")
    session.rollback()

finally:
    # Die Session wird in jedem Fall geschlossen, um die Verbindung freizugeben.
    session.close()
    logging.info("Datenbank-Session geschlossen.")