#!/bin/bash
set -e

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

echo -e "${BLUE}╔════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║         Dyolink - Build & Push        ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════╝${NC}"

# Load environment
if [ -f .env ]; then
    source .env
    echo -e "${GREEN}✓ Loaded .env${NC}"
fi

# Get version
read -p "Enter version tag (e.g., v1.0.0, default: latest): " TAG
TAG=${TAG:-latest}
echo -e "${YELLOW}Using tag: ${TAG}${NC}"

# Get Docker Hub username
read -p "Docker Hub username [${DOCKER_USERNAME:-yourdockerhub}]: " USERNAME
USERNAME=${USERNAME:-${DOCKER_USERNAME:-yourdockerhub}}

# Check Docker
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}✗ Docker is not running!${NC}"
    exit 1
fi

# Login
echo -e "${YELLOW}Logging into Docker Hub...${NC}"
docker login --username $USERNAME

echo -e "\n${GREEN}=== Building Images ===${NC}"

# Build backend
echo -e "${YELLOW}Building backend...${NC}"
docker build -t $USERNAME/dyolink-backend:$TAG -t $USERNAME/dyolink-backend:latest ../backend
echo -e "${GREEN}✓ Backend built${NC}"

# Build frontend
echo -e "${YELLOW}Building frontend...${NC}"
docker build -t $USERNAME/dyolink-frontend:$TAG -t $USERNAME/dyolink-frontend:latest ../frontend
echo -e "${GREEN}✓ Frontend built${NC}"

echo -e "\n${GREEN}=== Pushing to Docker Hub ===${NC}"

# Push backend
echo -e "${YELLOW}Pushing backend...${NC}"
docker push $USERNAME/dyolink-backend:$TAG
docker push $USERNAME/dyolink-backend:latest
echo -e "${GREEN}✓ Backend pushed${NC}"

# Push frontend
echo -e "${YELLOW}Pushing frontend...${NC}"
docker push $USERNAME/dyolink-frontend:$TAG
docker push $USERNAME/dyolink-frontend:latest
echo -e "${GREEN}✓ Frontend pushed${NC}"

# Create deployment package
echo -e "\n${YELLOW}Creating deployment package...${NC}"
DEPLOY_DIR="dyolink-deploy-$TAG"
mkdir -p $DEPLOY_DIR
cp docker-compose.prod.yml $DEPLOY_DIR/
cp -r nginx/nginx.conf $DEPLOY_DIR/
cp database/init.sql $DEPLOY_DIR/
cp scripts/deploy.sh $DEPLOY_DIR/
cp .env.example $DEPLOY_DIR/

# Create env templates
echo "# Database" > $DEPLOY_DIR/database.env.example
echo "POSTGRES_USER=dyolink_user" >> $DEPLOY_DIR/database.env.example
echo "POSTGRES_PASSWORD=CHANGE_ME" >> $DEPLOY_DIR/database.env.example
echo "POSTGRES_DB=dyolink_db" >> $DEPLOY_DIR/database.env.example

echo "# Backend" > $DEPLOY_DIR/backend.env.example
echo "NODE_ENV=production" >> $DEPLOY_DIR/backend.env.example
echo "JWT_SECRET=CHANGE_ME_32_CHARS_MINIMUM" >> $DEPLOY_DIR/backend.env.example
echo "DATABASE_URL=postgresql://\${POSTGRES_USER}:\${POSTGRES_PASSWORD}@postgres:5432/\${POSTGRES_DB}" >> $DEPLOY_DIR/backend.env.example
echo "CORS_ORIGIN=https://dyolink.com" >> $DEPLOY_DIR/backend.env.example
echo "FRONTEND_URL=https://dyolink.com" >> $DEPLOY_DIR/backend.env.example
echo "SMS_IR_API_KEY=CHANGE_ME_SMS_IR_API_KEY" >> $DEPLOY_DIR/backend.env.example
echo "SMS_IR_TEMPLATE_ID=123456" >> $DEPLOY_DIR/backend.env.example

echo "# Frontend" > $DEPLOY_DIR/frontend.env.example
echo "NEXT_PUBLIC_API_URL=/api" >> $DEPLOY_DIR/frontend.env.example
echo "NEXT_PUBLIC_APP_NAME=Dyolink" >> $DEPLOY_DIR/frontend.env.example
echo "NEXT_PUBLIC_APP_URL=https://dyolink.com" >> $DEPLOY_DIR/frontend.env.example

# Package
tar -czf $DEPLOY_DIR.tar.gz $DEPLOY_DIR/
rm -rf $DEPLOY_DIR
echo -e "${GREEN}✓ Created: ${DEPLOY_DIR}.tar.gz${NC}"

echo -e "\n${BLUE}╔════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║              Build Complete!            ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════╝${NC}"
echo -e "${GREEN}Pushed:${NC}"
echo -e "  • ${USERNAME}/dyolink-backend:${TAG}"
echo -e "  • ${USERNAME}/dyolink-frontend:${TAG}"
echo -e "\n${YELLOW}Next: scp ${DEPLOY_DIR}.tar.gz user@server:/tmp/${NC}"