import os
import pandas as pd
import json
from datetime import datetime

def fetch_files_and_data():
    # Define the directory where the files are stored
    directory = '/var/www/html/optionChain/NIFTY/'

    # List all .txt files in the directory
    files = [f for f in os.listdir(directory) if f.endswith('.txt')]

    all_graphs = []
    for file in files:
        file_path = os.path.join(directory, file)

        # Read the file content
        df = pd.read_csv(file_path, sep="\t", names=["time", "value"])

        # Specify the format for 'time' column to ensure consistent parsing
        df['time'] = pd.to_datetime(df['time'], format='%d-%m-%Y %H:%M:%S', errors='coerce')

        # Drop rows where 'time' or 'value' is NaN
        df.dropna(subset=['time', 'value'], inplace=True)

        # Format the 'time' column to the desired format (DD-MM-YYYY HH:mm:ss)
        df['time'] = df['time'].dt.strftime('%d-%m-%Y %H:%M:%S')

        # Prepare the data for JSON output
        graph_data = {
            "name": file.replace('.txt', ''),  # Graph name without the '.txt' extension
            "data": df.to_dict(orient="records")  # Convert the DataFrame to a list of dictionaries
        }

        all_graphs.append(graph_data)

    # Return the final JSON object containing all graphs
    return {"graphs": all_graphs}

if __name__ == "__main__":
    try:
        data = fetch_files_and_data()
        # Ensure that data is not empty before printing
        if data['graphs']:
            print(json.dumps(data, indent=4))  # Pretty-print the JSON
        else:
            print(json.dumps({"error": "No data available"}))  # Return error if no graphs are available
    except Exception as e:
        print(json.dumps({"error": str(e)}))  # Return error as JSON
