#!/usr/bin/env bash
# ==============================================================================
# IKGPTU Universal Campus ERP — Automated Health & Full API Diagnostics Suite
# ==============================================================================

set -e

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

BACKEND_URL="http://127.0.0.1:8000"
FRONTEND_URL="http://localhost:3000"

echo ""
echo -e "${BOLD}${BLUE}========================================================================${NC}"
echo -e "${BOLD}${BLUE}   IKGPTU Universal Campus ERP — System & API Health Checkup Suite      ${NC}"
echo -e "${BOLD}${BLUE}========================================================================${NC}"
echo ""

TOTAL_TESTS=0
PASSED_TESTS=0

run_check() {
    local name="$1"
    local url="$2"
    local expected_code="${3:-200}"
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    start_time=$(date +%s%N)
    http_code=$(curl -s -o /tmp/api_check_resp.json -w "%{http_code}" "$url" || echo "000")
    end_time=$(date +%s%N)
    duration_ms=$(( (end_time - start_time) / 1000000 ))

    if [ "$http_code" -eq "$expected_code" ]; then
        PASSED_TESTS=$((PASSED_TESTS + 1))
        echo -e "  [${GREEN}✓ PASSED${NC}] [HTTP $http_code] (${duration_ms}ms) $name"
    else
        echo -e "  [${RED}✗ FAILED${NC}] [HTTP $http_code != $expected_code] (${duration_ms}ms) $name"
        echo -e "    Response: $(head -c 120 /tmp/api_check_resp.json)"
    fi
}

echo -e "${BOLD}1. Backend & Server Health Checks${NC}"
run_check "Backend Root Service Info (/)" "$BACKEND_URL/" 200
run_check "Backend Framework Up Signal (/up)" "$BACKEND_URL/up" 200
run_check "Backend API Health Check (/api/health)" "$BACKEND_URL/api/health" 200
run_check "Backend API v1 Health Check (/api/v1/health)" "$BACKEND_URL/api/v1/health" 200
run_check "Backend Full Diagnostic Engine (/api/full-check)" "$BACKEND_URL/api/full-check" 200

echo ""
echo -e "${BOLD}2. Super Admin & Governance API Endpoints${NC}"
run_check "Super Admin KPI Stats API (/api/v1/super-admin/stats)" "$BACKEND_URL/api/v1/super-admin/stats" 200
run_check "Academic Students Table (/api/v1/super-admin/tables/academic_students)" "$BACKEND_URL/api/v1/super-admin/tables/academic_students" 200
run_check "Students Table Alias (/api/v1/super-admin/tables/students)" "$BACKEND_URL/api/v1/super-admin/tables/students" 200
run_check "Faculty & HRM Employees Table (/api/v1/super-admin/tables/hrm_employees)" "$BACKEND_URL/api/v1/super-admin/tables/hrm_employees" 200
run_check "Employees Table Alias (/api/v1/super-admin/tables/employees)" "$BACKEND_URL/api/v1/super-admin/tables/employees" 200
run_check "HRM Leave Ledger Table (/api/v1/super-admin/tables/hrm_leave_records)" "$BACKEND_URL/api/v1/super-admin/tables/hrm_leave_records" 200
run_check "Leaves Table Alias (/api/v1/super-admin/tables/leaves)" "$BACKEND_URL/api/v1/super-admin/tables/leaves" 200
run_check "Master Users Table (/api/v1/super-admin/tables/master_users)" "$BACKEND_URL/api/v1/super-admin/tables/master_users" 200
run_check "Users Table Alias (/api/v1/super-admin/tables/users)" "$BACKEND_URL/api/v1/super-admin/tables/users" 200
run_check "Academic Courses Table (/api/v1/super-admin/tables/academic_courses)" "$BACKEND_URL/api/v1/super-admin/tables/academic_courses" 200
run_check "Courses Table Alias (/api/v1/super-admin/tables/courses)" "$BACKEND_URL/api/v1/super-admin/tables/courses" 200
run_check "Institutional Master Roles Table (/api/v1/super-admin/tables/master_roles)" "$BACKEND_URL/api/v1/super-admin/tables/master_roles" 200
run_check "Master Dropdowns Table (/api/v1/super-admin/tables/master_dropdowns)" "$BACKEND_URL/api/v1/super-admin/tables/master_dropdowns" 200
run_check "Master Sessions Table (/api/v1/super-admin/tables/master_sessions)" "$BACKEND_URL/api/v1/super-admin/tables/master_sessions" 200

echo ""
echo -e "${BOLD}3. Academics & Identity Engines${NC}"
run_check "ID Card Generation Engine (/api/v1/academics/id-cards/2516001)" "$BACKEND_URL/api/v1/academics/id-cards/2516001" 200

echo ""
echo -e "${BOLD}4. Frontend & Reverse Proxy Integration${NC}"
run_check "Frontend Dev Server Host (:3000)" "$FRONTEND_URL" 200
run_check "Frontend Proxied API Health (/api/health)" "$FRONTEND_URL/api/health" 200
run_check "Frontend Proxied Super Admin Stats (/api/v1/super-admin/stats)" "$FRONTEND_URL/api/v1/super-admin/stats" 200
run_check "Frontend Proxied Full Diagnostic Engine (/api/full-check)" "$FRONTEND_URL/api/full-check" 200

echo ""
echo -e "${BOLD}${BLUE}========================================================================${NC}"
if [ "$PASSED_TESTS" -eq "$TOTAL_TESTS" ]; then
    echo -e "${BOLD}${GREEN}   ALL $TOTAL_TESTS CHECKS PASSED (100% HEALTHY & OPERATIONAL)           ${NC}"
else
    echo -e "${BOLD}${YELLOW}   COMPLETED: $PASSED_TESTS / $TOTAL_TESTS CHECKS PASSED                  ${NC}"
fi
echo -e "${BOLD}${BLUE}========================================================================${NC}"
echo ""
