#!/bin/bash
set -e

DEPLOY_PATH="${1:-.}"
SERVICE_NAME="${2:-ePracownik}"

echo "=== ePracownik Deployment ==="
echo "Deploy path: $DEPLOY_PATH"
echo "Service: $SERVICE_NAME"

cd "$DEPLOY_PATH"

REQUIRED_DOTNET_VERSION=$(cat dotnet-version.txt 2>/dev/null || echo "10")
echo "Required .NET version: ${REQUIRED_DOTNET_VERSION}"

install_dotnet_runtime() {
    local version=$1
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        if [[ "$ID" == "ubuntu" ]] || [[ "$ID" == "debian" ]]; then
            echo "Installing .NET ${version} runtime for Ubuntu/Debian..."
            wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O /tmp/packages-microsoft-prod.deb 2>/dev/null || true
            sudo dpkg -i /tmp/packages-microsoft-prod.deb 2>/dev/null || true
            if sudo apt-get update -qq 2>/dev/null && sudo apt-get install -y aspnetcore-runtime-${version}.0 2>/dev/null; then
                echo "Installed via apt."
            else
                echo "apt package not available, using dotnet-install.sh..."
                wget -q https://dot.net/v1/dotnet-install.sh -O /tmp/dotnet-install.sh
                chmod +x /tmp/dotnet-install.sh
                sudo /tmp/dotnet-install.sh --channel ${version}.0 --runtime aspnetcore --install-dir /usr/share/dotnet
                sudo ln -sf /usr/share/dotnet/dotnet /usr/bin/dotnet 2>/dev/null || true
            fi
        elif [[ "$ID" == "rhel" ]] || [[ "$ID" == "centos" ]] || [[ "$ID" == "rocky" ]] || [[ "$ID" == "almalinux" ]]; then
            echo "Installing .NET ${version} runtime for RHEL/CentOS..."
            if sudo dnf install -y dotnet-aspnetcore-${version} 2>/dev/null; then
                echo "Installed via dnf."
            else
                echo "dnf package not available, using dotnet-install.sh..."
                wget -q https://dot.net/v1/dotnet-install.sh -O /tmp/dotnet-install.sh
                chmod +x /tmp/dotnet-install.sh
                sudo /tmp/dotnet-install.sh --channel ${version}.0 --runtime aspnetcore --install-dir /usr/share/dotnet
                sudo ln -sf /usr/share/dotnet/dotnet /usr/bin/dotnet 2>/dev/null || true
            fi
        else
            echo "Unsupported OS: $ID. Please install .NET ${version} runtime manually."
            exit 1
        fi
    else
        echo "Cannot detect OS. Please install .NET ${version} runtime manually."
        exit 1
    fi
}

NEED_INSTALL=false
if command -v dotnet &> /dev/null; then
    INSTALLED_MAJOR=$(dotnet --list-runtimes 2>/dev/null | grep -E "Microsoft.AspNetCore.App" | sed -n 's/.*\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/p' | sort -n | tail -1)
    echo "Installed ASP.NET Core major version: ${INSTALLED_MAJOR:-none}"
    if [ -z "$INSTALLED_MAJOR" ] || [ "$INSTALLED_MAJOR" -lt "$REQUIRED_DOTNET_VERSION" ]; then
        NEED_INSTALL=true
    fi
else
    NEED_INSTALL=true
fi

if [ "$NEED_INSTALL" = true ]; then
    echo "Installing/updating .NET ${REQUIRED_DOTNET_VERSION} runtime..."
    install_dotnet_runtime "$REQUIRED_DOTNET_VERSION"
else
    echo ".NET ${REQUIRED_DOTNET_VERSION} runtime is already installed."
fi

if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
    echo "Restarting service $SERVICE_NAME..."
    sudo systemctl restart "$SERVICE_NAME"
    echo "Service restarted successfully."
elif systemctl list-unit-files 2>/dev/null | grep -q "$SERVICE_NAME"; then
    echo "Starting service $SERVICE_NAME..."
    sudo systemctl start "$SERVICE_NAME"
    echo "Service started."
else
    echo "Warning: Service '$SERVICE_NAME' not found in systemd. Ensure the service is configured."
fi

echo "=== Deployment complete ==="
