// SPDX-FileCopyrightText: 2025 Slatian
//
// SPDX-License-Identifier: LGPL-3.0-only

//! Integrates the NumberCriterium with rusqlite

use rusqlite::types::Value;

use crate::rusqlite::ToRusqliteSingleField;
use crate::sql::Field;
use crate::BooleanCriterium;

/// Implement BooleanCriterium for use with rusqlite
///
/// The boolean Criterium will encode its values directly as SQL logic
/// and therefore not need any values.
impl<F: Field> ToRusqliteSingleField<F> for BooleanCriterium {
	fn get_sql_where(&self, field_name: &str) -> String {
		match self {
			Self::Equals(true) => format!("COALESCE({}, 0)", field_name),
			Self::Equals(false) => format!("COALESCE(NOT {}, 0)", field_name),
			Self::IsNone => format!("{field_name} is NULL"),
		}
	}

	fn get_inverted_sql_where(&self, field_name: &str) -> Option<String> {
		Some(match self {
			Self::Equals(true) => format!("COALESCE(NOT {}, 1)", field_name),
			Self::Equals(false) => format!("COALESCE({}, 1)", field_name),
			Self::IsNone => format!("{field_name} is not NULL"),
		})
	}

	fn get_where_values(&self, _is_inverted: bool) -> Vec<Value> {
		Vec::new()
	}
}
