#!/bin/bash
# ==============================================================================================
# ================================= Radarr Cleanup =============================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Delete orphaned movie files not tracked by Radarr. Queries the API for all
# tracked movie file paths, walks the library on disk, and removes anything
# untracked that is old enough to be past the import window. Triggers an Emby
# library clean after each deletion run so ghost entries disappear immediately.
#
# ==============================================================================================
# OPERATIONAL MODEL
# ==============================================================================================
#
# Every file encountered on disk is classified into one of five categories:
#
#   TRACKED   — Radarr API knows this exact path → leave it alone
#   PROTECTED — matches RADARR_PROTECTED_PATTERNS → never delete
#   ORPHAN    — video file, not tracked, older than RADARR_ORPHAN_AGE → delete
#   JUNK      — not a video extension, not protected → delete regardless of age
#   RECENT    — not tracked, under RADARR_ORPHAN_AGE → skip (may be mid-import)
#
# Radarr generates movie artwork (*.jpg), metadata (*.nfo), and manages subtitles
# (*.srt, *.sub, *.ass) but does NOT include these in its tracked file API response.
# Without PROTECTED classification these would be deleted — breaking Radarr and
# Emby metadata display.
#
# After deletions: notify_emby_scan() triggers Emby "Clean Missing Files" task.
# Emby removes ghost entries immediately — no user-facing file-not-found errors.
#
# ==============================================================================================
# OPERATIONAL SAFEGUARDS
# ==============================================================================================
#
# Six gates — ALL must pass before any file is touched:
#   1. Container running and not starting/unhealthy
#   2. API reachable
#   3. API version matches RADARR_VERSION_MAJOR in master.conf
#   4. Movie count > 0
#   5. Tracked file count > 0
#   6. Deletion size < RADARR_MAX_DELETE_GB — or --i-know-what-im-doing required
#
#   acquire_lock "wait"  — large scans take time, wait for previous run to finish
#   jq + curl validation — exits if either tool missing
#   DOCKER_TIMEOUT       — container checks protected against daemon hangs
#   notify_emby_scan()   — triggers Emby clean after deletion
#   platform_require_cmd  — notify script validated before use
#   Silent by default    — orphans/junk warn(), clean library logs silently
#
# ==============================================================================================
# CONFIGURATION
# ==============================================================================================
#
# host*.conf
#
#   HOST*_RADARR_URL / HOST*_RADARR_API_KEY / HOST*_RADARR_MOVIES_ROOT
#   HOST*_RADARR_PATH_MAP — container path → host path translation
#   All aliased by detect_hosts() — script uses unprefixed names
#
# master.conf
#
#   RADARR_ORPHAN_AGE          — days before untracked file eligible for deletion
#   RADARR_MAX_DELETE_GB       — require --i-know-what-im-doing above this
#   RADARR_EXTENSIONS          — video file extensions for orphan classification
#   RADARR_PROTECTED_PATTERNS  — file patterns never deleted
#   RADARR_VERSION_MAJOR       — expected Radarr major version for API safety check
#   RADARR_IMPORT_SCAN_TIMEOUT — seconds to wait for pre-flight import scan (default 600)
#   ARR_CLEANUP_STATS          — stats file path (read by coffee report)
#
# ==============================================================================================
# RUNTIME MODES
# ==============================================================================================
#
#   radarr_cleanup.sh                                           — normal run
#   radarr_cleanup.sh --dry-run                                 — preview, no deletions
#   radarr_cleanup.sh --log                                     — verbose output
#   radarr_cleanup.sh --status                                  — show config and exit
#   radarr_cleanup.sh --i-know-what-im-doing                    — bypass size threshold
#   radarr_cleanup.sh --i-know-what-im-doing --skip-strike-list — NUCLEAR MODE
#
# NUCLEAR MODE: both flags bypass age check AND size threshold. User accepts full
# responsibility — the flag name is long and annoying by design.
#
# ==============================================================================================

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

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

# ── Special flag pre-processing ───────────────────────────────────────────────────────────────
I_KNOW=false
SKIP_STRIKES=false
FILTERED_ARGS=()
for arg in "$@"; do
    case "$arg" in
        --i-know-what-im-doing) I_KNOW=true ;;
        --skip-strike-list)     SKIP_STRIKES=true ;;
        *)                      FILTERED_ARGS+=("$arg") ;;
    esac
