# ColaFlow Development Environment
# Docker Compose configuration for local development and testing

version: '3.8'

services:
  # PostgreSQL 16 - Primary Database
  postgres:
    image: postgres:16-alpine
    container_name: colaflow-postgres
    environment:
      POSTGRES_DB: colaflow
      POSTGRES_USER: colaflow
      POSTGRES_PASSWORD: colaflow_dev_password
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/01-init-db.sql:ro
      - ./scripts/seed-data.sql:/docker-entrypoint-initdb.d/02-seed-data.sql:ro
    networks:
      - colaflow-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U colaflow -d colaflow"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped

  # Redis 7 - Cache and Session Store
  redis:
    image: redis:7-alpine
    container_name: colaflow-redis
    command: redis-server --appendonly yes --requirepass colaflow_redis_password
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - colaflow-network
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    restart: unless-stopped

  # ColaFlow Backend API (.NET 9)
  backend:
    build:
      context: ./colaflow-api
      dockerfile: Dockerfile
    container_name: colaflow-api
    ports:
      - "5000:8080"
      - "5001:8081"
    environment:
      # ASP.NET Core
      ASPNETCORE_ENVIRONMENT: Development
      ASPNETCORE_HTTP_PORTS: 8080
      ASPNETCORE_HTTPS_PORTS: 8081

      # Database
      ConnectionStrings__DefaultConnection: "Host=postgres;Port=5432;Database=colaflow;Username=colaflow;Password=colaflow_dev_password;Include Error Detail=true"

      # Redis
      ConnectionStrings__Redis: "redis:6379,password=colaflow_redis_password,abortConnect=false"

      # JWT Settings
      JwtSettings__SecretKey: "ColaFlow-Development-Secret-Key-Min-32-Characters-Long-2025"
      JwtSettings__Issuer: "ColaFlow"
      JwtSettings__Audience: "ColaFlow-Clients"
      JwtSettings__ExpirationHours: 24

      # Logging
      Logging__LogLevel__Default: Information
      Logging__LogLevel__Microsoft.AspNetCore: Warning
      Logging__LogLevel__Microsoft.EntityFrameworkCore: Information

      # CORS
      CorsSettings__AllowedOrigins: "http://localhost:3000,http://frontend:3000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - colaflow-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    restart: unless-stopped
    # Uncomment for hot reload during development
    # volumes:
    #   - ./colaflow-api/src:/app/src

  # ColaFlow Frontend (Next.js 15)
  frontend:
    build:
      context: ./colaflow-web
      dockerfile: Dockerfile
      target: development
    container_name: colaflow-web
    ports:
      - "3000:3000"
    environment:
      # Next.js
      NODE_ENV: development
      PORT: 3000

      # API Configuration
      NEXT_PUBLIC_API_URL: http://localhost:5000
      NEXT_PUBLIC_WS_URL: ws://localhost:5000/hubs/project

      # Internal API URL (server-side)
      API_URL: http://backend:8080

      # Feature Flags
      NEXT_PUBLIC_ENABLE_ANALYTICS: "false"
      NEXT_PUBLIC_ENABLE_DEBUG: "true"
    depends_on:
      backend:
        condition: service_healthy
    networks:
      - colaflow-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    restart: unless-stopped
    # Hot reload for development
    volumes:
      - ./colaflow-web:/app
      - /app/node_modules
      - /app/.next

  # PostgreSQL Test Database (for Integration Tests)
  postgres-test:
    image: postgres:16-alpine
    container_name: colaflow-postgres-test
    environment:
      POSTGRES_DB: colaflow_test
      POSTGRES_USER: colaflow_test
      POSTGRES_PASSWORD: colaflow_test_password
    ports:
      - "5433:5432"
    networks:
      - colaflow-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U colaflow_test -d colaflow_test"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    tmpfs:
      - /var/lib/postgresql/data

  # pgAdmin (Database Management Tool - Optional)
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: colaflow-pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@colaflow.com
      PGADMIN_DEFAULT_PASSWORD: admin
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      - "5050:80"
    depends_on:
      - postgres
    networks:
      - colaflow-network
    restart: unless-stopped
    profiles:
      - tools

  # Redis Commander (Redis Management Tool - Optional)
  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: colaflow-redis-commander
    environment:
      REDIS_HOSTS: "local:redis:6379:0:colaflow_redis_password"
    ports:
      - "8081:8081"
    depends_on:
      - redis
    networks:
      - colaflow-network
    restart: unless-stopped
    profiles:
      - tools

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

networks:
  colaflow-network:
    driver: bridge
