#!/bin/bash

# 查看歸檔文件和日誌的快速工具 (適配扁平化結構)

echo "🗃️ 文件歸檔系統狀態"
echo "=========================="

# 設定相對路徑指向 sop/archive
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ARCHIVE_BASE="$SCRIPT_DIR/../archive"

# 查看今日歸檔 (扁平化結構)
TODAY=$(date '+%Y%m%d')
TODAY_FILES=$(ls -la "$ARCHIVE_BASE/" 2>/dev/null | grep "^-.*$TODAY" || true)
if [[ -n "$TODAY_FILES" ]]; then
    echo -e "\n📦 今日歸檔文件 ($TODAY):"
    echo "$TODAY_FILES"
else
    echo -e "\n📦 今日無歸檔文件"
fi

# 查看所有歸檔文件 (扁平化)
echo -e "\n📅 所有歸檔文件:"
if [[ -d "$ARCHIVE_BASE" ]]; then
    ls -la "$ARCHIVE_BASE/" | grep -v "^d.*logs$" | grep "^-" || echo "無歸檔文件"
else
    echo "無歸檔目錄"
fi

# 查看遷移日誌
echo -e "\n📋 文件遷移日誌:"
if [[ -f "$ARCHIVE_BASE/logs/file_migration.log" ]]; then
    cat "$ARCHIVE_BASE/logs/file_migration.log"
else
    echo "尚無遷移日誌"
fi

# 統計資訊
echo -e "\n📊 歸檔統計:"
TOTAL_FILES=$(find "$ARCHIVE_BASE/" -type f 2>/dev/null | wc -l)
echo "總歸檔文件數: $TOTAL_FILES"

LOG_FILES=$(ls -1 "$ARCHIVE_BASE/logs/" 2>/dev/null | wc -l)  
echo "日誌文件數: $LOG_FILES"