use aws_local_core::db::DbPool;
use std::env;

use crate::db;

#[derive(Clone)]
pub struct AppState {
    pub db: DbPool,
    pub region: String,
}

pub fn new_state(data_dir: &str) -> AppState {
    let pool = db::connect(data_dir).expect("failed to open resources.db");
    db::run_migrations(&pool).expect("failed to run resources migrations");
    let region = env::var("AWS_REGION").unwrap_or_else(|_| "us-east-1".to_string());
    AppState { db: pool, region }
}
