services:
  mysql:
    image: mysql:8.0
    container_name: todo-mysql-server
    restart: always
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root123}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-todo_agileboot_pure}
      MYSQL_USER: ${MYSQL_APP_USERNAME:-todo_app}
      MYSQL_PASSWORD: ${MYSQL_APP_PASSWORD:-todo_app123}
    ports:
      - "${MYSQL_PORT:-3306}:3306"
    volumes:
      - todo_mysql_data:/var/lib/mysql
      - ./backend/sql/agileboot.sql:/docker-entrypoint-initdb.d/01-agileboot.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -uroot -p$${MYSQL_ROOT_PASSWORD} --silent"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - todo-network

  redis:
    image: redis:7.2-alpine
    container_name: todo-redis-server
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD:-redis123}
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redis123}
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - todo_redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a $${REDIS_PASSWORD} ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s
    networks:
      - todo-network

  app:
    profiles:
      - prod
    build:
      context: ./backend
      dockerfile: Dockerfile
    image: simple-todo-backend:latest
    container_name: todo-backend
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      SPRING_PROFILES_ACTIVE: basic,prod
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_DATABASE: ${MYSQL_DATABASE:-todo_agileboot_pure}
      MYSQL_USERNAME: ${MYSQL_APP_USERNAME:-todo_app}
      MYSQL_PASSWORD: ${MYSQL_APP_PASSWORD:-todo_app123}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redis123}
      JAVA_OPTS: ${JAVA_OPTS:-}
    ports:
      - "${APP_PORT:-8080}:8080"
    networks:
      - todo-network

  web:
    profiles:
      - prod
    build:
      context: ./frontend
      dockerfile: web/Dockerfile
    image: simple-todo-web:latest
    container_name: todo-web
    restart: always
    depends_on:
      app:
        condition: service_started
    ports:
      - "${WEB_PORT:-80}:80"
    networks:
      - todo-network

volumes:
  todo_mysql_data:
  todo_redis_data:

networks:
  todo-network:
    driver: bridge
