use std::path::Path;

use rusqlite::Connection;

use crate::core::config::{
    Config, EmbeddingConfig, GitLabConfig, LoggingConfig, ProjectConfig, ScoringConfig,
    StorageConfig, SyncConfig,
};
use crate::core::db::{create_connection, run_migrations};

pub fn setup_test_db() -> Connection {
    let conn = create_connection(Path::new(":memory:")).unwrap();
    run_migrations(&conn).unwrap();
    conn
}

pub fn insert_project(conn: &Connection, id: i64, path: &str) {
    conn.execute(
        "INSERT INTO projects (id, gitlab_project_id, path_with_namespace, web_url)
         VALUES (?1, ?2, ?3, ?4)",
        rusqlite::params![
            id,
            id * 100,
            path,
            format!("https://git.example.com/{path}")
        ],
    )
    .unwrap();
}

pub fn test_config(default_project: Option<&str>) -> Config {
    Config {
        gitlab: GitLabConfig {
            base_url: "https://gitlab.example.com".to_string(),
            token_env_var: "GITLAB_TOKEN".to_string(),
            token: None,
            username: None,
        },
        projects: vec![ProjectConfig {
            path: "group/project".to_string(),
        }],
        default_project: default_project.map(String::from),
        sync: SyncConfig::default(),
        storage: StorageConfig::default(),
        embedding: EmbeddingConfig::default(),
        logging: LoggingConfig::default(),
        scoring: ScoringConfig::default(),
    }
}
