from dotenv import load_dotenv
import os
import subprocess
import re
import openai
from time import sleep
from urllib import request
import math
from github_utils import get_repo
from openai_utils import generate_html
from stablehorde import generate_image
from beautifulsoup import generate_images


# Load environment variables from .env file
load_dotenv()

# Load environment variables from .env file
load_dotenv()

# Set GitHub access token and repository name
GITHUB_ACCESS_TOKEN = os.environ["GITHUB_ACCESS_TOKEN"]
REPO_NAME = os.environ["GITHUB_REPO_NAME"]

# Set OpenAI API key
openai.api_key = os.environ["OPENAI_API_KEY"]

# Set Stablehorde API key
STABLEHORDE_API_KEY = os.environ["STABLEHORDE_API_KEY"]

# Set prompt for OpenAI API
prompt_template = "I want you to create HTML code based on the input text: {}. Reply only with HTML code no other comments. Please include CSS for the page. For the images, the <img> tags must include at least two attributes: a style attribute with at least the heigh and width in pixel and a custom alt text attribute different for each image and with at least 100 words to describe the image."


def make_changes_and_push(repo, local_directory, commit_message, input_text):
    # Generate HTML content using OpenAI API
    prompt = prompt_template.format(input_text)
    html_content = generate_html(prompt)

    # Generate images using Stablehorde API
    html_content = generate_images(html_content, STABLEHORDE_API_KEY, local_directory)

    # Clone or pull the repository
    if os.path