use paper_gap::{arxiv::Paper, pwc};

fn paper(arxiv_id: Option<&str>) -> Paper {
    Paper {
        id: "http://arxiv.org/abs/2603.21852v1".into(),
        title: "Fast FFT Methods for PDE Solvers".into(),
        abstract_text: String::new(),
        authors: Vec::new(),
        published_date: None,
        updated_date: None,
        doi: None,
        arxiv_id: arxiv_id.map(str::to_string),
        url: "http://arxiv.org/abs/2603.21852v1".into(),
        categories: Vec::new(),
        extracted_methods: Vec::new(),
        source_provider: "arxiv".into(),
    }
}

#[test]
fn builds_paper_search_url_from_arxiv_id_without_version() {
    let url = pwc::paper_search_url(&paper(Some("2603.21852v1")));
    assert_eq!(
        url,
        "https://paperswithcode.com/api/v1/papers/?arxiv_id=2603.21852"
    );
}

#[test]
fn builds_paper_search_url_from_title_without_arxiv_id() {
    let url = pwc::paper_search_url(&paper(None));
    assert!(url.starts_with("https://paperswithcode.com/api/v1/papers/?q="));
    assert!(url.contains("Fast%20FFT%20Methods%20for%20PDE%20Solvers"));
}

#[test]
fn builds_repositories_url() {
    assert_eq!(
        pwc::paper_repositories_url("abc123"),
        "https://paperswithcode.com/api/v1/papers/abc123/repositories/"
    );
}

#[test]
fn parses_repository_links_and_skips_missing_urls() {
    let json = r#"{
        "count": 2,
        "next": null,
        "previous": null,
        "results": [
            {"url": "https://github.com/example/fft", "name": "example/fft", "is_official": true},
            {"url": null, "name": "broken", "is_official": false}
        ]
    }"#;
    let links = pwc::parse_repositories("paper-1", json).unwrap();
    assert_eq!(links.len(), 1);
    assert_eq!(links[0].source_provider, "papers-with-code");
    assert_eq!(links[0].paper_id.as_deref(), Some("paper-1"));
    assert_eq!(links[0].repository_url, "https://github.com/example/fft");
    assert_eq!(links[0].repository_name.as_deref(), Some("example/fft"));
    assert_eq!(links[0].is_official, Some(true));
}
