#!/bin/bash

# LQC API Integration Test Script
# Tests all CRM endpoints to verify implementation

API_URL="http://localhost:8000/api"
JWT_TOKEN="${1:-your_jwt_token_here}"

echo "================================"
echo "LQC API Integration Test Suite"
echo "================================"
echo ""
echo "API Base URL: $API_URL"
echo ""

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

# Test counter
PASSED=0
FAILED=0

# Function to test endpoint
test_endpoint() {
  local method=$1
  local endpoint=$2
  local data=$3
  local description=$4

  echo -e "${YELLOW}Testing: $description${NC}"
  
  if [ "$method" = "GET" ]; then
    response=$(curl -s -X GET "$API_URL$endpoint" \
      -H "Authorization: Bearer $JWT_TOKEN" \
      -H "Accept: application/json")
  elif [ "$method" = "POST" ]; then
    response=$(curl -s -X POST "$API_URL$endpoint" \
      -H "Authorization: Bearer $JWT_TOKEN" \
      -H "Content-Type: application/json" \
      -H "Accept: application/json" \
      -d "$data")
  fi

  # Check if response contains "success"
  if echo "$response" | grep -q '"success"'; then
    echo -e "${GREEN}✓ PASS${NC}"
    ((PASSED++))
  else
    echo -e "${RED}✗ FAIL${NC}"
    echo "Response: $response"
    ((FAILED++))
  fi
  echo ""
}

# ============================================
# LEAD TESTS
# ============================================
echo "========== LEAD TESTS =========="
echo ""

test_endpoint "GET" "/crm/leads" "" "Get all leads"

test_endpoint "POST" "/crm/leads" '{
  "lead_name": "Test Company",
  "lead_email": "test@company.com",
  "company_name": "Test Company Inc",
  "source_type": "website",
  "estimated_monthly_budget": 500000,
  "industry": "Technology"
}' "Create new lead"

test_endpoint "GET" "/crm/leads?status=lead_created" "" "Get leads filtered by status"

test_endpoint "GET" "/crm/leads?source=website" "" "Get leads filtered by source"

# ============================================
# QUOTE TESTS
# ============================================
echo "========== QUOTE TESTS =========="
echo ""

test_endpoint "GET" "/crm/quotes" "" "Get all quotes"

test_endpoint "POST" "/crm/quotes" '{
  "lead_id": 1,
  "base_rate": 50000,
  "total_amount": 500000,
  "currency": "INR",
  "validity_days": 30
}' "Create new quote"

# ============================================
# CONTRACT TESTS
# ============================================
echo "========== CONTRACT TESTS =========="
echo ""

test_endpoint "GET" "/crm/contracts" "" "Get all contracts"

test_endpoint "POST" "/crm/contracts" '{
  "quote_id": 1,
  "contract_value": 500000,
  "currency": "INR",
  "start_date": "2026-02-03",
  "end_date": "2027-02-03",
  "sla_uptime_percentage": 99.9
}' "Create new contract"

# ============================================
# CONTACT TESTS
# ============================================
echo "========== CONTACT TESTS =========="
echo ""

test_endpoint "GET" "/crm/contacts" "" "Get all contacts"

test_endpoint "POST" "/crm/contacts" '{
  "name": "John Doe",
  "email": "john@company.com",
  "company_name": "Company Inc",
  "job_title": "Manager",
  "phone": "+91 9876543210",
  "status": "active"
}' "Create new contact"

# ============================================
# SUMMARY
# ============================================
echo ""
echo "================================"
echo "Test Summary"
echo "================================"
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo ""

if [ $FAILED -eq 0 ]; then
  echo -e "${GREEN}✓ All tests passed!${NC}"
  exit 0
else
  echo -e "${RED}✗ Some tests failed${NC}"
  exit 1
fi
