#!/bin/bash
#
# Verify Python environment setup
# Quick check for all OrbiQuant IA services
#

set -e

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

CONDA_PATH="${HOME}/miniconda3/bin/conda"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}OrbiQuant IA - Setup Verification${NC}"
echo -e "${BLUE}================================${NC}"
echo ""

# Check Conda
echo -e "${BLUE}[1] Checking Conda installation...${NC}"
if [ -f "${CONDA_PATH}" ]; then
    echo -e "${GREEN}✓ Conda found${NC}"
    ${CONDA_PATH} --version
else
    echo -e "${RED}✗ Conda not found at ${CONDA_PATH}${NC}"
    exit 1
fi
echo ""

# Check environments
echo -e "${BLUE}[2] Checking Python environments...${NC}"

ENVS=("orbiquant-data-service" "orbiquant-ml-engine" "orbiquant-llm-agent")
ENV_COUNT=0

for env in "${ENVS[@]}"; do
    if ${CONDA_PATH} env list | grep -q "^${env} "; then
        echo -e "${GREEN}✓ ${env}${NC}"
        ((ENV_COUNT++))
    else
        echo -e "${YELLOW}⚠ ${env} - Not found${NC}"
    fi
done

echo ""
echo -e "${BLUE}Summary: ${ENV_COUNT}/3 environments ready${NC}"
echo ""

# Check environment files
echo -e "${BLUE}[3] Checking configuration files...${NC}"

FILES=(
    "apps/data-service/environment.yml"
    "apps/data-service/requirements.txt"
    "apps/data-service/.env.example"
    "apps/ml-engine/environment.yml"
    "apps/ml-engine/requirements.txt"
    "apps/llm-agent/environment.yml"
    "apps/llm-agent/requirements.txt"
    "apps/llm-agent/.env.example"
    "apps/llm-agent/pyproject.toml"
    "scripts/setup-python-envs.sh"
    "docs/95-guias-desarrollo/ml-engine/SETUP-PYTHON.md"
)

FILE_COUNT=0
for file in "${FILES[@]}"; do
    if [ -f "${PROJECT_ROOT}/${file}" ]; then
        echo -e "${GREEN}✓ ${file}${NC}"
        ((FILE_COUNT++))
    else
        echo -e "${RED}✗ ${file} - Missing${NC}"
    fi
done

echo ""
echo -e "${BLUE}Summary: ${FILE_COUNT}/${#FILES[@]} files present${NC}"
echo ""

# Final summary
echo -e "${BLUE}================================${NC}"
if [ ${ENV_COUNT} -eq 3 ] && [ ${FILE_COUNT} -eq ${#FILES[@]} ]; then
    echo -e "${GREEN}✓ Setup is complete!${NC}"
    echo ""
    echo -e "${BLUE}Next steps:${NC}"
    echo "1. Activate an environment: conda activate orbiquant-<service>"
    echo "2. Copy .env.example to .env and configure"
    echo "3. Run the service: python -m src.main"
    echo ""
    echo -e "${BLUE}Documentation:${NC}"
    echo "- Setup guide: docs/95-guias-desarrollo/ml-engine/SETUP-PYTHON.md"
else
    echo -e "${YELLOW}⚠ Setup incomplete${NC}"
    echo ""
    if [ ${ENV_COUNT} -lt 3 ]; then
        echo "Missing environments. Run: ./scripts/setup-python-envs.sh"
    fi
fi
echo -e "${BLUE}================================${NC}"
