#!/bin/bash

# ============================================================================
# Script de Déploiement Portainer - Xpeditis
# ============================================================================
# Ce script build et push les images Docker vers le registry Scaleway
# Usage: ./deploy-to-portainer.sh [backend|frontend|all]
# ============================================================================

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

# Configuration
REGISTRY="rg.fr-par.scw.cloud/weworkstudio"
TAG="preprod"

# Functions
print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}\n"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

# Check if Docker is running
check_docker() {
    print_info "Checking Docker..."
    if ! docker info > /dev/null 2>&1; then
        print_error "Docker is not running. Please start Docker Desktop."
        exit 1
    fi
    print_success "Docker is running"
}

# Build and push backend
build_backend() {
    print_header "Building Backend Image"

    cd apps/backend

    print_info "Building image: ${REGISTRY}/xpeditis-backend:${TAG}"
    docker build -t ${REGISTRY}/xpeditis-backend:${TAG} .

    print_success "Backend image built successfully"

    print_info "Pushing image to registry..."
    docker push ${REGISTRY}/xpeditis-backend:${TAG}

    print_success "Backend image pushed successfully"

    cd ../..
}

# Build and push frontend
build_frontend() {
    print_header "Building Frontend Image"

    cd apps/frontend

    print_info "Building image: ${REGISTRY}/xpeditis-frontend:${TAG}"
    docker build -t ${REGISTRY}/xpeditis-frontend:${TAG} .

    print_success "Frontend image built successfully"

    print_info "Pushing image to registry..."
    docker push ${REGISTRY}/xpeditis-frontend:${TAG}

    print_success "Frontend image pushed successfully"

    cd ../..
}

# Main script
main() {
    print_header "Xpeditis Deployment Script"

    # Check Docker
    check_docker

    # Get target from argument
    TARGET=${1:-all}

    case $TARGET in
        backend)
            build_backend
            ;;
        frontend)
            build_frontend
            ;;
        all)
            build_backend
            build_frontend
            ;;
        *)
            print_error "Invalid target: $TARGET"
            echo "Usage: $0 [backend|frontend|all]"
            exit 1
            ;;
    esac

    print_header "Deployment Summary"

    if [ "$TARGET" = "all" ] || [ "$TARGET" = "backend" ]; then
        echo -e "Backend:  ${GREEN}${REGISTRY}/xpeditis-backend:${TAG}${NC}"
    fi

    if [ "$TARGET" = "all" ] || [ "$TARGET" = "frontend" ]; then
        echo -e "Frontend: ${GREEN}${REGISTRY}/xpeditis-frontend:${TAG}${NC}"
    fi

    echo ""
    print_success "Images successfully built and pushed!"
    echo ""
    print_warning "Next Steps:"
    echo "  1. Go to Portainer: https://portainer.weworkstudio.com"
    echo "  2. Navigate to: Stacks → xpeditis-preprod"
    echo "  3. Click 'Update the stack'"
    echo "  4. Check '✅ Re-pull image and redeploy'"
    echo "  5. Click 'Update'"
    echo ""
    print_info "Documentation: DEPLOYMENT_CHECKLIST.md"
}

# Run main
main "$@"
