#!/bin/bash
# PWA Schema Setup - Run this script to set up PWA database tables

echo ""
echo "================================"
echo "PWA Schema Setup for Inventory Counter"
echo "================================"
echo ""

# Default values
USER="root"
PASSWORD=""
DATABASE="invcount"
HOST="localhost"

# Parse arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    -u|--user)
      USER="$2"
      shift 2
      ;;
    -p|--password)
      PASSWORD="$2"
      shift 2
      ;;
    -d|--database)
      DATABASE="$2"
      shift 2
      ;;
    -h|--host)
      HOST="$2"
      shift 2
      ;;
    *)
      echo "Unknown option: $1"
      echo "Usage: $0 [-u user] [-p password] [-d database] [-h host]"
      exit 1
      ;;
  esac
done

# Check if MySQL is available
if ! command -v mysql &> /dev/null; then
    echo "ERROR: MySQL is not found"
    echo "Please ensure MySQL is installed"
    exit 1
fi

echo "Connecting to MySQL and running PWA schema..."
echo ""

# Run the schema file
if [ -z "$PASSWORD" ]; then
    mysql -h "$HOST" -u "$USER" "$DATABASE" < database/pwa-schema.sql
else
    mysql -h "$HOST" -u "$USER" "-p$PASSWORD" "$DATABASE" < database/pwa-schema.sql
fi

if [ $? -ne 0 ]; then
    echo ""
    echo "ERROR: Schema setup failed"
    exit 1
fi

echo ""
echo "================================"
echo "PWA Schema Setup Complete!"
echo "================================"
echo ""
echo "Next steps:"
echo "1. Update config/config.php with VAPID keys"
echo "2. Run 'web-push generate-vapid-keys' to generate VAPID keys"
echo "3. Test offline functionality in DevTools"
echo ""
