version: '3.8'

services:
  trackeep-frontend:
    image: ghcr.io/dvorinka/trackeep/frontend:latest
    ports:
      - "80:80"
      - "443:443"
    environment:
      - NODE_ENV=production
      - VITE_DEMO_MODE=${VITE_DEMO_MODE}
      - VITE_APP_VERSION=${APP_VERSION:-1.0.0}
    depends_on:
      - trackeep-backend
    restart: unless-stopped
    networks:
      - trackeep-network

  trackeep-backend:
    image: ghcr.io/dvorinka/trackeep/backend:latest
    ports:
      - "8080:8080"
    env_file:
      - .env
    environment:
      - APP_VERSION=${APP_VERSION:-1.0.0}
    volumes:
      - ./data:/data
      - ./uploads:/app/uploads
      - ./logs:/app/logs
      - /var/run/docker.sock:/var/run/docker.sock  # Docker socket for updates
    restart: unless-stopped
    networks:
      - trackeep-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-trackeep}
      POSTGRES_USER: ${POSTGRES_USER:-trackeep}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./backups:/backups
    restart: unless-stopped
    networks:
      - trackeep-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-trackeep} -d ${POSTGRES_DB:-trackeep}"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    restart: unless-stopped
    networks:
      - trackeep-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  youtube-scraper:
    build:
      context: ./backend
      dockerfile: Dockerfile.youtube-scraper
    ports:
      - "7857:7857"
    restart: unless-stopped
    networks:
      - trackeep-network
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:7857/ || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    profiles:
      - demo

  # Backup service
  backup:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-trackeep}
      POSTGRES_USER: ${POSTGRES_USER:-trackeep}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_HOST: postgres
    volumes:
      - ./backups:/backups
      - ./scripts/backup.sh:/backup.sh
    command: sh -c "chmod +x /backup.sh && crond -f"
    restart: unless-stopped
    networks:
      - trackeep-network
    depends_on:
      - postgres

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

networks:
  trackeep-network:
    driver: bridge
