use lore::gitlab::transformers::discussion::transform_mr_discussion;
use lore::gitlab::types::{GitLabAuthor, GitLabDiscussion, GitLabNote};

fn make_author() -> GitLabAuthor {
    GitLabAuthor {
        id: 1,
        username: "testuser".to_string(),
        name: "Test User".to_string(),
    }
}

fn make_basic_note(id: i64, created_at: &str) -> GitLabNote {
    GitLabNote {
        id,
        note_type: Some("DiscussionNote".to_string()),
        body: format!("Note {}", id),
        author: make_author(),
        created_at: created_at.to_string(),
        updated_at: created_at.to_string(),
        system: false,
        resolvable: false,
        resolved: false,
        resolved_by: None,
        resolved_at: None,
        position: None,
    }
}

fn make_discussion(notes: Vec<GitLabNote>) -> GitLabDiscussion {
    GitLabDiscussion {
        id: "abc123def456".to_string(),
        individual_note: false,
        notes,
    }
}

#[test]
fn transform_mr_discussion_sets_merge_request_id() {
    let note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    let discussion = make_discussion(vec![note]);

    let result = transform_mr_discussion(&discussion, 100, 42);

    assert_eq!(result.merge_request_id, Some(42));
    assert_eq!(result.issue_id, None);
    assert_eq!(result.noteable_type, "MergeRequest");
}

#[test]
fn transform_mr_discussion_preserves_project_id() {
    let note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    let discussion = make_discussion(vec![note]);

    let result = transform_mr_discussion(&discussion, 200, 42);

    assert_eq!(result.project_id, 200);
}

#[test]
fn transform_mr_discussion_preserves_discussion_id() {
    let note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    let discussion = make_discussion(vec![note]);

    let result = transform_mr_discussion(&discussion, 100, 42);

    assert_eq!(result.gitlab_discussion_id, "abc123def456");
}

#[test]
fn transform_mr_discussion_computes_resolvable_from_notes() {
    let mut note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    note.resolvable = true;
    let discussion = make_discussion(vec![note]);

    let result = transform_mr_discussion(&discussion, 100, 42);

    assert!(result.resolvable);
    assert!(!result.resolved);
}

#[test]
fn transform_mr_discussion_computes_resolved_when_all_resolved() {
    let mut note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    note.resolvable = true;
    note.resolved = true;
    let discussion = make_discussion(vec![note]);

    let result = transform_mr_discussion(&discussion, 100, 42);

    assert!(result.resolvable);
    assert!(result.resolved);
}

#[test]
fn transform_mr_discussion_handles_individual_note() {
    let note = make_basic_note(1, "2024-01-16T09:00:00.000Z");
    let mut discussion = make_discussion(vec![note]);
    discussion.individual_note = true;

    let result = transform_mr_discussion(&discussion, 100, 42);

    assert!(result.individual_note);
}
