#!/bin/bash

# Visual timeline chart for missing number sequences
# Usage: ./timeline_chart_visual.sh numbers.txt [width]

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

file="$1"
chart_width="${2:-80}"  # Default width 80 characters

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

awk -v width="$chart_width" '
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 "╔═══════════════════════════════════════════════════════════════════════════════╗"
    print "║                           TIMELINE CHART                                      ║"
    print "╠═══════════════════════════════════════════════════════════════════════════════╣"
    printf "║ Range: %-4d to %-4d  (Total: %d positions)%*s║\n", first, last, total_range, 34, ""
    print "╚═══════════════════════════════════════════════════════════════════════════════╝"
    print ""

    # Create visual timeline in rows
    chars_per_row = width
    rows = int((time + chars_per_row - 1) / chars_per_row)

    for (row = 0; row < rows; row++) {
        start_idx = row * chars_per_row
        end_idx = start_idx + chars_per_row
        if (end_idx > time) end_idx = time

        # Print time labels
        printf "Time %4d-%4d: ", start_idx, end_idx - 1

        # Print visual bar
        for (i = start_idx; i < end_idx; i++) {
            if (status[i] == "FOUND") {
                printf "█"
            } else {
                printf "░"
            }
        }
        print ""
    }

    print ""
    print "Legend: █ = Found   ░ = Missing"
    print ""

    # Print detailed missing ranges
    print "Missing Ranges:"
    in_gap = 0
    for (i = 0; i < time; i++) {
        if (status[i] == "MISSING") {
            if (!in_gap) {
                gap_start = timeline[i]
                gap_start_time = i
                in_gap = 1
            }
            gap_end = timeline[i]
            gap_end_time = i
        } else {
            if (in_gap) {
                if (gap_start == gap_end) {
                    printf "  Time %4d: Number %d\n", gap_start_time, gap_start
                } else {
                    printf "  Time %4d-%4d: Numbers %d-%d (%d missing)\n",
                           gap_start_time, gap_end_time, gap_start, gap_end, gap_end - gap_start + 1
                }
                in_gap = 0
            }
        }
    }
    if (in_gap) {
        if (gap_start == gap_end) {
            printf "  Time %4d: Number %d\n", gap_start_time, gap_start
        } else {
            printf "  Time %4d-%4d: Numbers %d-%d (%d missing)\n",
                   gap_start_time, gap_end_time, gap_start, gap_end, gap_end - gap_start + 1
        }
    }

    print ""
    print "╔═══════════════════════════════════════════════════════════════════════════════╗"
    print "║                              STATISTICS                                       ║"
    print "╠═══════════════════════════════════════════════════════════════════════════════╣"
    printf "║  Total timeline positions: %-6d%*s║\n", time, 46, ""
    printf "║  Numbers found:            %-6d (%.2f%%)%*s║\n", found_count, (found_count / total_range) * 100, 33, ""
    printf "║  Numbers missing:          %-6d (%.2f%%)%*s║\n", missing_count, (missing_count / total_range) * 100, 33, ""
    print "╚═══════════════════════════════════════════════════════════════════════════════╝"
}
' "$file"
