#!/bin/bash

# Test script for Institute Management APIs
# This script tests the endpoints used by the frontend

echo "========================================="
echo "Testing Institute Management APIs"
echo "========================================="
echo ""

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

BASE_URL="http://localhost:3000"
API_BASE="${BASE_URL}/api"

echo "1. Testing GET /api/institutes (List Institutes)"
echo "----------------------------------------"
RESPONSE=$(curl -s -X GET "${API_BASE}/institutes" \
  -H "Content-Type: application/json" 2>&1)
echo "Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "\[\]"; then
  echo -e "${YELLOW}⚠️  Endpoint returns empty array (placeholder)${NC}"
else
  echo -e "${GREEN}✅ Endpoint working${NC}"
fi
echo ""

echo "2. Testing POST /api/institutes (Create Institute)"
echo "----------------------------------------"
RESPONSE=$(curl -s -X POST "${API_BASE}/institutes" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Test Institute",
    "email": "test@example.com",
    "phone": "+1234567890",
    "address": "123 Test St",
    "city": "Test City",
    "state": "Test State",
    "country": "Test Country",
    "pincode": "123456"
  }' 2>&1)
echo "Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "id"; then
  echo -e "${GREEN}✅ Endpoint working${NC}"
  INSTITUTE_ID=$(echo "$RESPONSE" | grep -o '"id":"[^"]*"' | cut -d'"' -f4)
  echo "Created Institute ID: $INSTITUTE_ID"
else
  echo -e "${YELLOW}⚠️  Endpoint may be a placeholder${NC}"
fi
echo ""

echo "3. Testing GET /api/superadmin/admins (List Admins)"
echo "----------------------------------------"
RESPONSE=$(curl -s -X GET "${API_BASE}/superadmin/admins" \
  -H "Content-Type: application/json" 2>&1)
echo "Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "\[\]"; then
  echo -e "${YELLOW}⚠️  No admins found (empty array)${NC}"
elif echo "$RESPONSE" | grep -q "id"; then
  echo -e "${GREEN}✅ Endpoint working - Admins found${NC}"
else
  echo -e "${RED}❌ Endpoint may have issues${NC}"
fi
echo ""

echo "4. Testing GET /api/superadmin/users (List Platform Users)"
echo "----------------------------------------"
RESPONSE=$(curl -s -X GET "${API_BASE}/superadmin/users" \
  -H "Content-Type: application/json" 2>&1)
echo "Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "\[\]"; then
  echo -e "${YELLOW}⚠️  No users found (empty array)${NC}"
elif echo "$RESPONSE" | grep -q "id"; then
  echo -e "${GREEN}✅ Endpoint working - Users found${NC}"
else
  echo -e "${RED}❌ Endpoint may have issues${NC}"
fi
echo ""

echo "5. Testing PUT /api/institutes/:id/status (Suspend/Activate)"
echo "----------------------------------------"
if [ -n "$INSTITUTE_ID" ]; then
  RESPONSE=$(curl -s -X PUT "${API_BASE}/institutes/${INSTITUTE_ID}/status" \
    -H "Content-Type: application/json" \
    -d '{
      "is_active": false,
      "status": "Suspended"
    }' 2>&1)
  echo "Response: $RESPONSE"
  if echo "$RESPONSE" | grep -q "error\|Error\|404\|Not Found"; then
    echo -e "${RED}❌ Endpoint not found or not implemented${NC}"
  else
    echo -e "${GREEN}✅ Endpoint working${NC}"
  fi
else
  echo -e "${YELLOW}⚠️  Skipping - No institute ID available${NC}"
fi
echo ""

echo "6. Testing PUT /api/users/:id (Update User - Assign Admin)"
echo "----------------------------------------"
echo -e "${YELLOW}⚠️  This endpoint may not exist - checking...${NC}"
RESPONSE=$(curl -s -X PUT "${API_BASE}/users/test-id" \
  -H "Content-Type: application/json" \
  -d '{
    "school_id": "test-institute-id"
  }' 2>&1)
echo "Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "404\|Not Found\|Method Not Allowed"; then
  echo -e "${RED}❌ Endpoint not found - Backend needs to implement this${NC}"
  echo -e "${YELLOW}💡 Suggestion: Implement PUT /api/users/:id or PUT /api/superadmin/users/:id/assign-institute${NC}"
else
  echo -e "${GREEN}✅ Endpoint exists${NC}"
fi
echo ""

echo "========================================="
echo "Summary"
echo "========================================="
echo "- GET /api/institutes: List institutes"
echo "- POST /api/institutes: Create institute (may be placeholder)"
echo "- GET /api/superadmin/admins: List admins"
echo "- GET /api/superadmin/users: List platform users"
echo "- PUT /api/institutes/:id/status: Suspend/Activate (needs verification)"
echo "- PUT /api/users/:id: Assign admin (needs backend implementation)"
echo ""
echo "Note: Some endpoints may be placeholders or need backend implementation."
echo "The frontend will handle these gracefully with error messages."
