version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: sacco_postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: sacco_user
      POSTGRES_PASSWORD: sacco_password
      POSTGRES_DB: sacco_db
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - '5432:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U sacco_user -d sacco_db']
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - sacco_network

  # Backend API (NestJS)
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: sacco_backend
    restart: unless-stopped
    ports:
      - '3001:3001'
    environment:
      DATABASE_URL: 'postgresql://sacco_user:sacco_password@postgres:5432/sacco_db?schema=public'
      JWT_SECRET: 'your-super-secret-jwt-key-change-in-production'
      JWT_EXPIRES_IN: '15m'
      JWT_REFRESH_SECRET: 'your-super-secret-refresh-key-change-in-production'
      JWT_REFRESH_EXPIRES_IN: '7d'
      PORT: 3001
      NODE_ENV: 'development'
      CORS_ORIGIN: 'http://localhost:3000'
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./backend:/app
      - /app/node_modules
    networks:
      - sacco_network
    command: npm run start:dev

  # Frontend (Next.js)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: sacco_frontend
    restart: unless-stopped
    ports:
      - '3000:3000'
    environment:
      NEXT_PUBLIC_API_URL: 'http://localhost:3001/api'
      NEXT_PUBLIC_APP_NAME: 'Honeyguide SACCO'
    depends_on:
      - backend
    volumes:
      - ./frontend:/app
      - /app/node_modules
      - /app/.next
    networks:
      - sacco_network
    command: npm run dev

  # pgAdmin (Optional - for database management)
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: sacco_pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@honeyguide.com
      PGADMIN_DEFAULT_PASSWORD: admin123
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
    ports:
      - '5050:80'
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      - postgres
    networks:
      - sacco_network
    profiles:
      - tools

networks:
  sacco_network:
    driver: bridge

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local
