#!/usr/bin/env bash
# Lists all .jsonl files as: DATE SIZE PATH
# Uses creation date if available, else modification date.
# Usage:
#   ./list_jsonl_paths.sh --path /your/input/dir
# or
#   ./list_jsonl_paths.sh               # will use the default INPUT_DIR

set -e

# ============================================================
# DEFAULT VARIABLES
# ============================================================
INPUT_DIR="/caiextensions/caiextensions-memory"
OUTPUT_FILE="logs.txt"

# ============================================================
# Parse arguments
# ============================================================
while [[ "$#" -gt 0 ]]; do
  case "$1" in
    --path)
      INPUT_DIR="$2"
      shift 2
      ;;
    *)
      echo "❌ Unknown option: $1"
      echo "Usage: $0 [--path <input_directory>]"
      exit 1
      ;;
  esac
done

# ============================================================
# Ensure output directory exists
# ============================================================
mkdir -p "$(dirname "$OUTPUT_FILE")"

# ============================================================
# Build the list
# ============================================================
# We emit: <date> <size> <path>
# date: creation (%w) if present, else modification (%y)
: > "$OUTPUT_FILE"  # truncate

echo "📂 Input directory: $INPUT_DIR"
echo "📝 Output file:     $OUTPUT_FILE"
echo

find "$INPUT_DIR" -type f -name "*.jsonl" -print0 |
while IFS= read -r -d '' file; do
  # Get birth (%w), mod (%y), size (%s), path (%n)
  IFS='|' read -r birth mod size path < <(stat -c '%w|%y|%s|%n' "$file")
  date_field="$birth"
  # If birth time unavailable ('-' or empty), use modification time
  if [[ -z "$date_field" || "$date_field" == "-" ]]; then
    date_field="$mod"
  fi
  printf '%s %s %s\n' "$date_field" "$size" "$path" >> "$OUTPUT_FILE"
done

# Sort (optional): by date then size
sort -o "$OUTPUT_FILE" "$OUTPUT_FILE"

echo "✅ Wrote $(wc -l < "$OUTPUT_FILE" | tr -d ' ') entries to $OUTPUT_FILE"
