import mysql.connector
import openai



# This code gets "do_this" from "do_this table" where "id=5" >>> then concatinates it with "do_this + to_this + gpt_response_1" from the "python_chains table" where the "target_row field = 1" >>> then concatinates that with the "do_this" fomr the "do_this table" where the "id = 6" >>> then response of intiger is storee in the "gpt_response_2" field 



# Configure OpenAI API
openai.api_key = "sk-hxKXip4XIKoZSsryz4sEVPafxoT1z4MXOMZpQhX_uDT3BlbkFJebXJbeJVxqg5mZH2DyBVwvocDwjExlDJE11Nr9kMAA"

# Database connection
def get_database_connection():
    return mysql.connector.connect(
        host='localhost',
        user='root',
        password='',
        database='gpt_o1_preview'
    )

# Function to process the next row where target_row is 1
def process_next_row():
    connection = get_database_connection()
    cursor = connection.cursor(dictionary=True)

    # Step 1: Locate the "DoThisTargetRow" in "do_this" table where id = 5
    cursor.execute("SELECT do_this FROM do_this WHERE id = 5")
    do_this_row = cursor.fetchone()
    if not do_this_row:
        print("Row with id = 5 not found in do_this table.")
        cursor.close()
        connection.close()
        return
    DoThisData = do_this_row['do_this']

    # Step 2: Locate the "PythonChainsTargetRow" in "python_chains" table where target_row = 1
    cursor.execute("SELECT * FROM python_chains WHERE target_row = 1 LIMIT 1")
    python_chains_row = cursor.fetchone()
    if not python_chains_row:
        print("No rows with target_row = 1 in python_chains table.")
        cursor.close()
        connection.close()
        return
    PythonChainsDoThis = python_chains_row['do_this']
    PythonChainsToThis = python_chains_row['to_this']
    PythonChainsGptResponse1 = python_chains_row['gpt_response_1']

    # Step 3: Concatenate the fields in the specified order
    concatenated_data = (
        f"{DoThisData} {PythonChainsDoThis} {PythonChainsToThis} {PythonChainsGptResponse1}"
    )

    # Step 4: Retrieve "do_this" field in "do_this" table where id = 6
    cursor.execute("SELECT do_this FROM do_this WHERE id = 6")
    additional_do_this_row = cursor.fetchone()
    if additional_do_this_row:
        concatenated_data += f" {additional_do_this_row['do_this']}"

    # Step 5: Send the concatenated data to GPT
    gpt_response = send_to_gpt(concatenated_data)

    # Step 6: Update the "python_chains" row with GPT's response in gpt_response_2
    cursor.execute("UPDATE python_chains SET gpt_response_2 = %s WHERE id = %s", (gpt_response, python_chains_row['id']))
    connection.commit()

    cursor.close()
    connection.close()
    print("GPT response stored in gpt_response_2 for row ID:", python_chains_row['id'])

# Helper function to send data to GPT
def send_to_gpt(input_text):
    try:
        response = openai.ChatCompletion.create(
            model="o1-preview",
            messages=[{'role': 'user', 'content': input_text}]
        )
        return response.choices[0].message['content']
    except Exception as e:
        return f"Error communicating with GPT: {str(e)}"

# Example usage
process_next_row()
