/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use diskann::ANNResult;

use super::{CheckpointManager, WorkStage};

/// A naive implementation of the CheckpointRecordManagerTrait
/// .. that always returns the default checkpoint record.
/// This implementation is used for keeping the backward compatibility
/// .. with the previous version of DiskANN for both DiskIndex and InMemoryIndex.
#[derive(Default, Clone)]
pub struct NaiveCheckpointRecordManager;

impl CheckpointManager for NaiveCheckpointRecordManager {
    fn get_resumption_point(&self, _stage: WorkStage) -> ANNResult<Option<usize>> {
        Ok(Some(0))
    }

    fn update(
        &mut self,
        _progress: super::Progress,
        _next_stage: super::WorkStage,
    ) -> ANNResult<()> {
        Ok(())
    }

    fn mark_as_invalid(&mut self) -> ANNResult<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::super::Progress;
    use super::*;

    #[test]
    fn test_naive_checkpoint_record_manager_default() {
        let manager = NaiveCheckpointRecordManager;
        // Test get_resumption_point always returns Some(0)
        let result = manager.get_resumption_point(WorkStage::Start);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some(0));
    }

    #[test]
    fn test_naive_checkpoint_record_manager_get_resumption_point() {
        let manager = NaiveCheckpointRecordManager;

        // Test with various stages
        for stage in [WorkStage::Start, WorkStage::End, WorkStage::QuantizeFPV] {
            let result = manager.get_resumption_point(stage);
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some(0));
        }
    }

    #[test]
    fn test_naive_checkpoint_record_manager_update() {
        let mut manager = NaiveCheckpointRecordManager;

        // Update should always succeed
        let result = manager.update(Progress::Completed, WorkStage::End);
        assert!(result.is_ok());

        let result = manager.update(Progress::Processed(100), WorkStage::InMemIndexBuild);
        assert!(result.is_ok());
    }

    #[test]
    fn test_naive_checkpoint_record_manager_mark_as_invalid() {
        let mut manager = NaiveCheckpointRecordManager;

        // mark_as_invalid should always succeed
        let result = manager.mark_as_invalid();
        assert!(result.is_ok());
    }
}
