version: "3.9"

services:
  mysql:
    image: mysql:8.0
    container_name: invoice-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-invoice_app}
      MYSQL_USER: ${MYSQL_USER:-invoice}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-invoice}
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    container_name: invoice-redis
    restart: unless-stopped
    ports:
      - "6379:6379"

  backend:
    build:
      context: .
      dockerfile: infra/docker/backend.Dockerfile
    container_name: invoice-backend
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started
    env_file:
      - .env
    environment:
      DATABASE_URL: mysql://${MYSQL_USER:-invoice}:${MYSQL_PASSWORD:-invoice}@mysql:3306/${MYSQL_DATABASE:-invoice_app}
      REDIS_URL: redis://redis:6379
    ports:
      - "4000:4000"
    volumes:
      - .:/app

  frontend:
    build:
      context: .
      dockerfile: infra/docker/frontend.Dockerfile
    container_name: invoice-frontend
    depends_on:
      - backend
    environment:
      VITE_API_URL: http://localhost:4000/api
    ports:
      - "5173:5173"
    volumes:
      - .:/app

  nginx:
    image: nginx:1.27-alpine
    container_name: invoice-nginx
    depends_on:
      - frontend
      - backend
    ports:
      - "8080:80"
    volumes:
      - ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro

volumes:
  mysql_data:
