services:
  recipe-frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    image: recipe-frontend:local
    container_name: recipe-frontend
    restart: unless-stopped
    environment:
      NODE_ENV: "production"
      HOSTNAME: "0.0.0.0"
      PORT: "3000"
      NEXT_PUBLIC_APP_URL: "${NEXT_PUBLIC_APP_URL}"
      NEXT_PUBLIC_API_URL: "${NEXT_PUBLIC_API_URL}"
      NEXT_PUBLIC_API_URL_INTERNAL: "http://recipe-api:8080"
      AUTH_SECRET: "${AUTH_SECRET}"
      AUTH_URL: "${NEXT_PUBLIC_APP_URL}"
      MISTRAL_API_KEY: "${MISTRAL_API_KEY:-}"
    volumes:
      - recipe_images:/app/public/images
    depends_on:
      recipe-api:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3000 >/dev/null 2>&1 || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 5
      start_period: 40s
    networks:
      - proxy
      - recipe-internal

  recipe-api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    image: recipe-api:local
    container_name: recipe-api
    restart: unless-stopped
    environment:
      NODE_ENV: "production"
      DATABASE_URL: "mysql://root:${MARIADB_ROOT_PASSWORD}@recipe-db:3306/${MARIADB_DATABASE}"
      MISTRAL_API_KEY: "${MISTRAL_API_KEY:-}"
      JWT_SECRET: "${JWT_SECRET}"
      ALLOWED_ORIGIN: "${NEXT_PUBLIC_APP_URL}"
      ADMIN_NADMIN_PASSWORD: "${ADMIN_NADMIN_PASSWORD}"
      ADMIN_PADMIN_PASSWORD: "${ADMIN_PADMIN_PASSWORD}"
      SEED_USER1_PASSWORD: "${SEED_USER1_PASSWORD}"
      SEED_USER2_PASSWORD: "${SEED_USER2_PASSWORD}"
      IMPORTER_SERVICE_URL: "http://importer-api:3001"
    volumes:
      - recipe_images:/app/recipe-images
    depends_on:
      recipe-db:
        condition: service_healthy
      importer-api:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/api/health >/dev/null 2>&1 || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 10
      start_period: 40s
    networks:
      - proxy
      - recipe-internal

  importer-api:
    build:
      context: ../microservice-importer
      dockerfile: backend/Dockerfile
    image: recipe-importer-api:local
    pull_policy: never
    container_name: importer-api
    restart: unless-stopped
    environment:
      NODE_ENV: "production"
      PORT: "3001"
      MISTRAL_API_KEY: "${MISTRAL_API_KEY:-}"
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3001/api/health >/dev/null 2>&1 || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 5
      start_period: 30s
    networks:
      - recipe-internal

  recipe-db:
    image: mariadb:11
    container_name: recipe-db
    restart: unless-stopped
    environment:
      MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD}
      MARIADB_DATABASE: ${MARIADB_DATABASE}
      MARIADB_USER: ${MARIADB_USER}
      MARIADB_PASSWORD: ${MARIADB_PASSWORD}
    volumes:
      - recipe_db_data:/var/lib/mysql
      - ./db/init:/docker-entrypoint-initdb.d
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
    healthcheck:
      test: ["CMD-SHELL", "mariadb-admin ping -h 127.0.0.1 -uroot -p$$MARIADB_ROOT_PASSWORD >/dev/null 2>&1 || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - recipe-internal

volumes:
  recipe_db_data:
  recipe_images:

networks:
  proxy:
    external: true
  recipe-internal:
    driver: bridge
