use std::sync::Arc;
use std::time::Duration;

use tokio::sync::Mutex;

use chroma_storage::s3_client_for_test_with_new_bucket;
use uuid::Uuid;

use wal3::{
    create_repl_factories, Cursor, CursorName, CursorStoreOptions, Error, GarbageCollectionOptions,
    LogReaderOptions, LogWriter, LogWriterOptions, Manifest, ManifestManagerFactory,
    ReplicatedManifestManagerFactory, StorageWrapper,
};

mod common;
use common::{default_repl_options, setup_spanner_client};

type ReplLogWriter = LogWriter<
    wal3::FragmentUuid,
    wal3::ReplicatedFragmentManagerFactory,
    wal3::ReplicatedManifestManagerFactory,
>;

async fn writer_thread(
    writer: Arc<ReplLogWriter>,
    mutex: Arc<Mutex<()>>,
    wait: Arc<tokio::sync::Notify>,
    notify: Arc<tokio::sync::Notify>,
    iterations: usize,
) -> (usize, usize) {
    let cursors = writer.cursors(CursorStoreOptions::default()).unwrap();
    let mut witness = cursors
        .load(&CursorName::new("my_cursor").unwrap())
        .await
        .unwrap()
        .expect("test initialized a cursor so witness must be Some(_)");
    let mut successful_writes = 0;
    let mut contention_errors = 0;
    for i in 0..iterations {
        let message = format!("Message from writer: {}", i).into_bytes();
        wait.notified().await;
        let _guard = mutex.lock().await;
        loop {
            match writer.append(message.clone()).await {
                Ok(position) => {
                    println!("writer succeeds in iteration {i}");
                    successful_writes += 1;
                    witness = cursors
                        .save(
                            &CursorName::new("my_cursor").unwrap(),
                            &Cursor {
                                position,
                                epoch_us: position.offset(),
                                writer: "Test Writer".to_string(),
                            },
                            &witness,
                        )
                        .await
                        .unwrap();
                    writer
                        .reader(LogReaderOptions::default())
                        .await
                        .unwrap()
                        .scrub(wal3::Limits::default())
                        .await
                        .unwrap();
                    break;
                }
                Err(Error::LogContentionDurable)
                | Err(Error::LogContentionRetry)
                | Err(Error::LogContentionFailure) => {
                    println!("writer sees contention preventing {i}");
                    contention_errors += 1;
                    continue;
                }
                Err(e) => panic!("Unexpected error: {:?}", e),
            }
        }
        notify.notify_one();
    }
    (successful_writes, contention_errors)
}

async fn garbage_collector_thread(
    writer: Arc<ReplLogWriter>,
    mutex: Arc<Mutex<()>>,
    wait: Arc<tokio::sync::Notify>,
    notify: Arc<tokio::sync::Notify>,
    iterations: usize,
) -> (usize, usize) {
    println!("gc {:?}", &*writer as *const ReplLogWriter);
    let mut successes = 0;
    let mut contentions = 0;
    for i in 0..iterations {
        wait.notified().await;
        let _guard = mutex.lock().await;
        println!("gc grabs lock in iteration {i}");
        loop {
            match writer
                .garbage_collect(&GarbageCollectionOptions::default(), None)
                .await
            {
                Ok(()) => break,
                Err(Error::CorruptGarbage(m))
                    if m.starts_with("First to keep does not overlap manifest") =>
                {
                    println!("gc sees cursor ahead of manifest; only a problem if looping");
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    contentions += 1;
                    continue;
                }
                Err(Error::LogContentionDurable)
                | Err(Error::LogContentionRetry)
                | Err(Error::LogContentionFailure) => {
                    println!("gc sees contention preventing {i}");
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    contentions += 1;
                    continue;
                }
                Err(e) => panic!("unexpected error: {:?}", e),
            }
        }
        successes += 1;
        notify.notify_one();
    }
    (successes, contentions)
}

