# =============================================================================
# MonkAPI - Service
# =============================================================================
apiVersion: v1
kind: Service
metadata:
  name: monkapi-service
  namespace: monkapi
  labels:
    app: monkapi
spec:
  type: ClusterIP
  selector:
    app: monkapi
  ports:
    - name: http
      port: 80
      targetPort: 3000
      protocol: TCP

---
# =============================================================================
# Ingress (NGINX Ingress Controller)
# =============================================================================
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: monkapi-ingress
  namespace: monkapi
  labels:
    app: monkapi
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: "10m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "60"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "60"
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "60"
    # Rate limiting
    nginx.ingress.kubernetes.io/limit-rps: "100"
    nginx.ingress.kubernetes.io/limit-connections: "50"
    # CORS
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "*"
    nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, PUT, DELETE, OPTIONS"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Merchant-Id,X-API-Key"
    # WebSocket support
    nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
    nginx.ingress.kubernetes.io/upstream-hash-by: "$request_uri"
    # TLS with cert-manager
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - api.monkapi.com
      secretName: monkapi-tls
  rules:
    - host: api.monkapi.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: monkapi-service
                port:
                  number: 80

---
# =============================================================================
# WebSocket Ingress (for Socket.io)
# =============================================================================
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: monkapi-ws-ingress
  namespace: monkapi
  labels:
    app: monkapi
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
    nginx.ingress.kubernetes.io/upstream-hash-by: "$request_uri"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - ws.monkapi.com
      secretName: monkapi-ws-tls
  rules:
    - host: ws.monkapi.com
      http:
        paths:
          - path: /socket.io
            pathType: Prefix
            backend:
              service:
                name: monkapi-service
                port:
                  number: 80
