#!/usr/bin/env perl

# Test to verify the fix for parseSnapshotDateTime seconds parsing bug
# Previously, HH.MM.SS was being converted to HH:MM.SS instead of HH:MM:SS

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/..";
use ZFS_Utils qw(parseSnapshotDateTime);

print "=" x 70 . "\n";
print "Testing parseSnapshotDateTime - Seconds Parsing Fix\n";
print "=" x 70 . "\n\n";

# Test cases specifically for the seconds bug
my @test_cases = (
    # [input, expected_description, should_parse]
    ['2025-12-21_10.30.45', 'HH.MM.SS with dot separators (all three components)', 1],
    ['2025-12-21_10.30', 'HH.MM with dot separators (no seconds)', 1],
    ['2025-12-21_10:30:45', 'HH:MM:SS with colon separators', 1],
    ['2025-12-21_10:30', 'HH:MM with colon separators (no seconds)', 1],
    ['pool/data@2025-12-21_14.15.30', 'Full snapshot name with HH.MM.SS', 1],
    ['daily_2025-12-21_09.45.20_backup', 'Snapshot with prefix/suffix and HH.MM.SS', 1],
);

my $passed = 0;
my $failed = 0;

foreach my $test (@test_cases) {
    my ($input, $description, $should_parse) = @$test;
    
    print "Test: $description\n";
    print "  Input: '$input'\n";
    
    my $epoch = parseSnapshotDateTime($input);
    
    if ($should_parse) {
        if (defined $epoch && $epoch > 0) {
            # Convert epoch back to check if seconds are preserved
            my @time = gmtime($epoch);
            my $formatted = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
                $time[5] + 1900, $time[4] + 1, $time[3],
                $time[2], $time[1], $time[0]);
            
            print "  Result: PASS - Epoch: $epoch\n";
            print "  Parsed as: $formatted (UTC)\n";
            
            # Check if seconds component matches expected (only for formats with seconds)
            if ($input =~ /[:\.](\d{2})[:\.](\d{2})$/) {
                # Format with seconds: HH:MM:SS or HH.MM.SS
                my $expected_seconds = $2;
                if ($time[0] == $expected_seconds) {
                    print "  ✓ Seconds preserved correctly: $expected_seconds\n";
                } else {
                    print "  ✗ WARNING: Seconds mismatch - expected $expected_seconds, got $time[0]\n";
                    $failed++;
                    next;
                }
            } elsif ($input =~ /[:\.](\d{2})$/) {
                # Format without seconds: HH:MM or HH.MM - should default to 00
                if ($time[0] == 0) {
                    print "  ✓ No seconds in format, correctly defaulted to 00\n";
                } else {
                    print "  ✗ WARNING: Expected seconds to be 00, got $time[0]\n";
                    $failed++;
                    next;
                }
            }
            
            $passed++;
        } else {
            print "  Result: FAIL - Should have parsed but returned: " . (defined $epoch ? $epoch : "undef") . "\n";
            $failed++;
        }
    } else {
        if (!defined $epoch) {
            print "  Result: PASS - Correctly returned undef\n";
            $passed++;
        } else {
            print "  Result: FAIL - Should not have parsed but got: $epoch\n";
            $failed++;
        }
    }
    print "\n";
}

print "=" x 70 . "\n";
print "Test Summary:\n";
print "  Total tests: " . ($passed + $failed) . "\n";
print "  Passed: $passed\n";
print "  Failed: $failed\n";
print "=" x 70 . "\n";

if ($failed == 0) {
    print "✓ ALL TESTS PASSED - Seconds parsing fix verified!\n";
    exit 0;
} else {
    print "✗ SOME TESTS FAILED\n";
    exit 1;
}
