#!/bin/bash

# Timeline summary script for analyzing missing number sequences
# Usage: ./timeline_summary.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
    max_gap_size = 0
    gap_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
    if (current > prev + 1) {
        gap_size = current - prev - 1
        gap_count++

        if (gap_size > max_gap_size) {
            max_gap_size = gap_size
        }

        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
    percentage_found = (found_count / total_range) * 100
    percentage_lost = (missing_count / total_range) * 100

    print "╔═══════════════════════════════════════════════════════════════════════════════╗"
    print "║                        SEQUENCE ANALYSIS SUMMARY                              ║"
    print "╠═══════════════════════════════════════════════════════════════════════════════╣"
    printf "║  Range:                %-4d to %-6d%*s║\n", first, last, 42, ""
    printf "║  Total positions:      %-10d%*s║\n", total_range, 49, ""
    printf "║  Timeline events:      %-10d%*s║\n", time, 49, ""
    print "╠═══════════════════════════════════════════════════════════════════════════════╣"
    printf "║  Numbers FOUND:        %-10d (%.2f%%)%*s║\n", found_count, percentage_found, 35, ""
    printf "║  Numbers MISSING:      %-10d (%.2f%%)%*s║\n", missing_count, percentage_lost, 35, ""
    print "╠═══════════════════════════════════════════════════════════════════════════════╣"
    printf "║  Number of gaps:       %-10d%*s║\n", gap_count, 49, ""
    printf "║  Largest gap:          %-10d numbers%*s║\n", max_gap_size, 41, ""
    if (gap_count > 0) {
        printf "║  Average gap size:     %-10.2f numbers%*s║\n", missing_count / gap_count, 41, ""
    }
    print "╚═══════════════════════════════════════════════════════════════════════════════╝"
    print ""

    # Print detailed missing ranges (first 20 gaps)
    print "Missing Ranges (first 20 gaps):"
    print "────────────────────────────────────────────────────────────────────────────────"
    in_gap = 0
    gap_num = 0
    for (i = 0; i < time && gap_num < 20; 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) {
                gap_num++
                if (gap_start == gap_end) {
                    printf "%2d. Time %6d: Number %6d (1 missing)\n", gap_num, gap_start_time, gap_start
                } else {
                    printf "%2d. Time %6d-%6d: Numbers %6d-%-6d (%d missing)\n",
                           gap_num, gap_start_time, gap_end_time, gap_start, gap_end, gap_end - gap_start + 1
                }
                in_gap = 0
            }
        }
    }
    if (in_gap && gap_num < 20) {
        gap_num++
        if (gap_start == gap_end) {
            printf "%2d. Time %6d: Number %6d (1 missing)\n", gap_num, gap_start_time, gap_start
        } else {
            printf "%2d. Time %6d-%6d: Numbers %6d-%-6d (%d missing)\n",
                   gap_num, gap_start_time, gap_end_time, gap_start, gap_end, gap_end - gap_start + 1
        }
    }
    if (gap_count > 20) {
        printf "\n... and %d more gaps\n", gap_count - 20
    }
}
' "$file"
