/***********************************************************************************************************
 * Name         : EventLogViewerHelperTest
 * Purpose      : Test class for EventLogViewerHelper
 **********************************************************************************************************/
@IsTest
private class EventLogViewerHelperTest {
    @TestSetup
    static void makeData() {
        System.runAs(new User(Id = UserInfo.getUserId())) {
            ContentWorkspace library = new ContentWorkspace(Name = 'EventLogs_' + DateTime.now().getTime());
            insert library;
            insert new Config__c(
                Name = ConfigUtils.EVENT_LOG_ARCHIVE_LIBRARY_ID,
                Value__c = library.Id
            );
        }
    }

    /************************************************************************************
     * Method: getEventLogs
     * APEX method tested : EventLogViewerHelper.getEventLogs
     * Description : Verifies the method returns a non-null list without throwing an exception
     *************************************************************************************/
    @IsTest
    static void getEventLogs() {
        Test.startTest();
        List<EventLogFile> result = EventLogViewerHelper.getEventLogs();
        Test.stopTest();

        // EventLogFile records cannot be inserted in tests; verify no exception is thrown
        Assert.isNotNull(result, 'Result should not be null');
    }

    /************************************************************************************
     * Method: getArchivedLogs
     * APEX method tested : EventLogViewerHelper.getArchivedLogs
     * Description : Verifies only ContentVersions tagged with the EventLog description and
     *  published to the configured library are returned
     *************************************************************************************/
    @IsTest
    static void getArchivedLogs() {
        Id libraryId = [SELECT Value__c FROM Config__c WHERE Name = :ConfigUtils.EVENT_LOG_ARCHIVE_LIBRARY_ID LIMIT 1].Value__c;

        ContentVersion cv = new ContentVersion(
            Title = 'TestArchive_2024-01-01',
            PathOnClient = 'TestArchive_2024-01-01.csv',
            Description = EventLogFileArchiveBatch.EVENT_LOG,
            VersionData = Blob.valueOf('col1,col2\nval1,val2'),
            FirstPublishLocationId = libraryId
        );

        // Insert a ContentVersion that should NOT be returned (different description)
        ContentVersion other = new ContentVersion(
            Title = 'OtherFile',
            PathOnClient = 'OtherFile.csv',
            Description = 'SomethingElse',
            VersionData = Blob.valueOf('data'),
            FirstPublishLocationId = libraryId
        );
        insert new List<ContentVersion>{ cv, other };

        Test.startTest();
        List<ContentVersion> result = EventLogViewerHelper.getArchivedLogs();
        Test.stopTest();

        Assert.areEqual(1, result.size(), 'Only EventLog-tagged ContentVersions should be returned');
        Assert.areEqual('TestArchive_2024-01-01', result[0].Title);
    }

    /************************************************************************************
     * Method: getEventLogFileContent
     * APEX method tested : EventLogViewerHelper.getEventLogFileContent
     * Description : Verifies the method handles an invalid Id by throwing the expected exception
     *************************************************************************************/
    @IsTest
    static void getEventLogFileContent() {
        // EventLogFile cannot be inserted in tests; verify the method signature is accessible
        // and that querying a non-existent Id throws a QueryException
        try {
            EventLogViewerHelper.getEventLogFileContent('000000000000000AAA');
            Assert.fail('Expected QueryException for invalid Id');
        } catch (System.QueryException ex) {
            Assert.isTrue(ex.getMessage() != null);
        } catch (System.StringException ex) {
            // Invalid Id format — acceptable in this context
            Assert.isTrue(ex.getMessage() != null);
        }
    }
}
