use super::*;

#[test]
fn reference_catalog_tables_match_ducklake_initializer_shape() {
    let names = REFERENCE_CATALOG_TABLES
        .iter()
        .map(|table| table.name)
        .collect::<Vec<_>>();
    assert_eq!(
        names,
        vec![
            "ducklake_metadata",
            "ducklake_snapshot",
            "ducklake_snapshot_changes",
            "ducklake_schema",
            "ducklake_table",
            "ducklake_view",
            "ducklake_tag",
            "ducklake_column_tag",
            "ducklake_data_file",
            "ducklake_file_column_stats",
            "ducklake_file_variant_stats",
            "ducklake_delete_file",
            "ducklake_column",
            "ducklake_table_stats",
            "ducklake_table_column_stats",
            "ducklake_partition_info",
            "ducklake_partition_column",
            "ducklake_file_partition_value",
            "ducklake_files_scheduled_for_deletion",
            "ducklake_inlined_data_tables",
            "ducklake_column_mapping",
            "ducklake_name_mapping",
            "ducklake_schema_versions",
            "ducklake_macro",
            "ducklake_macro_impl",
            "ducklake_macro_parameters",
            "ducklake_sort_info",
            "ducklake_sort_expression",
        ]
    );

    let data_file = REFERENCE_CATALOG_TABLES
        .iter()
        .find(|table| table.name == "ducklake_data_file")
        .unwrap();
    assert_eq!(
        data_file
            .columns
            .iter()
            .map(|column| column.name)
            .collect::<Vec<_>>(),
        vec![
            "data_file_id",
            "table_id",
            "begin_snapshot",
            "end_snapshot",
            "file_order",
            "path",
            "path_is_relative",
            "file_format",
            "record_count",
            "file_size_bytes",
            "footer_size",
            "row_id_start",
            "partition_id",
            "encryption_key",
            "mapping_id",
            "partial_max",
        ]
    );
    assert!(data_file.columns[0].primary_key);

    let metadata = REFERENCE_CATALOG_TABLES
        .iter()
        .find(|table| table.name == "ducklake_metadata")
        .unwrap();
    assert_eq!(metadata.columns[0].sql_type, "VARCHAR NOT NULL");
    assert_eq!(metadata.columns[1].sql_type, "VARCHAR NOT NULL");
}

#[test]
fn bootstrap_uses_reference_rows_and_generated_uuid() {
    let bootstrap = CatalogBootstrap::new("/tmp/lake", true, "NOW()");
    Uuid::parse_str(&bootstrap.schema_uuid).unwrap();
    let sql = render_bootstrap_statements(Some("meta"), &bootstrap).join(";\n");

    assert!(sql.contains("INSERT INTO \"meta\".ducklake_snapshot VALUES (0, NOW(), 0, 1, 0)"));
    assert!(sql.contains("'created_schema:\"main\"'"));
    assert!(sql.contains("('version', '1.0')"));
    assert!(sql.contains("('data_path', '/tmp/lake/')"));
    assert!(sql.contains("('encrypted', 'true')"));
    assert!(sql.contains("'main', 'main/', true"));
}

#[test]
fn bootstrap_from_options_requires_or_defaults_data_path() {
    let err = bootstrap_from_options(&DuckLakeCatalogInitOptions::default(), None, "NOW()")
        .unwrap_err()
        .to_string();
    assert!(err.contains("DATA_PATH is not set"));

    let bootstrap = bootstrap_from_options(
        &DuckLakeCatalogInitOptions::default(),
        Some("catalog.files"),
        "NOW()",
    )
    .unwrap();
    assert_eq!(bootstrap.data_path, "catalog.files/");

    let explicit = DuckLakeCatalogInitOptions {
        data_path: Some("explicit".into()),
        encrypted: Some(true),
        ..Default::default()
    };
    let bootstrap = bootstrap_from_options(&explicit, Some("catalog.files"), "NOW()").unwrap();
    assert_eq!(bootstrap.data_path, "explicit/");
    assert!(bootstrap.encrypted);
}
