use super::*;

#[test]
fn descriptors_render_the_shipped_sql() {
    assert_eq!(
        snapshots().render_embedded(),
        "SELECT snapshot_id, snapshot_time, schema_version, next_catalog_id, next_file_id FROM \"ducklake_snapshot\" ORDER BY snapshot_id"
    );
    let query = tables_at(SchemaId(3), SnapshotId(9));
    assert_eq!(
        query.render_embedded(),
        "SELECT table_id, table_uuid, begin_snapshot, end_snapshot, schema_id, table_name, path, path_is_relative FROM \"ducklake_table\" WHERE schema_id = ?1 AND ?2 >= begin_snapshot AND (end_snapshot IS NULL OR ?2 < end_snapshot) ORDER BY table_name"
    );
    assert_eq!(query.params, vec![3, 9]);
}

#[cfg(feature = "postgres")]
#[test]
fn descriptors_render_postgres_qualified_sql() {
    assert_eq!(
        snapshots().render_postgres("ducklake meta"),
        "SELECT snapshot_id, (EXTRACT(EPOCH FROM snapshot_time) * 1000000)::BIGINT, schema_version, next_catalog_id, next_file_id FROM \"ducklake meta\".\"ducklake_snapshot\" ORDER BY snapshot_id"
    );
    assert_eq!(
        schemas_at(SnapshotId(9)).render_postgres("m"),
        "SELECT schema_id, schema_uuid::TEXT, begin_snapshot, end_snapshot, schema_name, path, path_is_relative FROM \"m\".\"ducklake_schema\" WHERE $1 >= begin_snapshot AND (end_snapshot IS NULL OR $1 < end_snapshot) ORDER BY schema_name"
    );
}

#[cfg(feature = "quack")]
#[test]
fn descriptors_render_quack_qualified_sql() {
    assert_eq!(
        snapshots().render_quack_with_metadata_catalog(Some("ducklake meta")),
        "SELECT snapshot_id, snapshot_time, schema_version, next_catalog_id, next_file_id FROM \"ducklake meta\".\"ducklake_snapshot\" ORDER BY snapshot_id"
    );
    assert_eq!(
        schemas_at(SnapshotId(9)).render_quack_with_metadata_catalog(Some("m")),
        "SELECT schema_id, schema_uuid, begin_snapshot, end_snapshot, schema_name, path, path_is_relative FROM \"m\".\"ducklake_schema\" WHERE 9 >= begin_snapshot AND (end_snapshot IS NULL OR 9 < end_snapshot) ORDER BY schema_name"
    );
}

#[test]
fn inlined_file_deletions_quotes_the_table_identifier() {
    let query = inlined_file_deletions("ducklake_inlined_delete_7", SnapshotId(4));
    assert_eq!(
        query.render_embedded(),
        "SELECT file_id, row_id, begin_snapshot FROM \"ducklake_inlined_delete_7\" \
         WHERE begin_snapshot <= ?1 ORDER BY file_id, row_id"
    );
    assert_eq!(query.params, vec![4]);
}

#[cfg(feature = "postgres")]
#[test]
fn inlined_file_deletions_render_postgres_schema_qualified_table() {
    let query = inlined_file_deletions("ducklake_inlined_delete_7", SnapshotId(4));
    assert_eq!(
        query.render_postgres("ducklake meta"),
        "SELECT file_id, row_id, begin_snapshot FROM \"ducklake meta\".\"ducklake_inlined_delete_7\" \
         WHERE begin_snapshot <= $1 ORDER BY file_id, row_id"
    );
    assert_eq!(query.params, vec![4]);
}

#[cfg(feature = "quack")]
#[test]
fn renders_integer_placeholders_for_quack_without_prefix_clobbering() {
    let sql = "WHERE a = ?1 OR b BETWEEN ?2 AND ?2 OR c = ?10";
    assert_eq!(
        render_placeholders_as_literals(sql, &[11, 22, 33, 44, 55, 66, 77, 88, 99, 1000]),
        "WHERE a = 11 OR b BETWEEN 22 AND 22 OR c = 1000"
    );
}
