use v6.d;
use Test;
use RakuDoc::Render;

plan 17;

my $ast;
my $rv;
my RakuDoc::Processor $rdp .= new( :test );
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.rendered-citations, / 'Bibliography' .+? 'rakudoc-evo-chapter' /, 'a citations list is rendered';
like $rv.rendered-citations, / 'NO SUCH CITATION ID' /, 'citations list contains unknown ref';
like $rv.body.Str, /'<markup-Q>' .+? 'contents' .+? '[1]' .+? '</markup-Q>' /, 'citation UK style';
like $rv.warnings[0], / 'Q markup' /, 'Q markup warning generated';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => { family => "Docsworthy", given => "Poderick" },
        editor       => { family => "Wally", given => "Larr" },
        issued       => { 'date-parts' => [[2025, 10]] },
        publisher    => "Camelid Press",
        page         => "22-89",
        ISBN         => "978-1-2345-6789-0",
    })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.rendered-citations, /
    '<item>' .+?
    'bullet' .+? '[1]' .+?
    'The Evolution of RakuDoc'
/, 'a citations list with items is rendered';
unlike $rv.rendered-citations, / 'NO SUCH CITATION ID' /, 'citations list has ref now';
nok $rv.warnings.elems, 'no warnings generated';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =document :citation-style<acm-siggraph> :auto-citations<General Reading>
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => [{ family => "Docsworthy", given => "Poderick" }],
        editor       => [{ family => "Wally", given => "Larr" }],
        issued       => { 'date-parts' => [[[2025], [10]]] },
        publisher    => "Camelid Press",
        page         => "22-89",
        ISBN         => "978-1-2345-6789-0",
    })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.rendered-citations, /
    'General Reading' .+?
    'The Evolution of RakuDoc'
/, 'autocitations with new caption, and list has correct ref';
unlike $rv.rendered-citations, / '<item>' /, 'No items in citations for this style';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =document :!auto-citations
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => [{ family => "Docsworthy", given => "Poderick" }],
        editor       => [{ family => "Wally", given => "Larr" }],
        issued       => { 'date-parts' => [[[2025], [10]]] },
        publisher    => "Camelid Press",
        page         => "22-89",
        ISBN         => "978-1-2345-6789-0",
    })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
is $rv.rendered-citations, '', 'citations turned off';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    =citation XYZXXXX
        AND MORE USELESS STUFF

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.warnings[0], / 'Could not understand' /, 'bad citation format';
# put Raku error at publisher ; not , creates valid Raku but invalid citation
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => { family => "Docsworthy", given => "Poderick" },
        editor       => { family => "Wally", given => "Larr" },
        issued       => { 'date-parts' => [[2025, 10]] },
        publisher    => "Camelid Press",;
        page         => "22-89",
        ISBN         => "978-1-2345-6789-0",
    })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.warnings[0], / 'Valid Raku, invalid CSL-Raku' /, 'valid Raku, bad CSL-Raku';
# put Raku error in citation
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => { family => "Docsworthy", given => "Poderick" },
        editor       => { family => "Wally", given => "Larr" },
        issued       => { 'date-parts' => [[2025, 10]] },
        publisher    => "Camelid Press",
        page         => "22-89",
        ISBN         =>

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.warnings[0], / 'Invalid CSL-Raku' /, 'bad CSL-Raku';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => { family => "Docsworthy", given => "Poderick" },
        editor       => { family => "Wally", given => "Larr" },
        issued       => { 'date-parts' => [[2025, 10]] },
        publisher    => "Camelid Press",
        page         => { 29 }
        })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.warnings[0], / 'Citation data' .+ 'error' /, 'json error in test citation';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<rakudoc-evo-chapter> at some length

    =citation ({
        id           => "rakudoc-evo-chapter",
        type         => "chapter",
        title        => "The Evolution of RakuDoc: From Pod6 to Modern Documentation Standards",
        'container-title' => "The Raku Renaissance: A Comprehensive Guide to the 6.e Ecosystem",
        author       => { family => "Docsworthy", given => "Poderick" },
        editor       => { family => "Wally", given => "Larr" },
        issued       => { 'date-parts' => [[2025, 10]] },
        publisher    => "Camelid Press",
        page         => { 'start' => 29 }
        })
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.warnings[0], / 'With id' .+ 'Error' /, 'citation data error';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head Intro

    We have now explored Q<Generic2026> at some length

    =comment This citation in PubMed/Medline XML format
    =citation
       <PubmedArticleSet>
         <PubmedArticle>
           <MedlineCitation Status="Publisher">
             <PMID Version="1">98765432</PMID>
             <Article PubModel="Print">
               <Journal>
                 <ISSN IssnType="Print">1234-567X</ISSN>
                 <JournalIssue>
                   <Volume>14</Volume>
                   <Issue>1</Issue>
                   <PubDate>
                     <Year>2026</Year>
                     <Month>Jan</Month>
                     <Day>30</Day>
                   </PubDate>
                 </JournalIssue>
                 <Title>International Journal of Perl and Raku</Title>
               </Journal>
               <ArticleTitle>Concurrent Processing in RakuDoc: A Performance Analysis.</ArticleTitle>
               <Pagination>
                 <MedlinePgn>102-115</MedlinePgn>
               </Pagination>
               <AuthorList CompleteYN="Y">
                 <Author ValidYN="Y">
                   <LastName>Generic</LastName>
                   <ForeName>A</ForeName>
                 </Author>
                 <Author ValidYN="Y">
                   <LastName>Researcher</LastName>
                   <ForeName>B</ForeName>
                 </Author>
               </AuthorList>
               <Language>eng</Language>
               <PublicationTypeList>
                 <PublicationType>Journal Article</PublicationType>
               </PublicationTypeList>
             </Article>
           </MedlineCitation>
         </PubmedArticle>
       </PubmedArticleSet>


    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.rendered-citations, / 'Concurrent processing in RakuDoc' /, 'got Citation text using Bibtools dependency';

my $internet = run <ping -c 2 8.8.8.8>, :merge;
subtest 'live internet tests' => sub {
    plan :skip-all<no internet connection>
        if $internet.exitcode;
    plan 2;
    $ast = Q:to/QAST/.AST;
        =begin rakudoc
        =head Intro

        We have now explored Q<RakudoWay2021> at some length

        =for citation  :load< https://raw.githubusercontent.com/finanalyst/rakuast-rakudoc-render/main/xt/test-files/citations.json >

        =end rakudoc
        QAST
        $rv = $rdp.render( $ast, :pre-finalised );
        like $rv.rendered-citations, / 'Rakudo: The Way of the Camel-less' /, 'Bibliography from internet';
        nok $rv.warnings.elems, 'no warnings';
    }

done-testing