services:
  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: rezepte-mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: rezepte_klaus
      MYSQL_USER: rezepte_user
      MYSQL_PASSWORD: rezepte_pass
    ports:
      - "3307:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./sql-init:/docker-entrypoint-initdb.d
    networks:
      - rezepte-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  # Backend API
  backend:
    build:
      context: ./nodejs-version/backend
      dockerfile: Dockerfile
    container_name: rezepte-backend
    environment:
      NODE_ENV: production
      PORT: 3001
      DATABASE_URL: mysql://rezepte_user:rezepte_pass@mysql:3306/rezepte_klaus
      JWT_SECRET: your-super-secret-jwt-key-change-in-production
      UPLOAD_PATH: /app/uploads
      MAX_FILE_SIZE: 5242880
      CORS_ORIGIN: http://localhost:3000
    ports:
      - "3001:3001"
    volumes:
      - uploads_data:/app/uploads
      - ./uploads:/app/legacy-uploads:ro  # Mount existing uploads as read-only
    networks:
      - rezepte-network
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Frontend Application
  frontend:
    build:
      context: ./nodejs-version/frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: http://localhost:3001/api
    container_name: rezepte-frontend
    ports:
      - "3000:80"
    networks:
      - rezepte-network
    depends_on:
      - backend
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Legacy PHP Application (optional)
  php-app:
    build: .
    container_name: rezepte-php-legacy
    ports:
      - "8082:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - mysql
    networks:
      - rezepte-network
    environment:
      DB_HOST: mysql
      DB_NAME: rezepte_klaus
      DB_USER: rezepte_user
      DB_PASS: rezepte_pass
    profiles:
      - legacy

  # phpMyAdmin
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: rezepte-phpmyadmin
    ports:
      - "8083:80"
    environment:
      PMA_HOST: mysql
      PMA_USER: rezepte_user
      PMA_PASSWORD: rezepte_pass
      MYSQL_ROOT_PASSWORD: rootpassword
    depends_on:
      - mysql
    networks:
      - rezepte-network
    profiles:
      - admin

# Networks
networks:
  rezepte-network:
    driver: bridge

# Volumes for persistent data
volumes:
  mysql_data:
    driver: local
  uploads_data:
    driver: local