<%@ Language=VBScript %>
<%
Response.ContentType = "application/json"
Response.Charset = "UTF-8"
Response.AddHeader "Access-Control-Allow-Origin", "*"
Response.AddHeader "Cache-Control", "no-cache, no-store, must-revalidate"
%><!--#include file="./includes/wjf_employees-sql.asp"--><%

' Employee lookup API for Recognition notifications
' Input: sso (single SSO or comma-separated list)
' Output: JSON with employee names

Dim ssoInput, ssoList, names, jsonOutput
ssoInput = Trim(Request.QueryString("sso"))

If Len(ssoInput) = 0 Then
	Response.Write "{""success"":false,""error"":""SSO parameter required""}"
	Response.End
End If

' Split by comma for multiple SSOs
ssoList = Split(ssoInput, ",")
names = ""

Dim i, sso, cmd, rs, firstName, lastName
For i = 0 To UBound(ssoList)
	sso = Trim(ssoList(i))

	' Validate SSO is numeric
	If IsNumeric(sso) And Len(sso) > 0 Then
		' Query employee database
		Set cmd = Server.CreateObject("ADODB.Command")
		cmd.ActiveConnection = objconn
		cmd.CommandText = "SELECT First_Name, Last_Name FROM employees WHERE SSO = ?"
		cmd.CommandType = 1
		cmd.Parameters.Append cmd.CreateParameter("@sso", 3, 1, , CLng(sso))

		On Error Resume Next
		Set rs = cmd.Execute()

		If Err.Number = 0 And Not rs.EOF Then
			firstName = rs("First_Name") & ""
			lastName = rs("Last_Name") & ""
			If Len(names) > 0 Then names = names & ", "
			names = names & firstName & " " & lastName
		End If

		If Not rs Is Nothing Then
			If rs.State = 1 Then rs.Close
			Set rs = Nothing
		End If
		Set cmd = Nothing
		On Error GoTo 0
	End If
Next

If Len(names) > 0 Then
	jsonOutput = "{""success"":true,""names"":""" & JSEscape(names) & """}"
Else
	jsonOutput = "{""success"":false,""error"":""No employees found""}"
End If

Response.Write jsonOutput
objconn.Close

Function JSEscape(s)
	Dim r
	r = s
	r = Replace(r, "\", "\\")
	r = Replace(r, """", "\""")
	r = Replace(r, Chr(13), "")
	r = Replace(r, Chr(10), "\n")
	r = Replace(r, Chr(9), "\t")
	JSEscape = r
End Function
%>
