import csv
from datetime import datetime

# Parametri identici a MQL4
FILE_PREFIX = "NN_Alerts"
REPORT_SUFFIX = "Sessione_Prova"   # AutoSessionSuffix = false → usiamo questo
DAILY_ROTATION = True

BASE_PATH = r"C:\Users\Silvia\AppData\Roaming\MetaQuotes\Terminal\08923CDC9A5DDABDCAFDC0EAC393C2FF\MQL4\Files"

def get_csv_filename():
    """
    Ricostruisce il nome del file esattamente come MQL4.
    """
    date_str = ""
    if DAILY_ROTATION:
        date_str = datetime.now().strftime("%Y%m%d")  # YYYYMMDD

    filename = FILE_PREFIX
    if date_str:
        filename += "_" + date_str
    filename += "_" + REPORT_SUFFIX + ".csv"

    return BASE_PATH + "\\" + filename


def load_data():
    """
    Legge il CSV ULTRA e restituisce una lista di dict.
    """
    csv_path = get_csv_filename()
    rows = []

    try:
        with open(csv_path, newline='', encoding='utf-8') as csvfile:
            reader = csv.reader(csvfile, delimiter='|')

            for line in reader:
                if len(line) < 8:
                    continue

                rows.append({
                    "symbol": line[2],
                    "a": float(line[3]),
                    "b": float(line[4]),
                    "price": float(line[7])
                })

    except Exception as e:
        print(f"ERRORE lettura CSV: {e}")
        print(f"Percorso tentato: {csv_path}")

    return rows
