#
#===============================================================================
#         FILE: 005_pod_content.t
#  DESCRIPTION: POD content tests for Sys::OsRelease
#       AUTHOR: Ian Kluft (IKLUFT)
#      VERSION: 1.0
#      CREATED: 08/19/2026 10:32:01 PM
#===============================================================================

## no critic (Modules::RequireExplicitPackage)
use strict;
use warnings;
use autodie;
use Carp qw(croak);
use Pod::Simple::SimpleTree;
use Sys::OsRelease;

use Test::More;                      # last test to print

# configuration constants
my $MODULE = "Sys::OsRelease";  # do not optimize: source code is filtered in ...::Lite to modify module name
my $FILENAME = 'lib/' . join( '/', split( '::', $MODULE )) . '.pm';
my $SEARCH_HEAD2 = 'The os-release Standard';
my $SEARCH_TEXT = 'Current attributes recognized by ';
my $TEXTLEN = length( $SEARCH_TEXT );
my $AUTOGEN_HEAD2 = 'Auto-generated Accessor Methods';

# globals
my $POD_TREE = Pod::Simple::SimpleTree->new->parse_file( $FILENAME );

# scan the os-release standard section's text for the line with the list of supported attributes
sub scan_std_section
{
    my $root = shift;
    my $i = shift;
    my $offset = 1;
    while (( exists $root->[$i + $offset]) and substr( $root->[$i + $offset][0], 0, 4 ) ne 'head' ) {
        if ( $root->[$i + $offset][0] eq 'Para' ) {
            foreach my $str ( @{$root->[$i + $offset]} ) {
                next if ref $str;
                if ( substr( $str, 0, $TEXTLEN ) eq $SEARCH_TEXT ) {
                    # found the paragraph with the attribute names
                    # skip initial text up to word ending with a colon, then return a hash of keywords found
                    $str =~ s/^ .* : \s//x;
                    my %words = map { $_ => 1 } split ' ', $str;
                    return \%words;
                }
            }
        }
        $offset++;
    }
    return;
}

# extract supported attributes as a hash
# this requires intricate digging into the POD tree data
sub get_attrs_from_list
{
    my $root = $POD_TREE->root();
    for ( my $i=0; $i < scalar @$root; $i++ ) {
        if ( ref $root->[$i] eq "ARRAY" ) {
            if ( $root->[$i][0] eq 'head2' and $root->[$i][2] eq $SEARCH_HEAD2 ) {
                my $value = scan_std_section( $root, $i );
                if ( defined $value ) {
                    return $value;
                }
                last;
            }
        }
    }
    return;
}

# scan the autogenerated method section for item text
sub scan_autogen_section
{
    my $root = shift;
    my $i = shift;
    my $offset = 1;
    while (( exists $root->[$i + $offset]) and substr( $root->[$i + $offset][0], 0, 4 ) ne 'head' ) {
        if ( $root->[$i + $offset][0] eq 'over-text' ) {
            my %methods;
            foreach my $entry ( @{$root->[$i + $offset]} ) {
                if ( ref $entry eq "ARRAY" and $entry->[0] eq 'item-text' and $entry->[2] =~ / ^ ( \w+ ) \(\) /x ) {
                    $methods{uc $1} = 1;
                }
            }
            return \%methods;
        }
        $offset++;
    }
    return;
}

# extract generated attribute accessor methods as a hash
# this requires intricate digging into the POD tree data
sub get_gen_method_items
{
    my $root = $POD_TREE->root();
    for ( my $i=0; $i < scalar @$root; $i++ ) {
        if ( ref $root->[$i] eq "ARRAY" ) {
            if ( $root->[$i][0] eq 'head2' and $root->[$i][2] eq $AUTOGEN_HEAD2 ) {
                my $value = scan_autogen_section( $root, $i );
                if ( defined $value ) {
                    return $value;
                }
                last;
            }
        }
    }
    return;
}

# test mainline
my @std_attrs = Sys::OsRelease::std_attrs();
plan tests => ( scalar @std_attrs ) * 2;

# check that each attribute used occurs in the POD documentation
# POD scanning borrowed from Test::Pod::Content, didn't use it because it would re-open file for each attr
if ( ref $POD_TREE ne "Pod::Simple::SimpleTree" ) {
    croak "parse result expected Pod::Simple::SimpleTree ref, got " . ( ref $POD_TREE );
}
my $attrs_in_list = get_attrs_from_list();
my $method_items = get_gen_method_items();
foreach my $attr_name ( @std_attrs ) {
    ok exists $attrs_in_list->{ $attr_name }, "found $attr_name in POD attribute list";
    ok exists $method_items->{ $attr_name }, "found $attr_name in POD autogen method item";
}
1;
