#!/bin/bash
# =============================================================================
# SmartLife Monitor - AlmaLinux 9.7 Server Setup Script
# =============================================================================
# Usage: sudo bash setup-almalinux.sh
# =============================================================================

set -e

echo "=============================================="
echo " SmartLife Monitor - Server Setup"
echo " AlmaLinux 9.7 STANDARD"
echo "=============================================="

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

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}Please run as root (sudo)${NC}"
    exit 1
fi

echo -e "${GREEN}[1/10] Updating system packages...${NC}"
dnf update -y
dnf install -y epel-release

echo -e "${GREEN}[2/10] Installing essential tools...${NC}"
dnf install -y \
    git \
    curl \
    wget \
    vim \
    nano \
    htop \
    unzip \
    tar \
    net-tools \
    firewalld \
    policycoreutils-python-utils

echo -e "${GREEN}[3/10] Installing Python 3.11...${NC}"
dnf install -y python3.11 python3.11-pip python3.11-devel
alternatives --set python3 /usr/bin/python3.11
python3 --version

echo -e "${GREEN}[4/10] Installing Node.js 20 LTS...${NC}"
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash -
dnf install -y nodejs
node --version
npm --version

echo -e "${GREEN}[5/10] Installing PostgreSQL 15...${NC}"
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
dnf -qy module disable postgresql
dnf install -y postgresql15-server postgresql15
/usr/pgsql-15/bin/postgresql-15-setup initdb
systemctl enable postgresql-15
systemctl start postgresql-15

# Configure PostgreSQL
echo -e "${GREEN}Configuring PostgreSQL...${NC}"
sudo -u postgres psql << EOF
CREATE USER smartlife WITH PASSWORD 'CHANGE_THIS_PASSWORD';
CREATE DATABASE smartlife_db OWNER smartlife;
GRANT ALL PRIVILEGES ON DATABASE smartlife_db TO smartlife;
\q
EOF

# Update pg_hba.conf for md5 authentication
PG_HBA="/var/lib/pgsql/15/data/pg_hba.conf"
sed -i 's/local   all             all                                     peer/local   all             all                                     md5/' $PG_HBA
sed -i 's/host    all             all             127.0.0.1\/32            ident/host    all             all             127.0.0.1\/32            md5/' $PG_HBA
systemctl restart postgresql-15

echo -e "${GREEN}[6/10] Installing Redis 7...${NC}"
dnf install -y redis
systemctl enable redis
systemctl start redis
redis-cli ping

echo -e "${GREEN}[7/10] Installing Nginx...${NC}"
dnf install -y nginx
systemctl enable nginx

echo -e "${GREEN}[8/10] Installing Docker (optional)...${NC}"
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
systemctl enable docker
systemctl start docker
usermod -aG docker $SUDO_USER || true

echo -e "${GREEN}[9/10] Configuring Firewall...${NC}"
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-port=8000/tcp
firewall-cmd --reload

echo -e "${GREEN}[10/10] Configuring SELinux...${NC}"
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_relay 1

echo -e "${GREEN}=============================================="
echo " Setup Complete!"
echo "=============================================="
echo ""
echo " Next Steps:"
echo " 1. Change PostgreSQL password in the script"
echo " 2. Copy your application files to /var/www/smartlife"
echo " 3. Configure environment variables in .env"
echo " 4. Run: cd /var/www/smartlife && bash scripts/deploy.sh"
echo " 5. Setup SSL with: certbot --nginx -d your-domain.com"
echo ""
echo -e "${YELLOW} IMPORTANT: Remember to change default passwords!${NC}"
echo "=============================================="
