Function Clean_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        :   Clean_API

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

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

#pragma precomp SRP_PreCompiler

Declare function OI_Wizard_Services, Lot_Operation_Services, Database_Services, Lot_Services, Clean_Services
Declare subroutine Clean_Services, Lot_Services

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$insert OI_WIZARD_EQUATES
$insert LOT_OPERATION_EQUATES

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 clean.ID.HEAD
API clean.ID.GET

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        StatusCode  = ''
        CleanRecId = EndpointSegment
        CleanRecJson = Clean_Services('ConvertCleanRecToJson', CleanRecId)
        If Error_Services('NoError') then
            Http_Services('SetResponseBody', CleanRecJson, False$, 'application/hal+json')
            ResponseCode = 200
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API clean.ID.markcleanrecordcomplete.POST

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        StatusCode  = ''
        Body        = HTTP_Services('GetHTTPPostString', True$)
        // The POST string will have been encoded so use percent (URL) decoding.
        DecodedJSON = HTTP_Services('DecodePercentString', Body)
        If SRP_JSON(objBody, 'Parse', Body) EQ '' then
            CleanRecId = ParentSegment
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'LotOperationId')
            CleanTool   = SRP_JSON(objBody, 'GetValue', 'CleanTool')
            CleanRecipe   = SRP_JSON(objBody, 'GetValue', 'CleanRecipe')
            SRP_JSON(objBody, 'Release')
            Clean_Services('MarkCleanRecComplete', CleanRecId, CleanRecipe, CleanTool, UserId)
            If Error_Services('NoError') then
                CleanRecJson = Clean_Services('ConvertCleanRecToJson', CleanRecId)
                HTTP_Services('SetResponseBody', CleanRecJson, False$, 'application/hal+json')
                ResponseCode = 200
            end else
                ErrorMessage = Error_Services('GetMessage')
                ResponseCode = 500
            end
        end
        
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API clean.createcleanrecord.POST

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        StatusCode  = ''
        Body        = HTTP_Services('GetHTTPPostString', True$)
        // The POST string will have been encoded so use percent (URL) decoding.
        DecodedJSON = HTTP_Services('DecodePercentString', Body)
        If SRP_JSON(objBody, 'Parse', Body) EQ '' then
            LotId = SRP_JSON(objBody, 'GetValue', 'LotId')
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'LotOperationId')
            SRP_JSON(objBody, 'Release')
        end
        CleanRecId = Clean_Services('CreateNewCleanRecord', LotId, LotOperationId, UserId)
        If Error_Services('NoError') then
            CleanRecJson = Clean_Services('ConvertCleanRecToJson', CleanRecId)
            HTTP_Services('SetResponseBody', CleanRecJson, False$, 'application/hal+json')
            ResponseCode = 200
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API clean.getnewcleanoperationparams.HEAD
API clean.getnewcleanoperationparams.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)
            LotId = Http_Services('GetQueryField', 'LotId')
            objJSONResponse = ''
            If SRP_Json(objJSONResponse, 'New', 'Object') then
                //Available Tools
                If SRP_Json(objCleanTools, 'New', 'Array') then
                    CleanTools = Clean_Services('GetCleanToolOptions')
                    for each CleanTool in CleanTools using @FM
                        SRP_Json(objCleanTools, 'AddValue', CleanTool, 'String')
                    Next CleanTool
                    SRP_Json(objJsonResponse, 'Set', 'CleanToolOptions', objCleanTools)
                    SRP_Json(objCleanTools, 'Release')
                end
                //Available Recipes
                If SRP_Json(objCleanRecipes, 'New', 'Array') then
                    CleanRecipes = Clean_Services('GetCleanRecipeOptions')
                    for each Recipe in CleanRecipes using @VM
                        SRP_Json(objCleanRecipes, 'AddValue', Recipe, 'String')
                    Next Recipe
                    SRP_Json(objJsonResponse, 'Set', 'CleanRecipeOptions', objCleanRecipes)
                    SRP_Json(objCleanRecipes, 'Release')
                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
