use sea_orm::ConnectionTrait;

use sea_orm::EntityTrait;
use sea_orm::{DatabaseConnection, Statement, Value};

use crate::parameter;

pub async fn get_parameter(
    db: &DatabaseConnection,
    parameter_name: &str,
) -> Result<String, anyhow::Error> {
    //sql
    let sql = "SELECT * FROM parameters WHERE parameter_name=?";

    let statement = Statement::from_sql_and_values(
        db.get_database_backend(),
        sql,
        vec![Value::String(Some(Box::new(parameter_name.to_string())))],
    );

    let parameter = parameter::Entity::find()
        .from_raw_sql(statement)
        .one(db)
        .await?;

    match parameter {
        Some(model) => {
            return Ok(model.parameter_value);
        }
        None => return Err(anyhow::anyhow!("Parameter not found")),
    }
}
