Function Rearchive_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        :   Rearchive_API

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

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

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$Insert IFX_EQUATES

Declare subroutine Service_Services, Logging_Services
Declare function OI_Wizard_Services, Environment_Services, Logging_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 rearchive.ID.POST

    LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\Archive\ReArchiving'
    LogDate     = Oconv(Date(), 'D4/')
    LogTime     = Oconv(Time(), 'MTS')
    LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' ArchiveService.csv'
    Headers     = 'Logging DTM' : @FM : 'Message'
    objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
    LoggingDTM  = LogDate : ' ' : LogTime
    
    ErrorMsg = ''
    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 = EndpointSegment
        LogData    = ''
        LogData<1> = LoggingDTM;
        LogData<2> = ArchiveId
        LogData<3> = 'Attempting to add re-archival to the process queue.'
        Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
        swap '__' with '*' in ArchiveId
        If RowExists('ARCHIVE', ArchiveId) then
            Service_Services('PostProcedure', 'ARCHIVE_SERVICES', 'ReArchive':SD$:ArchiveId)
            If Error_Services('NoError') then
                Message = 'Successfully queued data for re-archive.'
                LogData    = ''
                LogData<1> = LoggingDTM;
                LogData<2> = ArchiveId
                LogData<3> = Message
                Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$) 
            end else
                ErrorMsg = 'Error queueing re-archiving.'
                LogData    = ''
                LogData<1> = LoggingDTM;
                LogData<2> = ArchiveId
                LogData<3> = ErrorMsg
                Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
            end
        end else
            ErrorMsg = 'Archive record not found in database.'
            LogData    = ''
            LogData<1> = LoggingDTM;
            LogData<2> = ArchiveId
            LogData<3> = ErrorMsg
            Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
        end
        
        If ErrorMsg NE '' then
            HTTP_Services('SetResponseStatus', 500, ErrorMsg)
        end else
            HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
            If Assigned(Message) then
                HTTP_Services('SetResponseStatus', 201, Message)
            end else
                HTTP_Services('SetResponseStatus', 201)
            end
        end
        
    end else
        HTTP_Services('SetResponseStatus', 500, 'Invalid session. Reauthentication required.')
    end

end api
