version: '3.8'

networks:
  traefik-net:
    external: true
  internal:
    internal: true

services:
  db:
    image: postgis/postgis:16-3.4
    container_name: keinasystem_db
    restart: always
    environment:
      POSTGRES_DB: keinasystem
      POSTGRES_USER: keinasystem
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U keinasystem -d keinasystem"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - internal

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.prod
    container_name: keinasystem_backend
    restart: always
    environment:
      DB_NAME: keinasystem
      DB_USER: keinasystem
      DB_PASSWORD: ${DB_PASSWORD}
      DB_HOST: db
      DB_PORT: 5432
      SECRET_KEY: ${SECRET_KEY}
      DEBUG: "False"
      ALLOWED_HOSTS: main.keinafarm.net
      CORS_ALLOWED_ORIGINS: https://main.keinafarm.net
      MAIL_API_KEY: ${MAIL_API_KEY}
      FRONTEND_URL: https://main.keinafarm.net
    depends_on:
      db:
        condition: service_healthy
    networks:
      - internal
      - traefik-net
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.keinasystem-api.rule=Host(`main.keinafarm.net`) && PathPrefix(`/api/`)"
      - "traefik.http.routers.keinasystem-api.entrypoints=websecure"
      - "traefik.http.routers.keinasystem-api.tls=true"
      - "traefik.http.routers.keinasystem-api.tls.certresolver=letsencrypt"
      - "traefik.http.routers.keinasystem-api.priority=10"
      - "traefik.http.services.keinasystem-api.loadbalancer.server.port=8000"
      - "traefik.docker.network=traefik-net"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
      args:
        NEXT_PUBLIC_API_URL: https://main.keinafarm.net
    container_name: keinasystem_frontend
    restart: always
    depends_on:
      - backend
    networks:
      - traefik-net
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.keinasystem.rule=Host(`main.keinafarm.net`)"
      - "traefik.http.routers.keinasystem.entrypoints=websecure"
      - "traefik.http.routers.keinasystem.tls=true"
      - "traefik.http.routers.keinasystem.tls.certresolver=letsencrypt"
      - "traefik.http.routers.keinasystem.priority=5"
      - "traefik.http.services.keinasystem.loadbalancer.server.port=3000"

volumes:
  postgres_data:
