#!/bin/bash

# cPanel Deployment Script for Schematics Backend
echo "Starting cPanel deployment..."

# Check if we're in the right directory
if [ ! -f "package.json" ]; then
    echo "Error: package.json not found. Please run this script from the project root."
    exit 1
fi

# Install dependencies
echo "Installing dependencies..."
npm install

# Generate Prisma client
echo "Generating Prisma client..."
npx prisma generate

# Verify Prisma client generation
if [ ! -d "node_modules/@prisma/client" ] && [ ! -d "src/generated/prisma" ]; then
    echo "Error: Prisma client was not generated properly"
    exit 1
fi

# Build the project
echo "Building project..."
npm run build

# Verify build
if [ ! -f "dist/server.js" ]; then
    echo "Error: Build failed - dist/server.js not found"
    exit 1
fi

# Copy necessary files for production
echo "Copying production files..."
cp -r prisma dist/
cp package.json dist/
cp package-lock.json dist/ 2>/dev/null || true

# Create production package.json
cat > dist/package.json << EOF
{
  "name": "schematics-backend",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "@prisma/client": "^6.15.0",
    "prisma": "^6.15.0"
  }
}
EOF

echo "Deployment preparation complete!"
echo "Files ready for cPanel upload in the 'dist' directory"
