use actix_web::{get, post};
use actix_web::{Error, HttpResponse};
use actix_web::web::{Path, Json};
use crate::models::sensors::SensorData;
use crate::models::sensors::get_sensor_data; // TODO - DELETE LATER, WHEN create data obj in Database WILL ADDED

// use crate::repository::remote::redis;
// use crate::repository::remote::sqlite;
// use crate::repository::remote::postgres;

// get data json obj
    // database.getSensorData()
    // send 200 http response with json obj
// 500

// create data json obj
    // database.create(obj)
    // send 200 http response
// 500


// TODO - add json deserialization anwer to client, maybe

#[post("/sensors/data")]
pub async fn create(data: Json<SensorData>) -> HttpResponse {
    let _received_data = data.into_inner();

    // match let database.create(data) = true {
        // Ok(HttpResponse::Ok().json(received_data))   
    // }

    // return data to client
    HttpResponse::Ok().json(200)
}

#[get("/sensors/data")]
pub async fn get() -> Result<HttpResponse, Error> {
    // get pool db
    // let pool_db = 

    // get all data from DB
    let data = get_sensor_data();

    // return data to client
    Ok(HttpResponse::Ok().json(data))    
}

#[get("/sensors/data/{id}")]
pub async fn get_by_id(id: Path<u32>) -> Result<HttpResponse, Error> {
    println!("{}", id);

    // Vulnirability: danger if ID in sql requsets

    // get pool db
    // let pool_db = 

    // get all data from DB
    let data = get_sensor_data();

    // return data to client
    Ok(HttpResponse::Ok().json(data))
}


// Vulnirability: danger if ID in sql requsets !!!
// #[get("/sensors/data/<id>")]
// pub async fn get_by_id(id: u32) -> status::Accepted<Json<SensorData>> {
//     // http://127.0.0.1:8000/api/v1/sensors/data/?query=SELECT%20id%20FROM%20sensors
    
//     // get data from DB
//     let data = get_sensor_data();
    
//     status::Accepted(Json(data))
// }

