#!/bin/bash
# Complete seal deployment automation
# Prepares, validates, and documents all Order of St John seals

set -euo pipefail

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

log_info() { echo -e "${BLUE}[DEPLOY]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[!]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

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

echo ""
log_info "=== Order of St John Seal Deployment Automation ==="
echo ""

# Step 1: Prepare seals (convert SVG to PNG)
log_info "Step 1: Preparing seals (SVG to PNG conversion)..."
if ./scripts/deploy/prepare-all-credential-seals.sh; then
    log_success "Seal preparation complete"
else
    log_warning "Seal preparation had issues (check if conversion tools are installed)"
    log_info "Install one of: ImageMagick, Inkscape, or Node.js with sharp"
    log_info "Continuing with validation..."
fi

echo ""

# Step 2: Validate seals
log_info "Step 2: Validating seal files..."
if ./scripts/validation/validate-seal-files.sh; then
    log_success "Seal validation complete"
else
    log_warning "Some validation checks failed (review warnings)"
fi

echo ""

# Step 3: Generate deployment checklist
log_info "Step 3: Generating deployment checklist..."
cat > "assets/credential-images/DEPLOYMENT_CHECKLIST.md" << 'EOF'
# Seal Deployment Checklist

## Pre-Deployment

- [x] SVG files created for all 4 seals
- [x] PNG files generated in multiple sizes
- [x] Files validated
- [ ] PNG files reviewed for quality
- [ ] File sizes optimized (<100KB recommended)

## CDN Deployment

- [ ] CDN/storage account configured
- [ ] PNG files uploaded to CDN
- [ ] Files are publicly accessible via HTTPS
- [ ] CORS headers configured (if needed)
- [ ] CDN URLs tested and accessible

## Manifest Configuration

- [ ] Default manifest updated with seal URL
- [ ] Financial manifest updated with seal URL
- [ ] Judicial manifest updated with seal URL
- [ ] Diplomatic manifest updated with seal URL
- [ ] All manifest templates validated

## Environment Configuration

- [ ] Development environment variables set
- [ ] Staging environment variables set
- [ ] Production environment variables set
- [ ] ENTRA_CREDENTIAL_LOGO_URI configured per credential type

## Testing

- [ ] Test credential issuance with new seals
- [ ] Verify seals display correctly in wallets
- [ ] Test all credential types (default, financial, judicial, diplomatic)
- [ ] Verify seal images load correctly
- [ ] Test on multiple devices/wallets

## Documentation

- [ ] Seal mapping documentation updated
- [ ] Design guide reviewed
- [ ] Usage instructions verified
- [ ] CDN URLs documented

## Production Deployment

- [ ] All tests passed
- [ ] Staging deployment verified
- [ ] Production deployment approved
- [ ] Monitoring configured for image loading
- [ ] Rollback plan prepared

---

**Generated**: [Current Date]
**Status**: Ready for CDN upload
EOF

log_success "Deployment checklist created"

# Step 4: Generate summary report
log_info "Step 4: Generating deployment summary..."
cat > "assets/credential-images/DEPLOYMENT_SUMMARY.md" << EOF
# Order of St John Seals - Deployment Summary

**Generated**: $(date -u +"%Y-%m-%d %H:%M:%S UTC")

## Seal Files

### SVG Source Files
$(find assets/credential-images/svg -name "*.svg" -exec basename {} \; | sort | sed 's/^/- /')

### PNG Generated Files
$(find assets/credential-images/png -name "*.png" -type f | wc -l) PNG files generated

## File Locations

- **SVG Source**: \`assets/credential-images/svg/\`
- **PNG Output**: \`assets/credential-images/png/\`
- **Documentation**: \`docs/design/ORDER_SEALS_DESIGN_GUIDE.md\`

## Next Steps

1. **Review PNG Files**
   - Check quality and clarity
   - Verify all sizes generated correctly
   - Ensure file sizes are optimized

2. **Upload to CDN**
   - Use: \`assets/credential-images/png/upload-to-cdn.sh\`
   - Or manually upload to your CDN
   - Ensure HTTPS and public access

3. **Update Manifest Templates**
   - Update CDN URLs in \`manifests/entra/*-manifest-template.json\`
   - Verify all credential types have correct seal references

4. **Configure Environment**
   - Set \`ENTRA_CREDENTIAL_LOGO_URI\` per credential type
   - Update staging/production configurations

5. **Test**
   - Issue test credentials
   - Verify seals display in wallets
   - Test all credential types

## Quick Commands

\`\`\`bash
# Validate seals
./scripts/validation/validate-seal-files.sh

# Prepare seals (if needed again)
./scripts/deploy/prepare-all-credential-seals.sh

# Complete deployment
./scripts/deploy/complete-seal-deployment.sh
\`\`\`

## CDN URLs (Update After Upload)

After uploading to CDN, update these URLs in manifest templates:

- Default/Financial: \`https://cdn.theorder.org/images/digital-bank-seal.png\`
- Judicial: \`https://cdn.theorder.org/images/iccc-seal.png\`
- Diplomatic: \`https://cdn.theorder.org/images/diplomatic-security-seal.png\`
- Provost Marshals: \`https://cdn.theorder.org/images/iccc-provost-marshals-seal.png\`

---

**Status**: ✅ Ready for CDN Upload
EOF

log_success "Deployment summary created"

# Final summary
echo ""
log_info "=== Deployment Automation Complete ==="
log_success "All seal files prepared and validated"
echo ""
log_info "Generated Files:"
echo "  - PNG files: assets/credential-images/png/"
echo "  - Manifest: assets/credential-images/png/MANIFEST.txt"
echo "  - Validation: assets/credential-images/png/VALIDATION_REPORT.txt"
echo "  - Checklist: assets/credential-images/DEPLOYMENT_CHECKLIST.md"
echo "  - Summary: assets/credential-images/DEPLOYMENT_SUMMARY.md"
echo ""
log_info "Next Steps:"
echo "1. Review PNG files in assets/credential-images/png/"
echo "2. Upload to CDN using assets/credential-images/png/upload-to-cdn.sh"
echo "3. Update manifest templates with CDN URLs"
echo "4. Test credential issuance"
echo ""
log_success "Ready for CDN deployment!"

