#!/bin/bash

# Integration Hub - Deploy Script for cPanel

echo "=================================================="
echo "🚀 Integration Hub - Deploy Script"
echo "=================================================="

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

# Check if running from correct directory
if [ ! -f "artisan" ]; then
    echo -e "${RED}❌ Error: artisan file not found. Run this script from Laravel root directory.${NC}"
    exit 1
fi

echo -e "${YELLOW}1. Setting up environment variables...${NC}"
if [ -f ".env.production" ]; then
    cp .env.production .env
    echo -e "${GREEN}✓ .env configured${NC}"
else
    echo -e "${RED}❌ .env.production not found${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}2. Installing composer dependencies...${NC}"
composer install --no-dev --optimize-autoloader
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Dependencies installed${NC}"
else
    echo -e "${RED}❌ Composer install failed${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}3. Running database migrations...${NC}"
php artisan migrate --force
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Migrations completed${NC}"
else
    echo -e "${RED}❌ Migrations failed${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}4. Seeding database...${NC}"
php artisan db:seed --force
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Database seeded${NC}"
else
    echo -e "${RED}⚠️  Seeding had issues (may be normal if already seeded)${NC}"
fi

echo ""
echo -e "${YELLOW}5. Generating JWT secret...${NC}"
php artisan jwt:secret --force
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ JWT secret generated${NC}"
else
    echo -e "${RED}⚠️  JWT secret generation skipped${NC}"
fi

echo ""
echo -e "${YELLOW}6. Generating application key...${NC}"
php artisan key:generate --force
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ App key generated${NC}"
else
    echo -e "${RED}⚠️  App key generation skipped${NC}"
fi

echo ""
echo -e "${YELLOW}7. Caching configuration...${NC}"
php artisan config:cache
php artisan route:cache
php artisan view:cache
echo -e "${GREEN}✓ Configuration cached${NC}"

echo ""
echo -e "${YELLOW}8. Setting file permissions...${NC}"
chmod -R 775 storage bootstrap/cache
echo -e "${GREEN}✓ Permissions set${NC}"

echo ""
echo -e "${YELLOW}9. Clearing application cache...${NC}"
php artisan cache:clear
php artisan view:clear
echo -e "${GREEN}✓ Cache cleared${NC}"

echo ""
echo -e "${YELLOW}10. Running health check...${NC}"
php artisan tinker --execute="echo 'Health check: OK';"
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Health check passed${NC}"
else
    echo -e "${YELLOW}⚠️  Health check skipped${NC}"
fi

echo ""
echo "=================================================="
echo -e "${GREEN}✅ DEPLOYMENT COMPLETED SUCCESSFULLY!${NC}"
echo "=================================================="
echo ""
echo "📝 Next steps:"
echo "1. Access your API: https://nucleoapi.eunecto.com.br"
echo "2. Test login: POST /api/login"
echo "3. Default credentials:"
echo "   Email:    admin@integration-hub.local"
echo "   Password: senha123"
echo ""
echo "⚠️  IMPORTANT: Change the default password immediately!"
echo ""
