use diesel::{r2d2::ConnectionManager, PgConnection};
use siren::ServiceError;
use crate::diesel_migrations::MigrationHarness;
use lazy_static::lazy_static;
use log::{error, info};
use r2d2;
use std::env;

pub mod backgrounds;
pub mod bestiary;
pub mod classes;
pub mod conditions;
pub mod feats;
pub mod guilds;
pub mod items;
pub mod messages;
pub mod options;
pub mod races;
pub mod spells;
pub mod users;
pub mod schema;

type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;

pub const MIGRATIONS: diesel_migrations::EmbeddedMigrations = embed_migrations!();

lazy_static! {
  static ref POOL: Pool = {
    let username = env::var("DATABASE_USER").expect("DATABASE_USERNAME is not set");
    let password = env::var("DATABASE_PASSWORD").expect("DATABASE_PASSWORD is not set");
    let host = env::var("DATABASE_HOST").unwrap_or("localhost".to_string());
    let name = env::var("DATABASE_NAME").expect("DATABASE_NAME is not set");
    let port = env::var("DATABASE_PORT").unwrap_or("5432".to_string());
    let url = format!("postgres://{}:{}@{}:{}/{}", username, password, host, port, name);
    let manager = ConnectionManager::<PgConnection>::new(url);
    Pool::builder().test_on_check_out(true).build(manager).expect("Failed to create db pool")
  };
}

pub fn init() {
  lazy_static::initialize(&POOL);
  let mut pool: DbConnection = connection().expect("Failed to get db connection");
  match pool.run_pending_migrations(MIGRATIONS) {
    Ok(_) => info!("Database initialized"),
    Err(err) => error!("Failed to initialize database; {}", err)
  };
}

pub fn connection() -> Result<DbConnection, ServiceError> {
  POOL.get()
    .map_err(|e| ServiceError::new(500, format!("Failed getting db connection: {}", e)))
}

pub fn load_data(data_dir_path: &str) {
  spells::load_data(data_dir_path);
}
