version: '3.8'

services:
  # 1. Nginx Gateway Reverse Proxy
  gateway:
    build:
      context: ./docker/nginx   # Build from our custom Dockerfile
    container_name: gis_gateway
    ports:
      - "${HOST_PORT_GATEWAY:-80}:80"
    depends_on:
      - php-app
      - next-app
    networks:
      - gis-network

  # 2. PHP + Apache Web App (for Root Portal, SPBU, Parsil, and Chloropleth)
  php-app:
    build: ./docker/php
    container_name: gis_php_app
    volumes:
      - .:/var/www/html
    environment:
      - DB_HOST=db
      - DB_DATABASE=spbu_db
      - DB_USERNAME=root
      - DB_PASSWORD=rootpassword
    depends_on:
      - db
    networks:
      - gis-network

  # 3. Next.js App Service
  next-app:
    build: ./docker/nextjs
    container_name: gis_next_app
    working_dir: /app
    volumes:
      - ./sistem-km-next:/app
      - /app/node_modules # Anonymous volume to isolate node_modules from host machine
      - /app/.next       # Anonymous volume for nextjs build artifacts
    environment:
      - NEXT_PUBLIC_SUPABASE_URL=https://gzpvygrsfdtzykyqbnes.supabase.co
      - NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd6cHZ5Z3JzZmR0enlreXFibmVzIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzkxNTUxNTIsImV4cCI6MjA5NDczMTE1Mn0.bP4qLY6PVT9UplSxB276tZ5HWClIkiuZ_-Yx3IlL8Dw
      - SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd6cHZ5Z3JzZmR0enlreXFibmVzIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3OTE1NTE1MiwiZXhwIjoyMDk0NzMxMTUyfQ.H8rDKBGeagZnF22lFZwlsqPbDP6_kDurNpt8ZRvAigc
      - PORT=3000
      - HOSTNAME=0.0.0.0
    networks:
      - gis-network

  # 4. MySQL Database Service
  db:
    image: mysql:8.0
    container_name: gis_db
    command: --default-authentication-plugin=mysql_native_password
    # No host port mapping - DB is only accessible internally via gis-network
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
      - MYSQL_DATABASE=spbu_db
    volumes:
      - db-data:/var/lib/mysql
      - ./docker/db/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    networks:
      - gis-network

  # 5. phpMyAdmin (Database Management Web UI)
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: gis_phpmyadmin
    # No host port mapping - Coolify's Traefik proxy routes traffic via the assigned domain
    environment:
      - PMA_HOST=db
      - PMA_USER=root
      - PMA_PASSWORD=rootpassword
    depends_on:
      - db
    networks:
      - gis-network

volumes:
  db-data:

networks:
  gis-network:
    driver: bridge
