#!/bin/bash
# Production-Skript für Image-Uploader Production Environment

set -e

# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== Image-Uploader Production Environment ===${NC}"
echo -e "${BLUE}Using docker/prod/docker-compose.yml${NC}"
echo

# Menü anzeigen
echo -e "${BLUE}Wähle eine Option:${NC}"
echo "1) Production Container starten"
echo "2) Production Images bauen"
echo "3) Production Images zur Registry pushen"
echo "4) Container neu bauen und starten"
echo "5) Production Container stoppen"
echo "6) Frontend Container Shell öffnen"
echo "7) Backend Container Shell öffnen"
echo "8) Frontend Logs anzeigen"
echo "9) Backend Logs anzeigen"
echo "10) Alle Logs anzeigen"
echo "11) Container Status anzeigen"
echo "12) Upload-Verzeichnis anzeigen"
echo "13) Production Container stoppen/löschen inkl Volumes"
echo "0) Beenden"
echo

read -p "Deine Wahl (0-13): " choice

case $choice in
    1)
        echo -e "${GREEN}Starte Production Container...${NC}"
        docker compose -f docker/prod/docker-compose.yml up -d
        echo -e "${GREEN}Container gestartet!${NC}"
        echo -e "${BLUE}Frontend: http://localhost${NC}"
        echo -e "${BLUE}Backend:  http://localhost:5000${NC}"
        echo -e "${BLUE}Slideshow: http://localhost/slideshow${NC}"
        echo -e "${BLUE}Logs verfolgen mit: docker compose -f docker/prod/docker-compose.yml logs -f${NC}"
        ;;
    2)
        echo -e "${GREEN}Baue Production Images...${NC}"
        echo -e "${BLUE}Baue mit docker/prod/docker-compose.yml...${NC}"
        docker compose -f docker/prod/docker-compose.yml build
        echo -e "${GREEN}Production Images erfolgreich gebaut!${NC}"
        ;;
    3)
        echo -e "${GREEN}Pushe Production Images zur Registry...${NC}"
        docker compose -f docker/prod/docker-compose.yml push
        echo -e "${GREEN}Production Images erfolgreich gepusht!${NC}"
        ;;
    4)
        echo -e "${GREEN}Baue Container neu...${NC}"
        docker compose -f docker/prod/docker-compose.yml down
        docker compose -f docker/prod/docker-compose.yml build --no-cache 
        docker compose -f docker/prod/docker-compose.yml up -d
        echo -e "${GREEN}Container neu gebaut und gestartet!${NC}"
        echo -e "${BLUE}Frontend: http://localhost${NC}"
        echo -e "${BLUE}Slideshow: http://localhost/slideshow${NC}"
        ;;
    5)
        echo -e "${YELLOW}Stoppe Container...${NC}"
        docker compose -f docker/prod/docker-compose.yml down
        echo -e "${GREEN}Container gestoppt!${NC}"
        ;;
    6)
        echo -e "${GREEN}Öffne Frontend Container Shell...${NC}"
        docker compose -f docker/prod/docker-compose.yml exec frontend bash
        ;;
    7)
        echo -e "${GREEN}Öffne Backend Container Shell...${NC}"
        docker compose -f docker/prod/docker-compose.yml exec backend bash
        ;;
    8)
        echo -e "${GREEN}Zeige Frontend Logs...${NC}"
        docker compose -f docker/prod/docker-compose.yml logs -f frontend
        ;;
    9)
        echo -e "${GREEN}Zeige Backend Logs...${NC}"
        docker compose -f docker/prod/docker-compose.yml logs -f backend
        ;;
    10)
        echo -e "${GREEN}Zeige alle Logs...${NC}"
        docker compose -f docker/prod/docker-compose.yml logs -f
        ;;
    11)
        echo -e "${GREEN}Container Status:${NC}"
        docker compose -f docker/prod/docker-compose.yml ps
        echo
        echo -e "${BLUE}Detaillierte Informationen:${NC}"
        docker images | grep "image-uploader" || echo "Keine lokalen Images gefunden"
        ;;
    12)
        echo -e "${GREEN}Upload-Verzeichnis Inhalt:${NC}"
        if docker compose -f docker/prod/docker-compose.yml ps -q backend > /dev/null 2>&1; then
            echo -e "${BLUE}Hochgeladene Bilder (data/images):${NC}"
            docker compose -f docker/prod/docker-compose.yml exec backend ls -la /usr/src/app/src/data/ || echo "Upload-Verzeichnis ist leer"
        else
            echo -e "${YELLOW}Backend Container ist nicht gestartet.${NC}"
        fi
        ;;
    13)
        echo -e "${YELLOW}Stoppe und lösche Container inkl. Volumes...${NC}"
        docker compose -f docker/prod/docker-compose.yml down -v
        echo -e "${GREEN}Container und Volumes gelöscht!${NC}"
        ;;
    0)
        echo -e "${GREEN}Auf Wiedersehen!${NC}"
        exit 0
        ;;
    *)
        echo -e "${RED}Ungültige Option! Bitte wähle 0-13.${NC}"
        exit 1
        ;;
esac