#!/bin/bash
# =============================================================================
# Schema Sync Script
# =============================================================================
# Synchronizes the database schema with the Payload-generated schema.
# Use this after adding new Collections/Globals to ensure the database
# has all required tables and columns.
#
# Usage:
#   ./scripts/sync-schema.sh           # Interactive mode
#   ./scripts/sync-schema.sh --force   # Non-interactive (for CI/automation)
#   ./scripts/sync-schema.sh --dry-run # Show what would change
# =============================================================================

set -e

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

# Parse arguments
FORCE=false
DRY_RUN=false

for arg in "$@"; do
  case $arg in
    --force|-f)
      FORCE=true
      shift
      ;;
    --dry-run|-n)
      DRY_RUN=true
      shift
      ;;
    -h|--help)
      head -15 "$0" | tail -12
      exit 0
      ;;
  esac
done

# Determine environment
if [ -f "/home/payload/payload-cms/.env" ]; then
  source /home/payload/payload-cms/.env 2>/dev/null || true
fi

PROJECT_DIR="${PROJECT_DIR:-/home/payload/payload-cms}"
cd "$PROJECT_DIR"

echo -e "${BLUE}=============================================="
echo -e "  Database Schema Sync"
echo -e "==============================================${NC}"
echo ""

# Check if schema file exists
if [ ! -f "src/payload-generated-schema.ts" ]; then
  echo -e "${YELLOW}Generating schema file...${NC}"
  pnpm payload generate:db-schema 2>/dev/null || true
fi

if [ ! -f "src/payload-generated-schema.ts" ]; then
  echo -e "${RED}Error: Could not find or generate schema file${NC}"
  exit 1
fi

# Dry run mode
if [ "$DRY_RUN" = true ]; then
  echo -e "${YELLOW}[DRY RUN] Showing what would change:${NC}"
  echo ""
  pnpm exec drizzle-kit push --config=drizzle.production.config.ts --dry-run 2>&1 || true
  exit 0
fi

# Interactive confirmation
if [ "$FORCE" != true ]; then
  echo -e "${YELLOW}This will sync the database schema with the code.${NC}"
  echo "Database: $DATABASE_URI"
  echo ""
  read -p "Continue? (y/n) " -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Aborted."
    exit 0
  fi
fi

# Run schema sync
echo ""
echo -e "${RED}WARNUNG: drizzle-kit push kann Daten löschen wenn Spalten entfernt werden!${NC}"
echo -e "${YELLOW}Empfehlung: Nutze das fix-locked-docs-rels.sql Script für sichere Spalten-Hinzufügung.${NC}"
echo ""

if [ "$FORCE" = true ]; then
  echo -e "${BLUE}Running drizzle-kit push (FORCE mode - GEFÄHRLICH!)...${NC}"
  pnpm exec drizzle-kit push --config=drizzle.production.config.ts --force
else
  echo -e "${BLUE}Running drizzle-kit push (interactive mode)...${NC}"
  pnpm exec drizzle-kit push --config=drizzle.production.config.ts
fi

echo ""
echo -e "${GREEN}Schema sync complete!${NC}"
echo ""
echo "Next steps:"
echo "  1. Restart Payload: pm2 restart payload"
echo "  2. Test the admin panel"
