#!/bin/bash
# getVideo.sh — Download video assets from YouTube
#
# Usage:
#   getVideo.sh <cat> <vid>              # download all: .json + .jpg + .mp4
#   getVideo.sh <cat> <vid> --json       # repair: only .json sidecar
#   getVideo.sh <cat> <vid> --jpg        # repair: only .jpg thumbnail
#   getVideo.sh <cat> <vid> --mp4        # repair: only .mp4 video

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
BASE_DIR="$(dirname "$SCRIPT_DIR")"

cat=$1
vid=$2
mode="${3:-all}"   # all | --json | --jpg | --mp4

do_json=false; do_jpg=false; do_mp4=false
case "$mode" in
    --json) do_json=true ;;
    --jpg)  do_jpg=true  ;;
    --mp4)  do_mp4=true  ;;
    *)      do_json=true; do_jpg=true; do_mp4=true ;;
esac

ERROR_LOG="$BASE_DIR/logs/error.log"
error=0

log_error() {
    echo "ERROR: $1"
    echo "[$(date -u '+%Y-%m-%d %H:%M:%S UTC')] getVideo $cat/$vid — $1" >> "$ERROR_LOG"
}

mkdir -p "$BASE_DIR/videos/$cat"
echo "getVideo.sh  cat=$cat  vid=$vid  mode=$mode"
echo "--------------------------------------------------"
echo "started at $(date)"

# ── Step 1: fetch metadata from YouTube API (needed for json and jpg) ─────────
if $do_json || $do_jpg; then
    KEY_FILE="$BASE_DIR/data/yt_key.txt"
    if [ ! -f "$KEY_FILE" ]; then log_error "Key file not found: $KEY_FILE"; exit 1; fi
    API_KEY=$(tr -d '[:space:]' < "$KEY_FILE")
    if [ -z "$API_KEY" ]; then log_error "yt_key.txt is empty"; exit 1; fi

    API_URL="https://www.googleapis.com/youtube/v3/videos?id=$vid&key=$API_KEY&part=snippet,contentDetails,statistics"
    echo "Fetching metadata: $API_URL"
    curl -sf "$API_URL" -o /tmp/ytapi_$vid.json
    if [ $? -ne 0 ] || [ ! -s /tmp/ytapi_$vid.json ]; then
        log_error "YouTube API request failed — URL: $API_URL"; exit 1
    fi

    # Check for API-level errors (quota exceeded, bad key, etc.)
    api_error=$(python3 -c "
import json, sys
d = json.load(open('/tmp/ytapi_$vid.json'))
err = d.get('error', {})
if err:
    print(f\"{err.get('code','?')} {err.get('message','unknown error')}\")
    sys.exit(1)
" 2>/dev/null)
    if [ $? -ne 0 ]; then
        log_error "YouTube API error: $api_error — URL: $API_URL"; exit 1
    fi
fi

# ── Step 2: write .json sidecar ───────────────────────────────────────────────
if $do_json; then
    THUMB_URL=$(VID=$vid CAT=$cat BASE_DIR=$BASE_DIR python3 << 'PYEOF'
import json, re, os, sys

vid      = os.environ['VID']
cat      = os.environ['CAT']
base_dir = os.environ['BASE_DIR']

with open(f'/tmp/ytapi_{vid}.json') as f:
    data = json.load(f)

if not data.get('items'):
    print(f"ERROR: video {vid} not found in API response (may be private/deleted)", file=sys.stderr)
    sys.exit(1)

item    = data['items'][0]
snippet = item.get('snippet', {})
details = item.get('contentDetails', {})
stats   = item.get('statistics', {})

dur      = re.match(r'PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?', details.get('duration', 'PT0S'))
duration = int(dur.group(1) or 0)*3600 + int(dur.group(2) or 0)*60 + int(dur.group(3) or 0)
upload_date = snippet.get('publishedAt', '')[:10].replace('-', '')
thumbs   = snippet.get('thumbnails', {})
thumb_url = next((thumbs[q]['url'] for q in ['maxres','standard','high','medium','default'] if q in thumbs), '')

result = {
    'id':          vid,
    'title':       snippet.get('title', ''),
    'upload_date': upload_date,
    'duration':    duration,
    'view_count':  int(stats.get('viewCount', 0)),
    'like_count':  int(stats.get('likeCount', 0)),
    'description': snippet.get('description', ''),
    'thumbnail':   thumb_url,
    'uploader':    snippet.get('channelTitle', ''),
}
with open(f'{base_dir}/videos/{cat}/{vid}.json', 'w') as f:
    json.dump(result, f, indent=2)
print(thumb_url)
PYEOF
)
    if [ $? -ne 0 ]; then
        log_error "metadata write failed (API returned no items)"; error=1
    fi
fi

# ── Step 3: download .jpg thumbnail ──────────────────────────────────────────
if $do_jpg; then
    # If we didn't fetch metadata above (--jpg only mode), get the thumbnail URL now
    if [ -z "$THUMB_URL" ]; then
        THUMB_URL=$(VID=$vid python3 << 'PYEOF'
import json, os, sys
vid = os.environ['VID']
with open(f'/tmp/ytapi_{vid}.json') as f:
    data = json.load(f)
if not data.get('items'):
    sys.exit(1)
thumbs = data['items'][0]['snippet'].get('thumbnails', {})
print(next((thumbs[q]['url'] for q in ['maxres','standard','high','medium','default'] if q in thumbs), ''))
PYEOF
)
    fi

    echo "Downloading thumbnail: $THUMB_URL"
    curl -s "$THUMB_URL" -o "$SCRIPT_DIR/tmp/$vid.jpg"
    if [ $? -ne 0 ] || [ ! -s "$SCRIPT_DIR/tmp/$vid.jpg" ]; then
        log_error "thumbnail download failed — URL: $THUMB_URL"; error=1
    else
        bash "$SCRIPT_DIR/checkFile.sh" jpg "$cat"
    fi
fi

# ── Step 4: download .mp4 via yt-dlp ─────────────────────────────────────────
if $do_mp4; then
    COOKIES_WORK=$(mktemp /tmp/cookies_work_XXXXXX.txt)
    cp "$BASE_DIR/data/cookies.txt" "$COOKIES_WORK"
    echo "Running: yt-dlp ... --cookies $COOKIES_WORK -o $SCRIPT_DIR/tmp/$vid.%(ext)s -- $vid"
    yt-dlp -f best --no-progress --no-warnings --cookies "$COOKIES_WORK" -o "$SCRIPT_DIR/tmp/$vid.%(ext)s" -- "$vid"
    yt_exit=$?
    rm -f "$COOKIES_WORK"
    if [ $yt_exit -ne 0 ]; then
        log_error "yt-dlp download failed"; error=1
    else
        bash "$SCRIPT_DIR/checkFile.sh" mp4 "$cat"
    fi
fi

echo "getVideo end at $(date)"
if [ $error -ne 0 ]; then
    echo "FAILED: $cat/$vid — see $ERROR_LOG"
    exit 1
fi