done

parse_args "${FILTERED_ARGS[@]}"

# ── Nuclear mode warning ──────────────────────────────────────────────────────────────────────
if [[ "$I_KNOW" == true ]] && [[ "$SKIP_STRIKES" == true ]] && [[ "$DRY_RUN" != true ]]; then
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "⚠️  WARNING — NUCLEAR MODE ACTIVE"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "  Flags:          --i-know-what-im-doing --skip-strike-list"
    echo "  Strike system:  BYPASSED — deletes on first pass"
    echo "  Size threshold: BYPASSED — no GB limit"
    echo "  Data recovery:  NOT POSSIBLE after deletion"
    echo ""
    echo "  Review --dry-run output before proceeding."
    echo "  You have 10 seconds to cancel (Ctrl+C)..."
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    sleep 10
    echo "  Proceeding..."
    echo ""
fi

# ==============================================================================================
# ━━━ Setup ━━━
# ==============================================================================================
if [[ "$EUID" -ne 0 ]]; then
    error "Must be run as root"
    exit 1
fi

if ! command -v curl >/dev/null 2>&1; then
    error "curl not found — required for Radarr API calls"
    exit 1
fi

if ! command -v jq >/dev/null 2>&1; then
    error "jq not found — required for JSON parsing"
    notify "Radarr cleanup failed on $(hostname) — jq not installed" "Radarr Cleanup" "warning"
    exit 1
fi


acquire_lock "wait"
TMP_DIR="/tmp/radarr_cleanup_$$"
mkdir -p "$TMP_DIR"
trap "_release_all_locks; rm -rf $TMP_DIR" EXIT

if ! command -v docker &>/dev/null; then
    error "Docker command not found"
    exit 1
fi

# detect_hosts() sets MY_ID and aliases RADARR_URL, RADARR_API_KEY, RADARR_MOVIES_ROOT
detect_hosts

DOCKER_TIMEOUT=15
RADARR_CONTAINER="Radarr"

