#!/bin/bash
set -e

echo "=========================================="
echo "Cleaning polaris-wvp Docker images"
echo "=========================================="

# 停止并删除 polaris-wvp 容器
echo ""
echo "Step 1: Stopping and removing polaris-wvp container..."
docker compose stop polaris-wvp 2>/dev/null || echo "Container not running"
docker compose rm -f polaris-wvp 2>/dev/null || echo "Container not found"

# 删除 polaris-wvp 相关的镜像
echo ""
echo "Step 2: Removing polaris-wvp images..."

# 删除 docker-polaris-wvp 镜像（docker compose 自动生成的）
if docker images | grep -q "docker-polaris-wvp"; then
    echo "Removing docker-polaris-wvp images..."
    docker rmi $(docker images | grep "docker-polaris-wvp" | awk '{print $3}') 2>/dev/null || echo "Failed to remove some images"
else
    echo "No docker-polaris-wvp images found"
fi

# 删除 polaris-wvp 镜像
if docker images | grep -q "^polaris-wvp "; then
    echo "Removing polaris-wvp images..."
    docker rmi $(docker images | grep "^polaris-wvp " | awk '{print $3}') 2>/dev/null || echo "Failed to remove some images"
else
    echo "No polaris-wvp images found"
fi

# 清理悬空镜像（dangling images）
echo ""
echo "Step 3: Cleaning up dangling images..."
docker image prune -f

echo ""
echo "=========================================="
echo "Cleanup completed!"
echo "=========================================="
echo ""
echo "Remaining polaris-wvp related images:"
docker images | grep -E "polaris-wvp|docker-polaris-wvp" || echo "None"

