#!/bin/bash

# Timeline chart script for visualizing missing number sequences
# Usage: ./timeline_chart.sh numbers.txt

if [ $# -eq 0 ]; then
    echo "Usage: $0 <file>"
    exit 1
fi

file="$1"

if [ ! -f "$file" ]; then
    echo "Error: File '$file' not found"
    exit 1
fi

awk '
BEGIN {
    found_count = 0
    missing_count = 0
}

NR == 1 {
    first = $1
    prev = $1
    timeline[0] = $1
    status[0] = "FOUND"
    time = 1
    next
}

{
    current = $1

    # Fill in any missing numbers between prev and current
    for (i = prev + 1; i < current; i++) {
        timeline[time] = i
        status[time] = "MISSING"
        missing_count++
        time++
    }

    # Add the current found number
    timeline[time] = current
    status[time] = "FOUND"
    found_count++
    time++

    prev = current
    last = current
}

END {
    if (NR == 0) {
        print "Error: No numbers found in file"
        exit 1
    }

    found_count++  # Include the first number
    total_range = last - first + 1

    print "=== TIMELINE CHART ==="
    print "Range:", first, "to", last, "(" total_range, "total positions)"
    print ""

    # Print timeline
    for (i = 0; i < time; i++) {
        num = timeline[i]
        stat = status[i]

        if (stat == "FOUND") {
            marker = "●"
            bar = "█"
        } else {
            marker = "○"
            bar = "░"
        }

        printf "Time %4d: [%s] %4d %s\n", i, marker, num, bar
    }

    print ""
    print "=== LEGEND ==="
    print "  ● = Found    █ = Found"
    print "  ○ = Missing  ░ = Missing"
    print ""
    print "=== STATISTICS ==="
    print "  Total timeline positions:", time
    print "  Numbers found:", found_count, sprintf("(%.2f%%)", (found_count / total_range) * 100)
    print "  Numbers missing:", missing_count, sprintf("(%.2f%%)", (missing_count / total_range) * 100)
}
' "$file"
