use super::route_prelude::*;

pub fn resp_delete_error() -> Response {
    Response::temporary_failure("Database delete error")
}

pub async fn db_purge(
    _ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let Ok(_) = user
        .client
        .database()
        .delete(
            Filter::new()
                .kind(Kind::TextNote)
                .kind(Kind::LongFormTextNote)
                .until(Timestamp::now() - dur_parse("6m").unwrap()),
        )
        .await
    else {
        return resp_delete_error();
    };

    let Ok(_) = user
        .client
        .database()
        .delete(
            Filter::new()
                .kind(Kind::Metadata)
                .until(Timestamp::now() - dur_parse("6m").unwrap()),
        )
        .await
    else {
        return resp_delete_error();
    };

    Response::success("OK")
}

pub async fn db_wipe(
    _ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    match user.client.database().wipe().await {
        Ok(_) => Response::success("OK"),
        Err(err) => Response::temporary_failure(format!("Wipe error: {err}")),
    }
}
