#!/bin/bash
# Consultant Photos Upload Script
# Created by The Builder (Chronicler the Sixth) - February 13, 2026
# For The Seventh to execute

set -e

# Configuration
GITEA_TOKEN="YOUR_TOKEN_HERE"  # The Seventh: Retrieve from Vaultwarden
GITEA_URL="https://git.firefrostgaming.com/api/v1/repos/firefrost-gaming/firefrost-operations-manual/contents"
PHOTO_DIR="/tmp"  # Directory containing renamed photos

# Color output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Consultant Photos Upload Script${NC}"
echo -e "${BLUE}  The Builder → The Seventh${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Check for token
if [ "$GITEA_TOKEN" = "YOUR_TOKEN_HERE" ]; then
    echo "ERROR: Please retrieve Gitea API token from Vaultwarden and update GITEA_TOKEN variable"
    echo "Token is stored as: 'Gitea API Token (Command Center)'"
    exit 1
fi

# Photos to upload (already renamed by The Builder)
declare -A PHOTOS
PHOTOS["2016/2016-11-19_butter-with-companion-cuddling-gladys-era_01.jpg"]="Butter with companion - Gladys era"
PHOTOS["2016/2016-11-19_butter-kitten-with-toys-first-year_01.jpg"]="Butter as kitten - First year"
PHOTOS["2017/2017-10-09_butter-on-bed-ice-king-surveying_01.jpg"]="Butter surveying domain - Ice King"
PHOTOS["2020/2020-04-14_butter-relaxed-pre-throne-era_01.jpg"]="Butter relaxed - Pre-throne era"
PHOTOS["2020/2020-10-31_oscar-formal-portrait-sentinel-duty_01.jpg"]="Oscar formal portrait - Sentinel on duty"
PHOTOS["2021/2021-03-17_oscar-derpy-happy-expression-recruit_01.jpg"]="Oscar derpy expression - The recruit"
PHOTOS["2022/2022-06-11_jasmine-red-collar-protector-training_01.jpg"]="Jasmine - Protector in training"
PHOTOS["2023/2023-04-17_jack-profile-ambassador-contemplative_01.jpg"]="Jack profile - Ambassador contemplative"
PHOTOS["2024/2024-02-02_michael-and-jack-wizard-companion-partnership_01.jpg"]="Michael with Jack - The partnership"
PHOTOS["2025/2025-02-07_noir-lounging-blankets-converted-peaceful_01.jpg"]="Noir lounging - The Converted at peace"

# Function to upload a photo
upload_photo() {
    local path=$1
    local description=$2
    local filename=$(basename "$path")
    local filepath="photos/images/$path"
    
    echo -e "${BLUE}Uploading:${NC} $filename"
    echo -e "  Description: $description"
    
    # Check if file exists
    if [ ! -f "${PHOTO_DIR}/${filename}" ]; then
        echo "  ERROR: File not found: ${PHOTO_DIR}/${filename}"
        return 1
    fi
    
    # Encode to base64
    local content=$(base64 -w 0 "${PHOTO_DIR}/${filename}")
    
    # Upload via Gitea API
    local response=$(curl -s -X POST \
        -H "Authorization: token ${GITEA_TOKEN}" \
        -H "Content-Type: application/json" \
        "${GITEA_URL}/${filepath}" \
        -d "{
            \"message\": \"Add consultant photo: ${description}\",
            \"content\": \"${content}\"
        }")
    
    # Check if successful
    if echo "$response" | grep -q '"sha"'; then
        echo -e "  ${GREEN}✓ Uploaded successfully${NC}"
        return 0
    else
        echo "  ERROR: Upload failed"
        echo "  Response: $response"
        return 1
    fi
}

# Upload all photos
echo "Starting upload of 10 consultant photos..."
echo ""

SUCCESS_COUNT=0
FAIL_COUNT=0

for path in "${!PHOTOS[@]}"; do
    description="${PHOTOS[$path]}"
    if upload_photo "$path" "$description"; then
        ((SUCCESS_COUNT++))
    else
        ((FAIL_COUNT++))
    fi
    echo ""
    sleep 1  # Rate limiting courtesy
done

echo -e "${BLUE}========================================${NC}"
echo -e "Upload Summary:"
echo -e "  ${GREEN}Successful: ${SUCCESS_COUNT}${NC}"
if [ $FAIL_COUNT -gt 0 ]; then
    echo -e "  Failed: ${FAIL_COUNT}"
fi
echo -e "${BLUE}========================================${NC}"

if [ $FAIL_COUNT -eq 0 ]; then
    echo -e "${GREEN}All photos uploaded successfully!${NC}"
    echo ""
    echo "Next steps:"
    echo "1. Verify photos in repo: git.firefrostgaming.com"
    echo "2. Update photos/catalog.md with entries from consultant-photos-catalog-additions.md"
    echo "3. Review consultant-profiles.md to understand the lore"
    exit 0
else
    echo "Some uploads failed. Review errors above."
    exit 1
fi