# Build path map from MY_ID's Radarr path map
declare -A ARR_PATH_MAP
local_path_map_var="${MY_ID}_RADARR_PATH_MAP"
eval "for key in \"\${!${local_path_map_var}[@]}\"; do
    ARR_PATH_MAP[\"\$key\"]=\"\${${local_path_map_var}[\$key]}\"
done"

require_var RADARR_URL
require_var RADARR_API_KEY
require_var RADARR_MOVIES_ROOT

if [[ ! -d "$RADARR_MOVIES_ROOT" ]]; then
    error "Movies root not found: $RADARR_MOVIES_ROOT"
    notify "Radarr cleanup failed on $(hostname) — movies root not found: $RADARR_MOVIES_ROOT" \
        "Radarr Cleanup" "warning"
    exit 1
fi

log "$ICON_GEAR Config: url=${RADARR_URL} root=${RADARR_MOVIES_ROOT}"
echo "  $MY_ID ($LOCAL_SERVER_NAME) — $RADARR_URL"

[[ "$DRY_RUN" == true ]]      && warn "DRY RUN — no files will be deleted"
[[ "$I_KNOW" == true ]]       && warn "OVERRIDE — --i-know-what-im-doing active"
[[ "$SKIP_STRIKES" == true ]] && warn "OVERRIDE — --skip-strike-list active — age check bypassed"

# ==============================================================================================
# ━━━ Status ━━━
# ==============================================================================================
if [[ "$SHOW_STATUS" == true ]]; then
    echo ""
    echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━"
    echo "$ICON_HOST    Identity:           $MY_ID ($LOCAL_SERVER_NAME)"
    echo "$ICON_GEAR    Radarr URL:         $RADARR_URL"
    echo "$ICON_GEAR    Movies root:        $RADARR_MOVIES_ROOT"
    echo "$ICON_TIME    Orphan age:         ${RADARR_ORPHAN_AGE} days"
    echo "$ICON_GEAR    Max delete:         ${RADARR_MAX_DELETE_GB}GB (requires --i-know-what-im-doing)"
    echo "$ICON_GEAR    Radarr ver:         v${RADARR_VERSION_MAJOR} expected"
    echo "$ICON_GEAR    Extensions:         ${RADARR_EXTENSIONS[*]}"
    echo "$ICON_GEAR    Protected patterns: ${RADARR_PROTECTED_PATTERNS[*]}"
    echo "$ICON_GEAR    Dry Run:            $DRY_RUN"
    echo "$ICON_GEAR    I know:             $I_KNOW"
    echo "$ICON_GEAR    Skip strikes:       $SKIP_STRIKES"
    echo "━━━━━━━━━━━━━━━━━━━━━━━"
    exit 0
fi

# ==============================================================================================
# ━━━ Safety Layer 1 — Container Health ━━━
# ==============================================================================================
echo ""
echo "━━━ $ICON_SHIELD Safety Checks ━━━"

CONTAINER_RUNNING=$(timeout "$DOCKER_TIMEOUT" docker inspect -f \
    '{{.State.Running}}' "$RADARR_CONTAINER" 2>/dev/null)
if [[ "$CONTAINER_RUNNING" != "true" ]]; then
    error "$RADARR_CONTAINER is not running — aborting"
    notify "Radarr cleanup aborted on $(hostname) — container not running" \
        "Radarr Cleanup" "warning"
    exit 1
fi

CONTAINER_HEALTH=$(timeout "$DOCKER_TIMEOUT" docker inspect -f \
    '{{.State.Health.Status}}' "$RADARR_CONTAINER" 2>/dev/null)
case "$CONTAINER_HEALTH" in
    healthy)   info "$RADARR_CONTAINER is healthy" ;;
    "")        info "$RADARR_CONTAINER has no health check — proceeding" ;;
    starting)
        error "$RADARR_CONTAINER is still starting — aborting"
        notify "Radarr cleanup aborted on $(hostname) — container still starting" \
            "Radarr Cleanup" "warning"
        exit 1 ;;
    unhealthy)
        error "$RADARR_CONTAINER is unhealthy — aborting"
        notify "Radarr cleanup aborted on $(hostname) — container unhealthy" \
            "Radarr Cleanup" "warning"
        exit 1 ;;
    *) warn "$RADARR_CONTAINER health: $CONTAINER_HEALTH — proceeding with caution" ;;
esac

info "Safety layer 1 passed — container healthy"

# ==============================================================================================
# ── HELPER FUNCTIONS ──────────────────────────────────────────────────────────────────────────
# ==============================================================================================

radarr_api() {
    local endpoint="$1"
    local response http_code body

    response=$(curl -sf \
        --max-time 30 \
        -H "X-Api-Key: $RADARR_API_KEY" \
        -w "\n%{http_code}" \
        "${RADARR_URL}/api/v3/${endpoint}" 2>/dev/null)

    http_code=$(echo "$response" | tail -1)
    body=$(echo "$response" | head -n -1)

    if [[ "$http_code" != "200" ]]; then
        error "Radarr API HTTP $http_code for: $endpoint"
        return 1
    fi
    echo "$body"
}

is_video_file() {
    local ext="${1##*.}"
    ext="${ext,,}"
    for valid_ext in "${RADARR_EXTENSIONS[@]}"; do
        [[ "$ext" == "$valid_ext" ]] && return 0
    done
    return 1
}

is_protected_file() {
    local filename
    filename=$(basename "$1")
    for pattern in "${RADARR_PROTECTED_PATTERNS[@]}"; do
        # shellcheck disable=SC2254
        case "$filename" in
            $pattern) return 0 ;;
        esac
    done
    return 1
}

format_bytes() {
    local bytes=$1
    if (( bytes > 1073741824 )); then
        awk "BEGIN {printf \"%.1fGB\", $bytes / 1073741824}"
    elif (( bytes > 1048576 )); then
        awk "BEGIN {printf \"%.1fMB\", $bytes / 1048576}"
    else
        echo "${bytes}B"
    fi
}

# ==============================================================================================
# ━━━ Pre-flight: Radarr Import Scan ━━━
# ==============================================================================================
echo ""
echo "━━━ $ICON_SYNC Pre-flight: Radarr Import Scan ━━━"

# Fetch root folders from Radarr API and translate container paths to host paths
mapfile -t SCAN_ROOTS < <(
    radarr_api "rootfolder" | \
    jq -r '.[].path' 2>/dev/null | \
    while IFS= read -r cp; do translate_path "$cp"; done
)

