use robson_plugin_implementation_workflow::{
    execute_repo_list, execute_task_list, workflow_connect_and_migrate, Repo, Task,
};

// ── migrate-workflow: tables exist after migration ────────────────────────────

#[tokio::test]
async fn test_workflow_migrate_creates_tasks_table() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();
    let tasks = Task::find_all_tasks(&db).await.unwrap();
    assert!(
        tasks.is_empty(),
        "tasks table should exist and be empty after migration"
    );
}

#[tokio::test]
async fn test_workflow_migrate_creates_repos_table() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();
    let repos = Repo::find_all(&db).await.unwrap();
    assert!(
        repos.is_empty(),
        "repos table should exist and be empty after migration"
    );
}

// ── migrate-core: core tables exist after migration ───────────────────────────

#[tokio::test]
async fn test_core_migrate_creates_conversations_table() {
    let db = robson_core::connect_and_migrate(":memory:").await.unwrap();
    // conversations entity exists — query it without error
    use robson_core::conversation::Entity as Conversation;
    use sea_orm::EntityTrait;
    let rows = Conversation::find().all(&db).await.unwrap();
    assert!(rows.is_empty());
}

#[tokio::test]
async fn test_core_migrate_creates_allowed_channels_table() {
    let db = robson_core::connect_and_migrate(":memory:").await.unwrap();
    use robson_core::allowed_channel::Entity as AllowedChannel;
    use sea_orm::EntityTrait;
    let rows = AllowedChannel::find().all(&db).await.unwrap();
    assert!(rows.is_empty());
}

// ── execute_task_list ─────────────────────────────────────────────────────────

#[tokio::test]
async fn test_execute_task_list_empty() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();
    let result = execute_task_list(&db).await.unwrap();
    assert!(
        result.contains("KEY"),
        "output should contain table header KEY"
    );
    assert!(
        result.contains("STATUS"),
        "output should contain table header STATUS"
    );
}

#[tokio::test]
async fn test_execute_task_list_shows_inserted_task() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();

    Repo::insert_new(
        &db,
        "git://github.com/org/repo",
        "github",
        "main",
        None,
        None,
        "token",
    )
    .await
    .unwrap();

    Task::upsert_with_repo(
        &db,
        "PROJ-1",
        "Fix login bug",
        "Details here",
        "git://github.com/org/repo",
        None,
    )
    .await
    .unwrap();

    let result = execute_task_list(&db).await.unwrap();
    assert!(
        result.contains("PROJ-1"),
        "output should contain the task key"
    );
    assert!(
        result.contains("Fix login bug"),
        "output should contain the task summary"
    );
}

// ── execute_repo_list ─────────────────────────────────────────────────────────

#[tokio::test]
async fn test_execute_repo_list_empty() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();
    let result = execute_repo_list(&db).await.unwrap();
    assert!(
        result.contains("SLUG"),
        "output should contain table header SLUG"
    );
    assert!(
        result.contains("PROVIDER"),
        "output should contain table header PROVIDER"
    );
}

#[tokio::test]
async fn test_execute_repo_list_shows_inserted_repo() {
    let db = workflow_connect_and_migrate(":memory:").await.unwrap();

    Repo::insert_new(
        &db,
        "git://github.com/org/myrepo",
        "github",
        "main",
        None,
        None,
        "token",
    )
    .await
    .unwrap();

    let result = execute_repo_list(&db).await.unwrap();
    assert!(
        result.contains("git://github.com/org/myrepo"),
        "output should contain the repo slug"
    );
    assert!(
        result.contains("github"),
        "output should contain the provider"
    );
}
