Function Monaengines_API(@API)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Monaengines_API

    Description :   API logic for the Monaengines resource.

    Notes       :   All web APIs should include the API_SETUP insert. This will provide several useful variables:

                        HTTPMethod              - The HTTP Method (Verb) submitted by the client (e.g., GET, POST, etc.)
                        APIURL                  - The URL for the API entry point (e.g., api.mysite.com/v1).
                        FullEndpointURL         - The URL submitted by the client, including query params.
                        FullEndpointURLNoQuery  - The URL submitted by the client, excluding query params.
                        EndpointSegment         - The URL endpoint segment.
                        ParentURL               - The URL path preceeding the current endpoint.
                        CurrentAPI              - The name of this stored procedure.

    Parameters  :
        API             [in] -- Web API to process. Format is [APIPattern].[HTTPMethod]:
                                    - APIPattern must follow this structure Monaengines[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Monaengines.POST
                                    - Monaengines.ID.PUT
                                    - Monaengines.ID.firstName.GET
        Response       [out] -- Response to be sent back to the Controller (HTTP_MCP) or requesting procedure. Web API
                                services do not rely upon anything being returned in the response. This is what the
                                various services like SetResponseBody and SetResponseStatus services are for. A response
                                value is only helpful if the developers want to use it for debug purposes.

    History     :   (Date, Initials, Notes)
        07/17/24    xxx     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$insert ENGINE_HEALTH_EQUATES

Equ Comma$ to ','

Declare function System_Healthcheck_Services, Environment_Services, Logging_Services
Declare subroutine Logging_Services

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\Healthchecks\Engines\MonA'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Mona_Engine_Health_Check_Log.csv'
Headers     = 'Logging DTM' : @FM : 'EngineID' : @FM : 'ResponseCode' : @FM : 'Message' : @FM : 'Requester'
objLogMonaEngines      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

GoToAPI else
    // The specific resource endpoint doesn't have a API handler yet.
    HTTP_Services('SetResponseStatus', 200, 'This is a valid endpoint but a web API handler has not yet been created.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Endpoint Handlers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


API monaengines.HEAD
API monaengines.GET

    HTTP_Resource_Services('LoremIpsum')

end api


API monaengines.ID.HEAD
API monaengines.ID.GET

   //Returns status of a defined engine
    EngineID = EndpointSegment
    If RowExists('APP_INFO', EngineID) then
    	EngineHealthInfo = System_Healthcheck_Services('GetEngineHealthInfo', EngineID)
    	If Error_Services('NoError') then
    		Healthy = EngineHealthInfo<ENGINE_HEALTH_HEALTH_STATUS$>
    		If Healthy then
    			ResponseCode = 200
    			Message = 'Engine is healthy'
    			HTTP_Services('SetResponseStatus', ResponseCode, Message)
    			LogData    = ''
				LogData<1> = LoggingDTM
				LogData<2> = EngineID
				LogData<3> = ResponseCode
				LogData<4> = Message
				LogData<5> = FullEndpointURL
				Logging_Services('AppendLog', objLogMonaEngines, LogData, @RM, @FM)
    		end else
    			ResponseCode = 418
    			Message = 'Engine is unhealthy.'
    			HTTP_Services('SetResponseStatus', ResponseCode, Message)
				LogData    = ''
				LogData<1> = LoggingDTM
				LogData<2> = EngineID
				LogData<3> = ResponseCode
				LogData<4> = Message
				LogData<5> = FullEndpointURL
				Logging_Services('AppendLog', objLogMonaEngines, LogData, @RM, @FM)
    		end
    	end else
    		ResponseCode = 500
    		Message = Error_Services('GetMessage')
    		HTTP_Services('SetResponseStatus', ResponseCode, Message)
			LogData    = ''
			LogData<1> = LoggingDTM
			LogData<2> = EngineID
			LogData<3> = ResponseCode
			LogData<4> = Message
			LogData<5> = FullEndpointURL
			Logging_Services('AppendLog', objLogMonaEngines, LogData, @RM, @FM)
    	end
    end else
    	ResponseCode = 404
    	Message = 'Invalid Engine ID'
    	HTTP_Services('SetResponseStatus', ResponseCode, 'Invalid Engine ID.')
		LogData    = ''
		LogData<1> = LoggingDTM
		LogData<2> = EngineID
		LogData<3> = ResponseCode
		LogData<4> = Message
		LogData<5> = FullEndpointURL
		Logging_Services('AppendLog', objLogMonaEngines, LogData, @RM, @FM)
    end

end api

