#!/bin/bash
# ==============================================================================================
# ============================= Sunday Morning Coffee Report ===================================
# ==============================================================================================
# Weekly monitoring orchestrator — runs all Sunday monitor scripts in sequence.
# Designed to be read over coffee Sunday morning while the system is fully caught up
# from the 2:30am maintenance window.
# Schedule: 0 7 * * 0 (7am Sunday — after weekly_sync_maintenance.sh finishes at ~3am)
#
# ── SCRIPTS (master.conf COFFEE_REPORT_SCRIPTS) ───────────────────────────────────────────────
#   Each script runs independently, logs to its own output, and notifies on findings.
#   All scripts receive --dry-run and --log flags from this orchestrator when set.
#
# ── HOST AWARENESS ────────────────────────────────────────────────────────────────────────────
#   detect_hosts() sets MY_ID — used in banner and summary.
#   HOST1 primary: runs all scripts. HOST2: limited to host-aware scripts only.
#
# ── USAGE ─────────────────────────────────────────────────────────────────────────────────────
#   sunday_morning_coffee_report.sh              — normal run
#   sunday_morning_coffee_report.sh --dry-run    — preview without any writes or notifications
#   sunday_morning_coffee_report.sh --log        — verbose per-script output
#   sunday_morning_coffee_report.sh --status     — show configured scripts and exit
# ==============================================================================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

source "$SCRIPT_DIR/../load_config.sh"

SCRIPTS_ROOT="$SCRIPT_DIR/.."

parse_args "$@"
detect_hosts

# ==============================================================================================
# ━━━ Helpers ━━━
# ==============================================================================================
JOB_PASS=()
JOB_FAIL=()

run_job() {
    local script_entry="$1"
    local extra_dry=""
    local extra_log=""
    [[ "$DRY_RUN"        == true ]] && extra_dry="--dry-run"
    [[ "$ENABLE_LOGGING" == true ]] && extra_log="--log"

    read -r -a script_args <<< "$script_entry"
    local script_path="$SCRIPTS_ROOT/${script_args[0]}"
    local script_name
    script_name=$(basename "${script_args[0]}")
    local extra_args=("${script_args[@]:1}")

    if [[ ! -f "$script_path" ]]; then
        error "$script_name — not found at $script_path"
        JOB_FAIL+=("$script_name")
        return 1
    fi

    log "Running: $script_name ${extra_args[*]}"
    if bash "$script_path" "${extra_args[@]}" $extra_dry $extra_log; then
        echo "$script_name — done ✅"
        JOB_PASS+=("$script_name")
    else
        error "$script_name — failed (exit $?)"
        JOB_FAIL+=("$script_name")
    fi
}

# ==============================================================================================
# ━━━ Status ━━━
# ==============================================================================================
if [[ "$SHOW_STATUS" == true ]]; then
    echo "━━━ Sunday Morning Coffee Report — Status ━━━"
    echo "  Host:     $MY_ID"
    echo "  Scripts:"
    if [[ ${#COFFEE_REPORT_SCRIPTS[@]} -eq 0 ]]; then
        echo "    None configured (COFFEE_REPORT_SCRIPTS in master.conf)"
    else
        for entry in "${COFFEE_REPORT_SCRIPTS[@]}"; do
            [[ -n "$entry" ]] && echo "    ☕ ${entry##*/}"
        done
    fi
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    exit 0
fi

# ==============================================================================================
# ━━━ Main ━━━
# ==============================================================================================
_unraid_ver=$(platform_get_os_version 2>/dev/null || echo "unknown")
_uptime_s=$(awk '{print int($1)}' /proc/uptime 2>/dev/null || echo 0)
_container_count=$(docker ps -q 2>/dev/null | wc -l || echo 0)
_rootfs_pct=$(df / --output=pcent 2>/dev/null | tail -1 | tr -d ' %')

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "☕  Sunday Morning Coffee Report — $MY_ID"
[[ "$DRY_RUN" == true ]] && echo "    [DRY RUN]"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log "$ICON_HOST Server:     $MY_ID ($LOCAL_SERVER_NAME) — unRAID $_unraid_ver"
log "$ICON_TIME Uptime:     $(format_duration $_uptime_s)"
log "$ICON_CONTAINERS Docker: $_container_count container(s) running"
log "$ICON_HEALTH Rootfs:   ${_rootfs_pct:-?}% used"
log "$ICON_GEAR Scripts:    ${#COFFEE_REPORT_SCRIPTS[@]} configured"

if [[ ${#COFFEE_REPORT_SCRIPTS[@]} -eq 0 ]]; then
    warn "No scripts configured — add entries to COFFEE_REPORT_SCRIPTS in master.conf"
    exit 0
fi

for script_entry in "${COFFEE_REPORT_SCRIPTS[@]}"; do
    [[ -z "$script_entry" ]] && continue
    echo ""
    run_job "$script_entry"
done

# ==============================================================================================
# ━━━ Summary ━━━
# ==============================================================================================
echo ""
echo "━━━━━ ☕ COFFEE REPORT SUMMARY ━━━━━"
echo "🖥️  Host:    $MY_ID"
echo "⏱️  Done:    $(date '+%Y-%m-%d %H:%M:%S')"

if [[ ${#JOB_PASS[@]} -gt 0 ]]; then
    echo "✅  Passed:  ${JOB_PASS[*]}"
fi
if [[ ${#JOB_FAIL[@]} -gt 0 ]]; then
    echo "❌  Failed:  ${JOB_FAIL[*]}"
fi

[[ ${#JOB_FAIL[@]} -gt 0 ]] && exit 1
exit 0
