import os
from dotenv import load_dotenv
import logging

from httpx import get

# Load environment variables from .env file
load_dotenv()


APPNAME = "Pedidos Express"
MAIL = os.getenv("MAIL_DIRECTION","")
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD","")
LOGS_FOLDER = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs')
IS_OPEN_STORE = True
if not os.path.exists(LOGS_FOLDER):
    os.makedirs(LOGS_FOLDER)
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
# Configure logging
logging.basicConfig(
    level=getattr(logging, LOG_LEVEL),
    format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(os.path.join(LOGS_FOLDER, 'app.log')),
        logging.StreamHandler()
    ]
)

# Configuration
APP_NAME = os.getenv("APP_NAME", "Pedidos Express")
FEEDBACK_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs', 'feedback.json')
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
IMAGE_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'public', 'images')
PORT = int(os.getenv("PORT", 6001))
CURRENT_URL = os.getenv("CURRENT_URL", "http://localhost:6001")
PIN_KEY = os.getenv("PIN_KEY", "-1")
DEVELOPMENT = True if os.getenv("NODE_ENV", "development").lower() == "development" else False
if PIN_KEY == "-1":
    logging.warning("Using default PIN_KEY. Please set a strong PIN_KEY in your .env file for production.")
# SECRET_KEY is crucial for signing session cookies.
# Fallback to a default if not set, but warn that this is insecure for production.
SECRET_KEY = os.getenv("SECRET_KEY", "your_very_very_secret_key_for_signing_cookies_python_v2")

# Data paths
BG_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data', 'llm_data.json')
PRODUCT_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data', 'products.json')


#postgresql connection settings
POSTGRESQL_DB_CONFIG = {
    'dbname': os.getenv('POSTGRES_DB', 'pedidos_express_pruebas'),
    'user': os.getenv('POSTGRES_USER', 'superti'),
    'password': os.getenv('POSTGRES_PASSWORD', 'BTD2DALN55N'),
    'host': os.getenv('POSTGRES_HOST', 'localhost'),
    'port': os.getenv('POSTGRES_PORT', '5432')
}

def validate_config():
    logger = logging.getLogger(__name__)
    """Validate configuration and show warnings"""
    if SECRET_KEY == "your_very_very_secret_key_for_signing_cookies_python_v2":
        logger.warning("Using default SECRET_KEY. Please set a strong SECRET_KEY in your .env file for production.")
    
    if POSTGRESQL_DB_CONFIG['dbname'] == 'pedidos_express_pruebas':
        logger.warning("Using default database name 'pedidos_express_pruebas'. Please set a valid database name in your .env file for production.")

    if not POSTGRESQL_DB_CONFIG['user'] or not POSTGRESQL_DB_CONFIG['password']:
        logger.critical("CRITICAL ERROR: POSTGRES_USER and POSTGRES_PASSWORD environment variables not set. The application will not work correctly.")
        return False

    if not MAIL or not MAIL_PASSWORD:
        logger.critical("CRITICAL ERROR: MAIL and MAIL_PASSWORD environment variables not set. The application will not work correctly.")
        return False

    if not OPENAI_API_KEY:
        logger.critical("CRITICAL ERROR: OPENAI_API_KEY environment variable not set. The application will not work correctly.")
        return False
    return True