if [[ "${#SCAN_ROOTS[@]}" -eq 0 ]]; then
    error "No root folders returned from Radarr API — aborting"
    notify "Radarr cleanup aborted on $(hostname) — no root folders from API" \
        "Radarr Cleanup" "warning"
    exit 1
fi

info "Scan targets (${#SCAN_ROOTS[@]}): ${SCAN_ROOTS[*]}"

info "Triggering DownloadedMoviesScan (all root folders)"
SCAN_PAYLOAD='{"name": "DownloadedMoviesScan"}'

SCAN_RESPONSE=$(curl -sf --max-time 30 -X POST \
    -H "X-Api-Key: $RADARR_API_KEY" \
    -H "Content-Type: application/json" \
    -d "$SCAN_PAYLOAD" \
    "${RADARR_URL}/api/v3/command" 2>/dev/null)

SCAN_CMD_ID=$(echo "$SCAN_RESPONSE" | jq -r '.id // empty' 2>/dev/null)

if [[ -z "$SCAN_CMD_ID" ]]; then
    warn "Could not trigger import scan — proceeding without pre-flight"
else
    info "Import scan queued (command ID: $SCAN_CMD_ID) — waiting for completion..."
    POLL_TIMEOUT=${RADARR_IMPORT_SCAN_TIMEOUT:-600}
    POLLED=0
    while [[ "$POLLED" -lt "$POLL_TIMEOUT" ]]; do
        SCAN_STATUS=$(curl -sf --max-time 10 \
            -H "X-Api-Key: $RADARR_API_KEY" \
            "${RADARR_URL}/api/v3/command/${SCAN_CMD_ID}" 2>/dev/null | \
            jq -r '.status // empty' 2>/dev/null)
        case "$SCAN_STATUS" in
            completed) info "Import scan complete ✅"; break ;;
            failed)    warn "Import scan reported failed — proceeding anyway"; break ;;
        esac
        sleep 10
        (( POLLED += 10 ))
        [[ $(( POLLED % 60 )) -eq 0 ]] && log "  Still scanning... (${POLLED}s elapsed)"
    done
    [[ "$POLLED" -ge "$POLL_TIMEOUT" ]] && \
        warn "Import scan timed out after ${POLL_TIMEOUT}s — proceeding anyway"
fi

# ==============================================================================================
# ━━━ Fetch Radarr Tracked Files ━━━
# ==============================================================================================
echo ""
echo "━━━ $ICON_SYNC Fetching Radarr Tracked Files ━━━"

# Safety Layer 2 — API reachability
if ! check_api "$RADARR_URL" "Radarr" 10; then
    notify "Radarr cleanup aborted on $(hostname) — API unreachable" "Radarr Cleanup" "warning"
    exit 1
fi

# Safety Layer 3 — API version check
check_arr_version "$RADARR_URL" "$RADARR_API_KEY" "v3" "$RADARR_VERSION_MAJOR" "Radarr" || exit 1

info "Querying Radarr API..."

# Fetch all movies
MOVIES_RESPONSE=$(radarr_api "movie") || {
    error "Failed to fetch movies from Radarr"
    notify "Radarr cleanup failed on $(hostname) — could not fetch movies" \
        "Radarr Cleanup" "warning"
    exit 1
}

MOVIE_IDS=$(echo "$MOVIES_RESPONSE" | jq -r '.[].id' 2>/dev/null)
MOVIE_COUNT=$(echo "$MOVIE_IDS" | grep -c "." 2>/dev/null || echo 0)

# Safety Layer 4 — movie count > 0
if [[ "$MOVIE_COUNT" -eq 0 ]]; then
    error "API returned 0 movies — aborting to prevent mass deletion"
    notify "Radarr cleanup aborted on $(hostname) — 0 movies returned" \
        "Radarr Cleanup" "warning"
    exit 1
fi

info "Found $MOVIE_COUNT movies — fetching movie files..."

TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
> "$TRACKED_FILE"

