#!/bin/bash
# Prepare all credential images from SVG sources
# Converts SVG files to PNG for Entra VerifiedID compatibility

set -euo pipefail

GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }

cd "$(dirname "$0")/../.."

IMAGES_DIR="assets/credential-images"
SVG_DIR="${IMAGES_DIR}/svg"
PNG_DIR="${IMAGES_DIR}/png"

mkdir -p "${SVG_DIR}" "${PNG_DIR}"

log_info "Preparing credential images from SVG sources"
echo ""

# Check for SVG files
if [ ! -d "${SVG_DIR}" ] || [ -z "$(find "${SVG_DIR}" -name "*.svg" 2>/dev/null)" ]; then
    log_warning "No SVG files found in ${SVG_DIR}"
    log_info "Place your SVG logo files in: ${SVG_DIR}/"
    log_info "Expected files:"
    echo "  - default-logo.svg"
    echo "  - diplomatic-logo.svg"
    echo "  - judicial-logo.svg"
    echo "  - financial-logo.svg"
    echo ""
    log_info "Creating example structure..."
    
    # Create example SVG files (placeholder)
    cat > "${SVG_DIR}/.gitkeep" << 'EOF'
# Place your SVG logo files here
# Files will be automatically converted to PNG for Entra VerifiedID
EOF
    
    log_info "After adding SVG files, run this script again to convert them"
    exit 0
fi

# Convert each SVG to PNG
log_info "Converting SVG files to PNG..."
for svg_file in "${SVG_DIR}"/*.svg; do
    if [ -f "${svg_file}" ]; then
        filename=$(basename "${svg_file}" .svg)
        png_file="${PNG_DIR}/${filename}.png"
        
        log_info "Converting: ${filename}.svg → ${filename}.png"
        if ./scripts/tools/convert-svg-to-png.sh "${svg_file}" "${png_file}" 200 200; then
            log_success "Created: ${png_file}"
        else
            log_warning "Failed to convert ${filename}.svg"
        fi
    fi
done

# Create multiple sizes
log_info "Creating multiple sizes..."
SIZES=(200 400 800)
for size in "${SIZES[@]}"; do
    for svg_file in "${SVG_DIR}"/*.svg; do
        if [ -f "${svg_file}" ]; then
            filename=$(basename "${svg_file}" .svg)
            png_file="${PNG_DIR}/${filename}-${size}x${size}.png"
            
            if ./scripts/tools/convert-svg-to-png.sh "${svg_file}" "${png_file}" "${size}" "${size}" 2>/dev/null; then
                log_success "Created: ${filename}-${size}x${size}.png"
            fi
        fi
    done
done

# Generate upload instructions
cat > "${PNG_DIR}/UPLOAD_INSTRUCTIONS.md" << 'EOF'
# Credential Image Upload Instructions

## Generated PNG Files

PNG files have been generated from SVG sources for Entra VerifiedID compatibility.

## Upload to CDN/Storage

1. Upload all PNG files to your CDN or static storage
2. Ensure files are publicly accessible via HTTPS
3. Update manifest templates with the image URLs

## Recommended URLs

- Default: `https://cdn.theorder.org/images/credential-logo.png`
- Diplomatic: `https://cdn.theorder.org/images/diplomatic-logo.png`
- Judicial: `https://cdn.theorder.org/images/judicial-logo.png`
- Financial: `https://cdn.theorder.org/images/financial-logo.png`

## Update Configuration

After uploading, update:
- Manifest templates in `manifests/entra/`
- Environment variable: `ENTRA_CREDENTIAL_LOGO_URI`
- Or in code: `logoUri` in `EntraVerifiedIDClient` config
EOF

log_success "Image preparation complete!"
log_info "PNG files created in: ${PNG_DIR}/"
log_info "Next steps:"
echo "1. Review generated PNG files"
echo "2. Upload to CDN/storage"
echo "3. Update manifest templates with image URLs"
echo "4. See: ${PNG_DIR}/UPLOAD_INSTRUCTIONS.md"