#[tokio::test]
async fn test_k8s_mcmr_integration_repl_98_garbage_alternate() {
    let client = setup_spanner_client().await;
    let log_id = Uuid::new_v4();

    let storage = s3_client_for_test_with_new_bucket().await;
    let prefix = format!("repl_98_garbage_alternate/{}", log_id);
    let wrapper = StorageWrapper::new("test-region".to_string(), storage.clone(), prefix.clone());
    let storages = Arc::new(vec![wrapper]);
    let writer_name = "init";

    // Initialize the log.
    let init_factory = ReplicatedManifestManagerFactory::new(Arc::clone(&client), log_id);
    init_factory
        .init_manifest(&Manifest::new_empty(writer_name))
        .await
        .expect("init should succeed");

    // Create a shared mutex that our two threads will use to coordinate access.
    let mutex = Arc::new(Mutex::new(()));

    // Create two writers that will contend with each other.
    let options1 = LogWriterOptions::default();
    let (fragment_factory1, manifest_factory1) = create_repl_factories(
        options1.clone(),
        default_repl_options(),
        Arc::clone(&storages),
        Arc::clone(&client),
        log_id,
    );
    let writer1 = Arc::new(
        LogWriter::open(
            options1,
            Arc::new(storage.clone()),
            &prefix,
            "writer1",
            fragment_factory1,
            manifest_factory1,
            None,
        )
        .await
        .expect("LogWriter::open should succeed"),
    );
    let cursors = writer1.cursors(CursorStoreOptions::default()).unwrap();
    cursors
        .init(&CursorName::new("my_cursor").unwrap(), Cursor::default())
        .await
        .expect("cursor init should succeed");

    let options2 = LogWriterOptions::default();
    let (fragment_factory2, manifest_factory2) = create_repl_factories(
        options2.clone(),
        default_repl_options(),
        Arc::clone(&storages),
        Arc::clone(&client),
        log_id,
    );
    let writer2 = Arc::new(
        LogWriter::open(
            options2,
            Arc::new(storage),
            &prefix,
            "writer2",
            fragment_factory2,
            manifest_factory2,
            None,
        )
        .await
        .expect("LogWriter::open should succeed"),
    );

    // Set a timer to make sure the test only runs for 3 minutes.
    let fail = tokio::spawn(async move {
        tokio::time::sleep(Duration::from_secs(180)).await;
        eprintln!("Taking down the test");
        std::process::exit(13);
    });

    let notify_writer = Arc::new(tokio::sync::Notify::new());
    let notify_gcer = Arc::new(tokio::sync::Notify::new());
    notify_writer.notify_one();

    // Launch both threads.
    let handle1 = tokio::spawn(writer_thread(
        Arc::clone(&writer1),
        Arc::clone(&mutex),
        Arc::clone(&notify_writer),
        Arc::clone(&notify_gcer),
        20,
    ));

    let handle2 = tokio::spawn(garbage_collector_thread(
        Arc::clone(&writer2),
        Arc::clone(&mutex),
        Arc::clone(&notify_gcer),
        Arc::clone(&notify_writer),
        20,
    ));

    // Wait for both threads to complete.
    let (writer1_results, writer2_results) = tokio::join!(handle1, handle2);
    fail.abort();

    // Examine results.
    let (writer1_successes, writer1_contentions) =
        writer1_results.expect("writer1 task should complete");
    let (writer2_successes, writer2_contentions) =
        writer2_results.expect("writer2 task should complete");

    println!(
        "Writer 1: {} successful writes, {} contentions",
        writer1_successes, writer1_contentions
    );
    println!(
        "Writer 2: {} successful writes, {} contentions",
        writer2_successes, writer2_contentions
    );

    // Assert some things about the test.
    assert!(
        writer1_successes > 0,
        "Writer 1 should have some successful writes"
    );
    assert!(
        writer2_successes > 0,
        "Writer 2 should have some successful writes"
    );

    println!("repl_98_garbage_alternate: passed, log_id={}", log_id);
}
