#!/bin/bash

# Server Environment Setup Script
# This script helps you set up .env files on your deployment servers

set -e

echo "================================================"
echo "Server Environment Setup for MerchBay Admin"
echo "================================================"
echo ""

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

# Function to prompt for input with default value
prompt_input() {
    local prompt="$1"
    local default="$2"
    local value
    
    if [ -n "$default" ]; then
        read -p "$prompt [$default]: " value
        echo "${value:-$default}"
    else
        read -p "$prompt: " value
        echo "$value"
    fi
}

# Function to prompt for password (hidden input)
prompt_password() {
    local prompt="$1"
    local value
    
    read -s -p "$prompt: " value
    echo ""
    echo "$value"
}

# Choose environment
echo "Which environment are you setting up?"
echo "1) Production"
echo "2) Development"
read -p "Enter choice [1-2]: " env_choice

case $env_choice in
    1)
        ENV_TYPE="production"
        DEPLOY_DIR="/var/www/merchbay_admin"
        DEFAULT_DOMAIN="merchbay.com"
        DEFAULT_APP_URL="https://merchbay.com"
        DEFAULT_APP_ENV="production"
        DEFAULT_DB_NAME="merchbay_prod"
        ;;
    2)
        ENV_TYPE="development"
        DEPLOY_DIR="/var/www/merchbay_admin_dev"
        DEFAULT_DOMAIN="dev-admin.merchbay.app"
        DEFAULT_APP_URL="https://dev-admin.merchbay.app"
        DEFAULT_APP_ENV="staging"
        DEFAULT_DB_NAME="merchbay_dev"
        ;;
    *)
        echo -e "${RED}Invalid choice${NC}"
        exit 1
        ;;
esac

echo -e "\n${GREEN}Setting up $ENV_TYPE environment${NC}\n"

# Collect information
echo "=== Application Settings ==="
APP_ENV=$(prompt_input "APP_ENV" "$DEFAULT_APP_ENV")
APP_DEBUG=$(prompt_input "APP_DEBUG (true/false)" "false")
APP_URL=$(prompt_input "APP_URL" "$DEFAULT_APP_URL")
DOMAIN=$(prompt_input "DOMAIN" "$DEFAULT_DOMAIN")

echo -e "\n=== Database Settings ==="
DB_HOST=$(prompt_input "DB_HOST" "localhost")
DB_PORT=$(prompt_input "DB_PORT" "3306")
DB_DATABASE=$(prompt_input "DB_DATABASE" "$DEFAULT_DB_NAME")
DB_USERNAME=$(prompt_input "DB_USERNAME" "merchbay_user")
DB_PASSWORD=$(prompt_password "DB_PASSWORD")

echo -e "\n=== Server Connection ==="
SERVER_USER=$(prompt_input "SSH Username" "root")
SERVER_HOST=$(prompt_input "Server IP/Hostname" "")

if [ -z "$SERVER_HOST" ]; then
    echo -e "${RED}Error: Server hostname is required${NC}"
    exit 1
fi

# Generate .env content
ENV_CONTENT="APP_ENV=$APP_ENV
APP_DEBUG=$APP_DEBUG
APP_URL=$APP_URL
DB_HOST=$DB_HOST
DB_PORT=$DB_PORT
DB_DATABASE=$DB_DATABASE
DB_USERNAME=$DB_USERNAME
DB_PASSWORD=$DB_PASSWORD
DOMAIN=$DOMAIN"

# Show summary
echo -e "\n${YELLOW}=== Configuration Summary ===${NC}"
echo "Environment: $ENV_TYPE"
echo "Deploy Directory: $DEPLOY_DIR"
echo "Server: $SERVER_USER@$SERVER_HOST"
echo "Domain: $DOMAIN"
echo "Database: $DB_DATABASE"
echo ""

# Ask for confirmation
read -p "Deploy this configuration to the server? (y/n): " confirm

if [ "$confirm" != "y" ]; then
    echo "Aborted."
    exit 0
fi

# Deploy to server
echo -e "\n${GREEN}Deploying configuration to server...${NC}"

# Create deployment directory and .env file
ssh "$SERVER_USER@$SERVER_HOST" "mkdir -p $DEPLOY_DIR"

# Upload .env file
echo "$ENV_CONTENT" | ssh "$SERVER_USER@$SERVER_HOST" "cat > $DEPLOY_DIR/.env && chmod 600 $DEPLOY_DIR/.env"

echo -e "\n${GREEN}✓ Configuration deployed successfully!${NC}"
echo -e "\nThe .env file has been created at: ${YELLOW}$DEPLOY_DIR/.env${NC}"
echo ""

# Display Gitea secrets needed
echo -e "${YELLOW}=== Gitea Secrets Required ===${NC}"
if [ "$ENV_TYPE" = "production" ]; then
    echo "Add these secrets to your Gitea repository:"
    echo "  • PROD_DEPLOY_SSH_KEY: Your SSH private key"
    echo "  • PROD_DEPLOY_USER: $SERVER_USER"
    echo "  • PROD_DEPLOY_HOST: $SERVER_HOST"
else
    echo "Add these secrets to your Gitea repository:"
    echo "  • DEPLOY_SSH_KEY: Your SSH private key"
    echo "  • DEPLOY_USER: $SERVER_USER"
    echo "  • DEPLOY_HOST: $SERVER_HOST"
fi

echo -e "\n${GREEN}Setup complete!${NC}"
