apiVersion: v1
kind: Service
metadata:
  name: savepal-backend-service
  namespace: savepal-production
  labels:
    app: savepal-backend
    component: api
    environment: production
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
    prometheus.io/scrape: "true"
    prometheus.io/port: "4100"
    prometheus.io/path: "/metrics"
spec:
  type: ClusterIP
  ports:
  - port: 4100
    targetPort: 4100
    protocol: TCP
    name: http
  selector:
    app: savepal-backend
    component: api
    environment: production
  sessionAffinity: None

---
apiVersion: v1
kind: Service
metadata:
  name: savepal-redis-service
  namespace: savepal-production
  labels:
    app: savepal-redis
    component: cache
    environment: production
spec:
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
    protocol: TCP
    name: redis
  selector:
    app: savepal-redis
    component: cache
    environment: production
  sessionAffinity: None

---
# Headless service for StatefulSet (if needed for Redis clustering)
apiVersion: v1
kind: Service
metadata:
  name: savepal-redis-headless
  namespace: savepal-production
  labels:
    app: savepal-redis
    component: cache
    environment: production
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - port: 6379
    targetPort: 6379
    protocol: TCP
    name: redis
  selector:
    app: savepal-redis
    component: cache
    environment: production

---
# External LoadBalancer service (if needed for direct access)
apiVersion: v1
kind: Service
metadata:
  name: savepal-backend-external
  namespace: savepal-production
  labels:
    app: savepal-backend
    component: api
    environment: production
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "http"
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: "arn:aws:acm:region:account:certificate/cert-id"
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "https"
    external-dns.alpha.kubernetes.io/hostname: "api.savepal.ai"
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 4100
    protocol: TCP
    name: http
  - port: 443
    targetPort: 4100
    protocol: TCP
    name: https
  selector:
    app: savepal-backend
    component: api
    environment: production
  loadBalancerSourceRanges:
  - 0.0.0.0/0  # Allow from anywhere (adjust as needed for security)