MOVIE_INDEX=0
while IFS= read -r movie_id; do
    [[ -z "$movie_id" ]] && continue
    (( MOVIE_INDEX++ ))
    [[ $(( MOVIE_INDEX % 100 )) -eq 0 ]] && \
        log "Fetching files: $MOVIE_INDEX/$MOVIE_COUNT movies..."
    MOVIE_FILES=$(radarr_api "moviefile?movieId=${movie_id}" 2>/dev/null)
    if [[ -n "$MOVIE_FILES" ]]; then
        while IFS= read -r api_path; do
            [[ -z "$api_path" ]] && continue
            translate_path "$api_path" >> "$TRACKED_FILE"
        done < <(echo "$MOVIE_FILES" | jq -r '.[].path // .path' 2>/dev/null)
    fi
done <<< "$MOVIE_IDS"

sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"

# Build in-memory lookup map — O(1) per lookup vs O(n) grep per file
# Eliminates the main performance bottleneck for large libraries
declare -A TRACKED_MAP
while IFS= read -r _tracked_path; do
    [[ -n "$_tracked_path" ]] && TRACKED_MAP["$_tracked_path"]=1
done < "$TRACKED_FILE"
unset _tracked_path
info "Built in-memory lookup map: ${#TRACKED_MAP[@]} tracked paths"
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")

# Safety Layer 5 — tracked count > 0
if [[ "$TRACKED_COUNT" -eq 0 ]]; then
    error "API returned 0 tracked files — aborting to prevent mass deletion"
    notify "Radarr cleanup aborted on $(hostname) — 0 tracked files returned" \
        "Radarr Cleanup" "warning"
    exit 1
fi

info "$MOVIE_COUNT movies | $TRACKED_COUNT tracked movie files"

# ==============================================================================================
# ━━━ Scan Movies Root ━━━
# ==============================================================================================
echo ""
echo "━━━ $ICON_CLEAN Scanning Movies Root ━━━"
info "Root: $RADARR_MOVIES_ROOT | Orphan age: ${RADARR_ORPHAN_AGE} days"

START=$(date +%s)
ORPHAN_COUNT=0
JUNK_COUNT=0
RECENT_COUNT=0
PROTECTED_COUNT=0
ORPHAN_BYTES=0
JUNK_BYTES=0

AGE_SECONDS=$(( RADARR_ORPHAN_AGE * 86400 ))
NOW=$(date +%s)
MAX_DELETE_BYTES=$(awk "BEGIN {printf \"%d\", $RADARR_MAX_DELETE_GB * 1073741824}")

while IFS= read -r filepath; do
    [[ -z "$filepath" ]] && continue

    if [[ -n "${TRACKED_MAP[$filepath]:-}" ]]; then
        log "TRACKED: $filepath"
        continue
    fi

    if is_protected_file "$filepath"; then
        log "$ICON_PROTECTED PROTECTED: $filepath"
        (( PROTECTED_COUNT++ ))
        continue
    fi

    FILE_SIZE=$(stat -c%s "$filepath" 2>/dev/null || echo 0)

    if is_video_file "$filepath"; then
        FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0)
        FILE_AGE=$(( NOW - FILE_MTIME ))

        if [[ "$FILE_AGE" -lt "$AGE_SECONDS" ]] && [[ "$SKIP_STRIKES" != true ]]; then
            log "RECENT (skipping): $filepath"
            (( RECENT_COUNT++ ))
            continue
        fi

        warn "$ICON_TRASH ORPHAN: $filepath"
        (( ORPHAN_COUNT++ ))
        ORPHAN_BYTES=$(( ORPHAN_BYTES + FILE_SIZE ))
    else
        log "JUNK: $filepath"
        (( JUNK_COUNT++ ))
        JUNK_BYTES=$(( JUNK_BYTES + FILE_SIZE ))
    fi

