#!/bin/bash

# ============================================================
# COMARNET DEPLOYMENT SCRIPT FOR cPANEL
# ============================================================
# This script automates the deployment process
# Run this on your cPanel server after uploading files
# ============================================================

set -e  # Exit on error

echo "=================================================="
echo "Comarnet Deployment Script"
echo "=================================================="
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if running from correct directory
if [ ! -f "package.json" ]; then
    echo -e "${RED}Error: package.json not found!${NC}"
    echo "Please run this script from the application root directory"
    exit 1
fi

echo -e "${YELLOW}Step 1: Checking Node.js installation...${NC}"
if ! command -v node &> /dev/null; then
    echo -e "${RED}Error: Node.js is not installed${NC}"
    echo "Please install Node.js via cPanel or contact your hosting provider"
    exit 1
fi
echo -e "${GREEN}✓ Node.js $(node -v) found${NC}"
echo ""

echo -e "${YELLOW}Step 2: Installing dependencies...${NC}"
if command -v pnpm &> /dev/null; then
    pnpm install
else
    npm install
fi
echo -e "${GREEN}✓ Dependencies installed${NC}"
echo ""

echo -e "${YELLOW}Step 3: Checking .env.production file...${NC}"
if [ ! -f ".env.production" ]; then
    echo -e "${RED}Error: .env.production file not found!${NC}"
    echo "Please create .env.production from .env.production.example"
    echo "Run: cp .env.production.example .env.production"
    echo "Then edit .env.production with your database credentials"
    exit 1
fi
echo -e "${GREEN}✓ .env.production found${NC}"
echo ""

echo -e "${YELLOW}Step 4: Running database migrations...${NC}"
if npm run db:push; then
    echo -e "${GREEN}✓ Database migrations completed${NC}"
else
    echo -e "${RED}Warning: Database migrations failed${NC}"
    echo "Please check your database credentials in .env.production"
    echo "You may need to run migrations manually"
fi
echo ""

echo -e "${YELLOW}Step 5: Building production files...${NC}"
npm run build
echo -e "${GREEN}✓ Production build completed${NC}"
echo ""

echo -e "${YELLOW}Step 6: Verifying build output...${NC}"
if [ -d "dist" ]; then
    echo -e "${GREEN}✓ dist/ folder found${NC}"
    if [ -f "dist/index.js" ]; then
        echo -e "${GREEN}✓ dist/index.js found${NC}"
    else
        echo -e "${RED}Error: dist/index.js not found${NC}"
        exit 1
    fi
else
    echo -e "${RED}Error: dist/ folder not found${NC}"
    exit 1
fi
echo ""

echo -e "${GREEN}=================================================="
echo "Deployment preparation completed successfully!"
echo "==================================================${NC}"
echo ""
echo "Next steps:"
echo "1. In cPanel, go to 'Setup Node.js App'"
echo "2. Create a new application with:"
echo "   - Application root: $(pwd)"
echo "   - Startup file: dist/index.js"
echo "   - Domain: comarnet.com"
echo "3. Click 'Create' and then 'Restart'"
echo ""
echo "Then visit https://comarnet.com to verify"
echo ""
