#!/bin/bash

# ColaFlow API Connection Test Script
# This script helps diagnose API connection issues

echo "============================================"
echo "ColaFlow API Connection Diagnostic Test"
echo "============================================"
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Test 1: Check if backend is running on port 5167
echo "Test 1: Checking if backend is running on port 5167..."
if curl -s -o /dev/null -w "%{http_code}" http://localhost:5167 > /dev/null 2>&1; then
    echo -e "${GREEN}✓ Backend is responding on port 5167${NC}"
else
    echo -e "${RED}✗ Backend is NOT responding on port 5167${NC}"
    echo "  → Solution: Start the backend server"
    echo "     cd ColaFlow.Api && dotnet run"
fi
echo ""

# Test 2: Check API health endpoint
echo "Test 2: Checking API health endpoint..."
HEALTH_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5167/api/v1/health 2>&1)
if [ "$HEALTH_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ API health endpoint is working${NC}"
elif [ "$HEALTH_STATUS" = "404" ]; then
    echo -e "${YELLOW}⚠ API health endpoint not found (404)${NC}"
    echo "  → This is OK if health endpoint is not implemented"
elif [ "$HEALTH_STATUS" = "000" ]; then
    echo -e "${RED}✗ Cannot connect to API (Connection refused)${NC}"
    echo "  → Solution: Start the backend server"
else
    echo -e "${YELLOW}⚠ Unexpected status code: $HEALTH_STATUS${NC}"
fi
echo ""

# Test 3: Check projects endpoint
echo "Test 3: Checking projects endpoint..."
PROJECTS_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5167/api/v1/projects 2>&1)
if [ "$PROJECTS_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ Projects endpoint is working${NC}"
    PROJECTS_DATA=$(curl -s http://localhost:5167/api/v1/projects)
    echo "  Response: $PROJECTS_DATA"
elif [ "$PROJECTS_STATUS" = "401" ]; then
    echo -e "${YELLOW}⚠ Projects endpoint requires authentication (401)${NC}"
    echo "  → This is OK, authentication is working"
elif [ "$PROJECTS_STATUS" = "404" ]; then
    echo -e "${RED}✗ Projects endpoint not found (404)${NC}"
    echo "  → Check backend routing configuration"
elif [ "$PROJECTS_STATUS" = "000" ]; then
    echo -e "${RED}✗ Cannot connect to API (Connection refused)${NC}"
    echo "  → Solution: Start the backend server"
else
    echo -e "${YELLOW}⚠ Unexpected status code: $PROJECTS_STATUS${NC}"
fi
echo ""

# Test 4: Check if frontend is running
echo "Test 4: Checking if frontend is running on port 3000..."
FRONTEND_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 2>&1)
if [ "$FRONTEND_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ Frontend is running on port 3000${NC}"
elif [ "$FRONTEND_STATUS" = "000" ]; then
    echo -e "${RED}✗ Frontend is NOT running on port 3000${NC}"
    echo "  → Solution: Start the frontend server"
    echo "     cd colaflow-web && npm run dev"
else
    echo -e "${YELLOW}⚠ Unexpected status code: $FRONTEND_STATUS${NC}"
fi
echo ""

# Test 5: Check .env.local configuration
echo "Test 5: Checking .env.local configuration..."
if [ -f "colaflow-web/.env.local" ]; then
    echo -e "${GREEN}✓ .env.local file exists${NC}"

    # Check if API URL is configured
    if grep -q "NEXT_PUBLIC_API_URL" colaflow-web/.env.local; then
        API_URL=$(grep "NEXT_PUBLIC_API_URL" colaflow-web/.env.local | cut -d '=' -f 2)
        echo "  Configured API URL: $API_URL"

        if [ "$API_URL" = "http://localhost:5167/api/v1" ]; then
            echo -e "${GREEN}✓ API URL is correctly configured${NC}"
        else
            echo -e "${YELLOW}⚠ API URL might be incorrect${NC}"
            echo "  Expected: http://localhost:5167/api/v1"
            echo "  Found: $API_URL"
        fi
    else
        echo -e "${RED}✗ NEXT_PUBLIC_API_URL not found in .env.local${NC}"
        echo "  → Solution: Add NEXT_PUBLIC_API_URL=http://localhost:5167/api/v1"
    fi
else
    echo -e "${RED}✗ .env.local file not found${NC}"
    echo "  → Solution: Create .env.local with NEXT_PUBLIC_API_URL=http://localhost:5167/api/v1"
fi
echo ""

# Summary
echo "============================================"
echo "Summary"
echo "============================================"
echo ""
echo "Next steps:"
echo "1. If backend is not running: cd ColaFlow.Api && dotnet run"
echo "2. If frontend is not running: cd colaflow-web && npm run dev"
echo "3. If .env.local is missing: Create it with NEXT_PUBLIC_API_URL=http://localhost:5167/api/v1"
echo "4. After starting services, open http://localhost:3000/projects"
echo "5. Press F12 and check Console and Network tabs for detailed errors"
echo ""
echo "For more details, see DEBUGGING_GUIDE.md"
echo ""
