Function Archive_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        :   Archive_API

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

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

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$insert SRPJSONX

Declare Function OI_WIZARD_SERVICES, ARCHIVE_SERVICES, Date_Services

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

    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
        ArchiveIds = Archive_Services('GetAllArchiveIDs')
        ArchiveListJson = ''
        GoSub GenerateArchiveListJson
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', ArchiveListJson, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', 201, Message)
        end else
            HTTP_Services('SetResponseStatus', 201)
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api


API archive.getarchive.HEAD
API archive.getarchive.GET

    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
        ArchiveId = Http_Services('GetQueryField', 'ArchiveId')
        ArchiveJson = Archive_Services('ConvertArchiveRecordToJson', ArchiveId)
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', ArchiveJson, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', 201, Message)
        end else
            HTTP_Services('SetResponseStatus', 201)
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end


end api


API archive.findarchivebyrecord.HEAD
API archive.findarchivebyrecord.GET
    
    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
        
        Table = Http_Services('GetQueryField', 'Table')
        RecordId = Http_Services('GetQueryField', 'RecordId')
        ArchiveIds = Archive_Services('GetArchiveIDsByRecord', RecordId, Table)
        
        SRP_JsonX_Begin('JSON', '{')
            SRP_JsonX('ArchiveRecords','[')
                for each ArchiveId in ArchiveIds using @FM
                    ThisArchiveJson = Archive_Services('ConvertArchiveRecordToJson', ArchiveId)
                    SRP_JsonX(ThisArchiveJson)
                Next ArchiveId
            SRP_JsonX(']')
        ArchiveJson = SRP_JsonX_End('Pretty')
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', ArchiveJson, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', 201, Message)
        end else
            HTTP_Services('SetResponseStatus', 201)
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api


API archive.findarchivebymetadata.HEAD
API archive.findarchivebymetadata.GET

    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
        MetaDataType = Http_Services('GetQueryField', 'MetaDataType')
        SearchValues = Http_Services('GetQueryField', 'SearchValue')
        ArchiveIds = Archive_Services('GetArchiveIDsByMetaData', MetaDataType, SearchValues)
        
        SRP_JsonX_Begin('JSON', '{')
            SRP_JsonX('ArchiveRecords','[')
                for each ArchiveId in ArchiveIds using @FM
                    ThisArchiveJson = Archive_Services('ConvertArchiveRecordToJson', ArchiveId)
                    SRP_JsonX(ThisArchiveJson)
                Next ArchiveId
            SRP_JsonX(']')
        ArchiveJson = SRP_JsonX_End('Pretty')
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', ArchiveJson, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', 201, Message)
        end else
            HTTP_Services('SetResponseStatus', 201)
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GenerateArchiveListJson:
    
    If Assigned(ArchiveListJson) AND Assigned(ArchiveIds)  then
        SRP_JsonX_Begin('JSON', '{')
            SRP_JsonX('Archives', '[')
                for each ArchiveId in ArchiveIds using @FM
                    ArchiveType = Field(ArchiveId, '*', 1)
                    ArchiveParent = Field(ArchiveId, '*', 2)
                    CreationDtm = Date_Services('ConvertDateTimeToISO8601', XLATE('ARCHIVE', ArchiveId, ARCHIVE_ARCHIVE_CREATION_DTM$, 'X'))
                    RecordCount = DCOUNT(XLATE('ARCHIVE', ArchiveId, ARCHIVE_CHILD_RECORD$, 'X'), @VM)
                    SRP_JsonX('{')
                        SRP_JsonX('ArchiveId', ArchiveId, 'String')
                        SRP_JsonX('ArchiveType', ArchiveType, 'String')
                        SRP_JsonX('ArchiveParent', ArchiveParent, 'String')
                        SRP_JsonX('CreationDtm', CreationDtm, 'String')
                        SRP_JsonX('RecordCount', RecordCount)
                    SRP_JsonX('}')
                Next ArchiveId
            SRP_JsonX(']')
        ArchiveListJson = SRP_JsonX_End('Pretty')
    end
    
    
return



