services:
  couchdb:
    image: couchdb:3.3
    container_name: couchdb-https
    restart: unless-stopped
    ports:
      - "5984:5984"   # HTTP
      - "5981:6984"   # HTTPS
    environment:
      # Admin credentials required for CouchDB 3.0+
      - COUCHDB_USER=admin
      - COUCHDB_PASSWORD=smartlogger
    volumes:
      - couchdb-data:/opt/couchdb/data
    entrypoint: ["/bin/bash", "-c"]
    command:
      - |
        # Generate SSL certificates
        mkdir -p /opt/couchdb/etc/certs
        if [ ! -f /opt/couchdb/etc/certs/cert.pem ]; then
          openssl req -x509 -newkey rsa:2048 -keyout /opt/couchdb/etc/certs/key.pem -out /opt/couchdb/etc/certs/cert.pem -days 365 -nodes -subj "/CN=localhost"
          chmod 600 /opt/couchdb/etc/certs/key.pem
          chmod 644 /opt/couchdb/etc/certs/cert.pem
        fi
        
        # Create config directory
        mkdir -p /opt/couchdb/etc/local.d
        
        # Create SSL config
        cat > /opt/couchdb/etc/local.d/ssl.ini << 'EOF'
        [ssl]
        enable = true
        cert_file = /opt/couchdb/etc/certs/cert.pem
        key_file = /opt/couchdb/etc/certs/key.pem
        port = 6984
        
        [daemons]
        httpsd = {couch_httpd, start_link, [https]}
        EOF
        
        # Create CORS and Security config 
        cat > /opt/couchdb/etc/local.d/security.ini << 'EOF'
        [couchdb]
        single_node = true
        
        [cors]
        enabled = true
        origins = *
        credentials = true
        methods = GET, PUT, POST, HEAD, DELETE, OPTIONS, PATCH
        headers = accept, authorization, content-type, origin, referer, x-csrf-token, x-requested-with
        
        [chttpd]
        bind_address = 0.0.0.0
        port = 5984
        require_valid_user = false
        enable_cors = true
        
        [httpd]
        bind_address = 0.0.0.0
        port = 5984
        require_valid_user = false
        enable_cors = true
        
        [couch_httpd_auth]
        require_valid_user = false
        
        [chttpd_auth]
        require_valid_user = false
        
        # Empty admins section = no authentication required
        [admins]
        
        EOF
        
        # Start CouchDB in background
        /docker-entrypoint.sh /opt/couchdb/bin/couchdb &
        
        # Wait for CouchDB to start
        until curl -s http://localhost:5984/ > /dev/null 2>&1; do
            sleep 1
        done
        
        # Create system databases
        curl -X PUT http://localhost:5984/_users > /dev/null 2>&1
        curl -X PUT http://localhost:5984/_replicator > /dev/null 2>&1
        curl -X PUT http://localhost:5984/_global_changes > /dev/null 2>&1
        
        echo "========================================"
        echo "CouchDB is ready!"
        echo "========================================"
        echo "HTTP access:  http://localhost:5984/"
        echo "HTTPS access: https://localhost:5981/"
        echo "CORS: Enabled for all origins"
        echo "Admin credentials:"
        echo "  Username: admin"
        echo "  Password: smartlogger"
        echo ""
        echo "Test access:"
        echo "curl -u admin:smartlogger http://localhost:5984/"
        echo "curl -u admin:smartlogger -k https://localhost:5981/"
        echo ""
        echo "Web UI access:"
        echo "   http://localhost:5984/_utils/"
        echo "   https://localhost:5981/_utils/"
        echo "========================================"
        
        # Keep container running
        wait

volumes:
  couchdb-data:
    driver: local 