Function Operation_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        :   Operation_API

    Description :   API logic for the Operation 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 Operation[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Operation.POST
                                    - Operation.ID.PUT
                                    - Operation.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)
        05/19/25    xxx     Original programmer.

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

#pragma precomp SRP_PreCompiler

Declare function OI_Wizard_Services, Operation_Services, Clean_Services, Lot_Operation_Services

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS

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

Return Response OR ''


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


API operation.HEAD
API operation.GET

    JSONCollection = ''
    OIWizardID = ''
    Cookies    = HTTP_Services('GetHTTPCookie')
    For each Cookie in Cookies using ';'
        Key = Field(Cookie, '=', 1)
        If Key EQ 'sessionID' then
            OIWizardID = Field(Cookie, '=', 2)
        end
    Next Cookie
    
    ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)
    If ValidSession then
        Body    = HTTP_Services('GetHTTPGetString')
        If Body NE '' then
            RequestJson      = HTTP_Services('DecodePercentString', Body)
            ClassFilter = Http_Services('GetQueryField', 'Class')
            ActiveFilter = Http_Services('GetQueryField', 'Active')
            LotId = Http_Services('GetQueryField', 'LotId')
            Operations = Operation_Services('GetOperations', ClassFilter, ActiveFilter)
            
            objJSONResponse = ''
            If SRP_Json(objJSONResponse, 'New', 'Object') then
                //Available Operations
                If SRP_Json(objOperations, 'New', 'Array') then
                    for each Operation in Operations using @VM
                        OperationJSONString = Operation_Services('ConvertRecordToJSON', Operation)
                        objOperation = ''
                        If SRP_Json(objOperation, 'Parse', OperationJSONString) EQ '' then
                            SRP_Json(objOperations, 'Add', objOperation)
                            SRP_Json(objOperation, 'Release')
                        end
                    Next Operation
                    SRP_Json(objJsonResponse, 'Set', 'Operations', objOperations)
                    SRP_Json(objOperations, 'Release')
                end else
                    Error_Services('Add', 'Error when creating Operation array in JSON response.')
                end
                //Available Sequences
                Sequences = Lot_Operation_Services('GetAvailableSequences', LotId)
                If SRP_Json(objSequences, 'New', 'Array') then
                    for each Sequence in Sequences using @VM
                        SRP_Json(objSequences, 'AddValue', Sequence, 'Number')
                    Next Sequence
                    SRP_Json(objJsonResponse, 'Set', 'AvailableSequences', objSequences)
                    SRP_Json(objSequences, 'Release')
                end else
                    Error_Services('Add', 'Error when creating Operation array in JSON response.')
                end
                JsonResponse = SRP_Json(objJsonResponse, 'Stringify', 'Styled')
                SRP_Json(objJsonResponse, 'Release')
            end else
                Error_Services('Add', 'Error when creating JSON response.')
            end
        end else
            Error_Services('Add', 'No body was sent with the request.')
        end
        If Error_Services('NoError') then
            HTTP_Services('SetResponseStatus', 201, 'Success')
            HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
            HTTP_Services('SetResponseBody', JsonResponse, False$, 'application/hal+json')
        end else
            HTTP_Services('SetResponseStatus', 400, Error_Services('GetMessage'))
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api
