#!/bin/bash

# Set environment variables
export OPENAI_API_KEY="sk-proj-kGQPjKXup6g5QmjWvN3GT3BlbkFJDOYyhv8auoHBDIznmbgj"
export SUPABASE_URI=postgresql://postgres:chuz8310xsystex@db.ptt.cx:5432/postgres
export SUPABASE_URL="http://db.ptt.cx:8000/"
export SUPABASE_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q"


Function to check if Docker is running
start_docker() {
    if ! docker info > /dev/null 2>&1; then
        echo "Starting Docker Desktop..."
        open --background -a Docker
        # Wait until Docker daemon is running
        while ! docker info > /dev/null 2>&1; do
            echo "Waiting for Docker to start..."
            sleep 1
        done
        echo "Docker started."
    else
        echo "Docker is already running."
    fi
}

#########################################
# # Start Docker if it's not already running
# if ! systemctl is-active --quiet docker; then
#     echo "Starting Docker..."
#     sudo systemctl start docker
#     sleep 5  # Wait for Docker to fully start
# fi

# # Get the script directory
# script_dir=$(dirname "$0")

# # Change to the directory containing the docker-compose.yml file
# cd "$script_dir"

# # Start the services defined in docker-compose.yml
# echo "Starting services with Docker Compose..."
# docker-compose up -d

# # Wait for services to be fully up
# echo "Waiting for services to start..."
# sleep 20  # Adjust this time as needed

# # Change to the directory containing your Python script
# cd "$script_dir/systex-RAG-sherry"

# Run your Python script
# echo "Running RAG application..."
# python RAG_app_copy.py

##########################################

# Check and manage Redis Stack container
if [ "$(docker ps -a -q -f name=redis-stack)" ]; then
    if [ ! "$(docker ps -q -f name=redis-stack)" ]; then
        echo "Starting existing Redis Stack container..."
        docker start redis-stack
    else
        echo "Redis Stack container is already running."
    fi
else
    echo "Creating and starting Redis Stack container..."
    docker run -d --name redis-stack -p 6380:6379 redis/redis-stack:latest
fi

# Check if Redis container started successfully
if [ ! "$(docker ps -q -f name=redis-stack)" ]; then
    echo "Redis Stack container failed to start. Exiting script."
    exit 1
fi

echo "Running RAG application..."
python RAG_app_copy.py

# 使脚本文件可执行：
# chmod +x run.sh

# /answer2?question=解釋碳排放法規