done < <(
    for host_path in "${SCAN_ROOTS[@]}"; do
        [[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null
    done | sort -u
)

TOTAL_DELETE_BYTES=$(( ORPHAN_BYTES + JUNK_BYTES ))
TOTAL_REMOVED=$(( ORPHAN_COUNT + JUNK_COUNT ))

# ==============================================================================================
# ━━━ Safety Layer 6 — Deletion Size Threshold ━━━
# ==============================================================================================
if [[ "$TOTAL_DELETE_BYTES" -gt "$MAX_DELETE_BYTES" ]]; then
    TOTAL_HUMAN=$(awk "BEGIN {printf \"%.1fGB\", $TOTAL_DELETE_BYTES / 1073741824}")
    if [[ "$I_KNOW" != true ]]; then
        echo ""
        error "Deletion would exceed ${RADARR_MAX_DELETE_GB}GB — $TOTAL_HUMAN would be deleted"
        error "Review ORPHAN lines above carefully before proceeding"
        error "Rerun with: --i-know-what-im-doing"
        error "To also bypass age check: add --skip-strike-list"
        notify "Radarr cleanup halted on $(hostname) — ${TOTAL_HUMAN} requires --i-know-what-im-doing" \
            "Radarr Cleanup" "warning"
        exit 1
    else
        warn "OVERRIDE — deletion is $TOTAL_HUMAN — proceeding with --i-know-what-im-doing"
    fi
fi

# ── Execute Deletions ─────────────────────────────────────────────────────────────────────────
if [[ "$DRY_RUN" == false ]]; then
    while IFS= read -r filepath; do
        [[ -z "$filepath" ]] && continue
        [[ -n "${TRACKED_MAP[$filepath]:-}" ]] && continue
        is_protected_file "$filepath" && continue

        FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0)
        FILE_AGE=$(( NOW - FILE_MTIME ))

        if is_video_file "$filepath"; then
            [[ "$FILE_AGE" -lt "$AGE_SECONDS" ]] && \
                [[ "$SKIP_STRIKES" != true ]] && continue
        fi

        rm -f "$filepath" 2>/dev/null || error "Failed to delete: $filepath"

    done < <(
        for host_path in "${SCAN_ROOTS[@]}"; do
            [[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null
        done | sort -u
    )

    info "Cleaning up empty folders..."
    for host_path in "${SCAN_ROOTS[@]}"; do
        [[ -d "$host_path" ]] && \
            find "$host_path" -mindepth 1 -type d -empty -delete 2>/dev/null
    done
    info "Empty folders removed"
fi

END=$(date +%s)

ORPHAN_HUMAN=$(format_bytes "$ORPHAN_BYTES")
JUNK_HUMAN=$(format_bytes "$JUNK_BYTES")

# ==============================================================================================
# ━━━ Summary ━━━
# ==============================================================================================
echo ""
echo "━━━━━ $ICON_SUMMARY RADARR CLEANUP SUMMARY ━━━━━"
echo "$ICON_HOST    Identity:        $MY_ID ($LOCAL_SERVER_NAME)"
echo "$ICON_SYNC    Tracked:         $TRACKED_COUNT files ($MOVIE_COUNT movies)"
echo "$ICON_SHIELD  Protected:       $PROTECTED_COUNT files (artwork, subtitles, metadata)"
echo "$ICON_TRASH   Orphans:         $ORPHAN_COUNT files ($ORPHAN_HUMAN)"
echo "$ICON_TRASH   Junk:            $JUNK_COUNT files ($JUNK_HUMAN)"
echo "$ICON_SKIP    Recent skipped:  $RECENT_COUNT files (under ${RADARR_ORPHAN_AGE} days)"
echo "$ICON_TIME    Duration:        $(format_duration $(( END - START )))"
echo ""

if [[ "$DRY_RUN" == true ]]; then
    warn "DRY RUN — no files deleted"
elif [[ "$TOTAL_REMOVED" -eq 0 ]]; then
    echo "$ICON_DONE Clean — nothing to remove"
else
    warn "$ICON_DONE Removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)"
    notify "Radarr cleanup on $(hostname) — removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" \
        "Radarr Cleanup" "warning"
    # Notify Emby to clean missing files — removes ghost entries immediately
    notify_emby_scan
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Write stats for sunday_morning_coffee_report.sh
if [[ "$DRY_RUN" == false ]] && [[ -n "${ARR_CLEANUP_STATS:-}" ]]; then
    echo "$(date '+%Y-%m-%d')|radarr|${ORPHAN_COUNT}|${ORPHAN_BYTES}|${JUNK_COUNT}|${JUNK_BYTES}|${RECENT_COUNT}|${TRACKED_COUNT}" \
        >> "$ARR_CLEANUP_STATS" 2>/dev/null || true
fi

exit 0