#!/bin/bash

# ================================================================
# Docker Image Build Script - Xpeditis
# ================================================================
# This script builds and optionally pushes Docker images for
# backend and frontend to a Docker registry.
#
# Usage:
#   ./build-images.sh [staging|production] [--push]
#
# Examples:
#   ./build-images.sh staging            # Build staging images only
#   ./build-images.sh production --push  # Build and push production images
# ================================================================

set -e  # Exit on error

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

# Default values
ENVIRONMENT=${1:-staging}
PUSH_IMAGES=${2:-}
REGISTRY=${DOCKER_REGISTRY:-docker.io}
REPO=${DOCKER_REPO:-xpeditis}

# Validate environment
if [[ "$ENVIRONMENT" != "staging" && "$ENVIRONMENT" != "production" ]]; then
    echo -e "${RED}Error: Environment must be 'staging' or 'production'${NC}"
    echo "Usage: $0 [staging|production] [--push]"
    exit 1
fi

# Set tags based on environment
if [[ "$ENVIRONMENT" == "staging" ]]; then
    BACKEND_TAG="staging-latest"
    FRONTEND_TAG="staging-latest"
    API_URL="https://api-staging.xpeditis.com"
    APP_URL="https://staging.xpeditis.com"
    SENTRY_ENV="staging"
else
    BACKEND_TAG="latest"
    FRONTEND_TAG="latest"
    API_URL="https://api.xpeditis.com"
    APP_URL="https://xpeditis.com"
    SENTRY_ENV="production"
fi

echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}  Building Xpeditis Docker Images${NC}"
echo -e "${BLUE}================================================${NC}"
echo -e "Environment: ${YELLOW}$ENVIRONMENT${NC}"
echo -e "Registry: ${YELLOW}$REGISTRY${NC}"
echo -e "Repository: ${YELLOW}$REPO${NC}"
echo -e "Backend Tag: ${YELLOW}$BACKEND_TAG${NC}"
echo -e "Frontend Tag: ${YELLOW}$FRONTEND_TAG${NC}"
echo -e "Push: ${YELLOW}${PUSH_IMAGES:-No}${NC}"
echo -e "${BLUE}================================================${NC}"
echo ""

# Navigate to project root
cd "$(dirname "$0")/.."

# ================================================================
# Build Backend Image
# ================================================================
echo -e "${GREEN}[1/2] Building Backend Image...${NC}"
echo "Image: $REGISTRY/$REPO/backend:$BACKEND_TAG"

docker build \
    --file apps/backend/Dockerfile \
    --tag $REGISTRY/$REPO/backend:$BACKEND_TAG \
    --tag $REGISTRY/$REPO/backend:$(date +%Y%m%d-%H%M%S) \
    --build-arg NODE_ENV=$ENVIRONMENT \
    --platform linux/amd64 \
    apps/backend/

echo -e "${GREEN}✓ Backend image built successfully${NC}"
echo ""

# ================================================================
# Build Frontend Image
# ================================================================
echo -e "${GREEN}[2/2] Building Frontend Image...${NC}"
echo "Image: $REGISTRY/$REPO/frontend:$FRONTEND_TAG"

docker build \
    --file apps/frontend/Dockerfile \
    --tag $REGISTRY/$REPO/frontend:$FRONTEND_TAG \
    --tag $REGISTRY/$REPO/frontend:$(date +%Y%m%d-%H%M%S) \
    --build-arg NEXT_PUBLIC_API_URL=$API_URL \
    --build-arg NEXT_PUBLIC_APP_URL=$APP_URL \
    --build-arg NEXT_PUBLIC_SENTRY_ENVIRONMENT=$SENTRY_ENV \
    --platform linux/amd64 \
    apps/frontend/

echo -e "${GREEN}✓ Frontend image built successfully${NC}"
echo ""

# ================================================================
# Push Images (if --push flag provided)
# ================================================================
if [[ "$PUSH_IMAGES" == "--push" ]]; then
    echo -e "${BLUE}================================================${NC}"
    echo -e "${BLUE}  Pushing Images to Registry${NC}"
    echo -e "${BLUE}================================================${NC}"

    echo -e "${YELLOW}Pushing backend image...${NC}"
    docker push $REGISTRY/$REPO/backend:$BACKEND_TAG

    echo -e "${YELLOW}Pushing frontend image...${NC}"
    docker push $REGISTRY/$REPO/frontend:$FRONTEND_TAG

    echo -e "${GREEN}✓ Images pushed successfully${NC}"
    echo ""
fi

# ================================================================
# Summary
# ================================================================
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}  Build Complete!${NC}"
echo -e "${BLUE}================================================${NC}"
echo ""
echo -e "Images built:"
echo -e "  • Backend:  ${GREEN}$REGISTRY/$REPO/backend:$BACKEND_TAG${NC}"
echo -e "  • Frontend: ${GREEN}$REGISTRY/$REPO/frontend:$FRONTEND_TAG${NC}"
echo ""

if [[ "$PUSH_IMAGES" != "--push" ]]; then
    echo -e "${YELLOW}To push images to registry, run:${NC}"
    echo -e "  $0 $ENVIRONMENT --push"
    echo ""
fi

echo -e "To test images locally:"
echo -e "  docker run -p 4000:4000 $REGISTRY/$REPO/backend:$BACKEND_TAG"
echo -e "  docker run -p 3000:3000 $REGISTRY/$REPO/frontend:$FRONTEND_TAG"
echo ""

echo -e "To deploy with Portainer:"
echo -e "  1. Login to Portainer UI"
echo -e "  2. Go to Stacks → Add Stack"
echo -e "  3. Use ${YELLOW}docker/portainer-stack-$ENVIRONMENT.yml${NC}"
echo -e "  4. Fill environment variables from ${YELLOW}docker/.env.$ENVIRONMENT.example${NC}"
echo -e "  5. Deploy!"
echo ""

echo -e "${GREEN}✓ All done!${NC}"
