#!/usr/bin/env perl

# Copyright (c) 2025, R. W. Rodolico
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package fixture_report_helper;

use strict;
use warnings;
use YAML::Tiny;

#######################################################
#
# slurpFile( $path )
#
# Reads one text file into a scalar.
#
# Parameters:
#   $path - fixture path
#
# Returns:
#   scalar content
#
# Side Effects:
#   file IO
#
#######################################################
sub slurpFile {
   my $path = shift;
   return '' unless defined($path) && -f $path;
   open my $fh, '<', $path or return '';
   my $txt = do { local $/; <$fh> };
   close($fh);
   return defined($txt) ? $txt : '';
}

#######################################################
#
# applyTabLines( $report, $lines )
#
# Applies sensor tab lines into report hash structure.
#
# Parameters:
#   $report - hash reference to mutate
#   $lines  - array reference of tab lines
#
# Returns:
#   none
#
# Side Effects:
#   mutates $report
#
#######################################################
sub applyTabLines {
   my ($report, $lines) = @_;
   return unless ref($report) eq 'HASH' && ref($lines) eq 'ARRAY';

   foreach my $line (@{$lines}) {
      next unless defined($line) && $line =~ /\S/;
      my @parts = split(/\t/, $line);
      next if @parts < 2;
      my $value = pop @parts;
      my $cursor = $report;
      for (my $i = 0; $i < @parts; $i++) {
         my $key = $parts[$i];
         next if !defined($key) || $key eq '';
         if ($i == $#parts) {
            $cursor->{$key} = $value;
         } else {
            $cursor->{$key} = {} if !exists($cursor->{$key}) || ref($cursor->{$key}) ne 'HASH';
            $cursor = $cursor->{$key};
         }
      }
   }
}

#######################################################
#
# loadFixtureReport( $fixtureDir )
#
# Loads the v3.7.1 reference report for one fixture role.
#
# Parameters:
#   $fixtureDir - fixture role directory path
#
# Returns:
#   hash reference
#
# Side Effects:
#   file IO
#
#######################################################
sub loadFixtureReport {
   my $fixtureDir = shift;
   my $path = "$fixtureDir/v3.7.1-report.yaml";
   my $yaml = YAML::Tiny->read($path);
   return {} unless $yaml && ref($yaml->[0]) eq 'HASH';
   return $yaml->[0];
}

#######################################################
#
# writeReport( $path, $report )
#
# Writes report hash to YAML::Tiny output.
#
# Parameters:
#   $path   - destination YAML path
#   $report - hash reference
#
# Returns:
#   boolean
#
# Side Effects:
#   file IO
#
#######################################################
sub writeReport {
   my ($path, $report) = @_;
   return 0 unless defined($path) && ref($report) eq 'HASH';
   my $yaml = YAML::Tiny->new($report);
   return $yaml->write($path) ? 1 : 0;
}

1;
