mod util;
mod endpoints;
mod models;
mod repository;

use actix_web::{HttpServer, App, web, middleware};
use util::constants;
use util::debug;
// use repository::remote::redis;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    util::colored_text::print_colored_text_brand("> SocioCybereeng ");
    util::colored_text::print_colored_text_project("> Project: Hydroponic Systems ");
    util::colored_text::print_colored_text_info("- [Welcome to ResourceHUB API] ");

    if util::constants::IS_DEBUG == true {
        debug::print_debug_info();
    }
    
    util::colored_text::print_colored_text_info( 
        &format!("Starting HTTP server at http://{}:{}/api/v1/",
        constants::SERVER_URL, constants::SERVER_PORT));

    HttpServer::new(|| { 
      App::new()
        // enable logger - always register Actix Web Logger middleware last
        .wrap(middleware::Logger::default().log_target("@"))

        .service(
          web::scope( &format!("/api/v{}", constants::API_VERSION) )
            .service(endpoints::sensors::data::get)
            .service(endpoints::sensors::data::get_by_id)
            .service(endpoints::sensors::data::create)
        )
        
        // Service /api/v2
        // Service /api/v3
        // Service /api/v4
        // ... 

      })
    .bind((constants::SERVER_URL, constants::SERVER_PORT))?
    .run()
    .await

}

// TODO - ADD TLS (HTTPS)
// .bind_rustls_0_23(("127.0.0.1", 443), config)? // HTTPS port
// https://github.com/actix/examples/blob/main/middleware/http-to-https/src/main.rs

// TODO - ADD CORS 
// https://actix.rs/docs/cors


//    // Get database pool
//    // let pool = redis::get_pool(constants::DB_URI).await;


//    // Attempt to connect to DB and check the result

//    // Start server
//            endpoints::sensors::data::create,
//            endpoints::sensors::data::get,
//            // endpoints::sensors::data::get_by_id,
