mod config;
mod models;
mod routes;
mod services;
mod utils;

use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use dotenv::from_filename;
use std::env;
use env_logger::Env;
use log::info;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    from_filename("src/.env").ok(); // Load environment variables from src/.env file
    env_logger::init_from_env(Env::default().default_filter_or("info")); // Initialize the logger
    config::init(); // Initialize configurations
    let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    info!("DATABASE_URL: {}", db_url); // Print the DATABASE_URL to verify
    let db = utils::db::MongoRepo::init(&db_url).await;

    HttpServer::new(move || {
        App::new()
            .wrap(
                Cors::default()
                    .allow_any_origin()
                    .allow_any_method()
                    .allow_any_header()
            )
            .app_data(web::Data::new(db.clone()))
            .configure(routes::init)
    })
    .bind(("0.0.0.0", 8080))?
    .run()
    .await
}