#!/bin/bash

# Check Migration Status Script for ParentFlow App
# This script checks which migrations have been applied to the database
# Created: October 6, 2025

set -e

# Configuration
DATABASE_NAME="${DATABASE_NAME:-parentflow_production}"
DATABASE_USER="${DATABASE_USER:-parentflow_user}"
DATABASE_PASSWORD="${DATABASE_PASSWORD:-parentflow_secure_password_2024}"
DATABASE_HOST="${DATABASE_HOST:-localhost}"
DATABASE_PORT="${DATABASE_PORT:-5432}"

# For development
if [ "$NODE_ENV" = "development" ]; then
    DATABASE_NAME="${DATABASE_NAME:-maternal_app}"
    DATABASE_USER="${DATABASE_USER:-maternal_user}"
    DATABASE_PASSWORD="${DATABASE_PASSWORD:-maternal_dev_password_2024}"
    DATABASE_PORT="${DATABASE_PORT:-5555}"
fi

MIGRATION_DIR="$(dirname "$0")/../src/database/migrations"

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

echo "=========================================="
echo "ParentFlow Migration Status Check"
echo "=========================================="
echo "Database: $DATABASE_NAME@$DATABASE_HOST:$DATABASE_PORT"
echo ""

# Check if migration tracking table exists
echo -e "${YELLOW}Checking for migration tracking table...${NC}"
TABLE_EXISTS=$(PGPASSWORD="$DATABASE_PASSWORD" psql \
    -h "$DATABASE_HOST" \
    -p "$DATABASE_PORT" \
    -U "$DATABASE_USER" \
    -d "$DATABASE_NAME" \
    -t \
    -c "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'schema_migrations');" 2>/dev/null | xargs)

if [ "$TABLE_EXISTS" != "t" ]; then
    echo -e "${RED}✗ Migration tracking table does not exist.${NC}"
    echo "This database appears to be uninitialized or using an old migration system."
    echo ""
    echo "To initialize migrations, run:"
    echo "  ./scripts/master-migration.sh"
    exit 1
fi

echo -e "${GREEN}✓ Migration tracking table found${NC}"
echo ""

# Get list of applied migrations
echo -e "${BLUE}Applied Migrations:${NC}"
echo "----------------------------------------"

PGPASSWORD="$DATABASE_PASSWORD" psql \
    -h "$DATABASE_HOST" \
    -p "$DATABASE_PORT" \
    -U "$DATABASE_USER" \
    -d "$DATABASE_NAME" \
    -c "SELECT version, filename, applied_at::date as applied_date, description
        FROM schema_migrations
        ORDER BY version;" \
    --pset border=2

echo ""

# Count migrations
APPLIED_COUNT=$(PGPASSWORD="$DATABASE_PASSWORD" psql \
    -h "$DATABASE_HOST" \
    -p "$DATABASE_PORT" \
    -U "$DATABASE_USER" \
    -d "$DATABASE_NAME" \
    -t \
    -c "SELECT COUNT(*) FROM schema_migrations;" | xargs)

# Count migration files in directory
if [ -d "$MIGRATION_DIR" ]; then
    FILE_COUNT=$(ls -1 "$MIGRATION_DIR"/*.sql 2>/dev/null | wc -l)
else
    FILE_COUNT=0
fi

echo "=========================================="
echo "Summary:"
echo "=========================================="
echo -e "${GREEN}Applied migrations: $APPLIED_COUNT${NC}"
echo -e "${BLUE}Migration files available: $FILE_COUNT${NC}"

if [ "$APPLIED_COUNT" -lt "$FILE_COUNT" ]; then
    PENDING=$((FILE_COUNT - APPLIED_COUNT))
    echo -e "${YELLOW}Pending migrations: $PENDING${NC}"
    echo ""
    echo "Run './scripts/master-migration.sh' to apply pending migrations."
else
    echo -e "${GREEN}✓ All migrations are up to date!${NC}"
fi

echo ""

# Show latest migration
echo -e "${BLUE}Latest Migration:${NC}"
PGPASSWORD="$DATABASE_PASSWORD" psql \
    -h "$DATABASE_HOST" \
    -p "$DATABASE_PORT" \
    -U "$DATABASE_USER" \
    -d "$DATABASE_NAME" \
    -t \
    -c "SELECT version || ' - ' || filename || ' (Applied: ' || applied_at::date || ')'
        FROM schema_migrations
        ORDER BY version DESC
        LIMIT 1;"