# Rubis API — AdonisJS V7 (Node 22). ClusterIP uniquement, accessible
# depuis nginx (rubis-web) via DNS K3s : rubis-api.rubis.svc.cluster.local
# Workers BullMQ tournent dans le même process (cf. start/queue.ts).
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rubis-api
  namespace: rubis
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: rubis-api
  template:
    metadata:
      labels:
        app: rubis-api
    spec:
      imagePullSecrets:
        - name: gitea-registry
      # Migrations : exécutées en init-container avant que le serveur démarre.
      # Idempotent (ace migration:run skip ce qui est déjà appliqué).
      # workingDir = build/ pour utiliser ace.js compilé (les .ts ne se chargent
      # pas en runtime, devDeps absentes).
      initContainers:
        - name: migrate
          image: git.arthurbarre.fr/ordinarthur/rubis-api:latest
          imagePullPolicy: Always
          workingDir: /app/apps/api/build
          command: ['node', 'ace.js', 'migration:run', '--force']
          envFrom:
            - secretRef: { name: rubis-app-secrets }
            - configMapRef: { name: rubis-api-config }
          resources:
            requests: { cpu: 50m, memory: 128Mi }
            limits: { cpu: 500m, memory: 512Mi }
      containers:
        - name: api
          image: git.arthurbarre.fr/ordinarthur/rubis-api:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 3333
              name: http
          envFrom:
            - secretRef: { name: rubis-app-secrets }
            - configMapRef: { name: rubis-api-config }
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
            limits:
              cpu: 1000m
              memory: 768Mi
          startupProbe:
            httpGet: { path: /api/v1/health, port: http }
            initialDelaySeconds: 5
            periodSeconds: 5
            failureThreshold: 30
          livenessProbe:
            httpGet: { path: /api/v1/health, port: http }
            periodSeconds: 30
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet: { path: /api/v1/health, port: http }
            periodSeconds: 10
            timeoutSeconds: 3
            failureThreshold: 3
---
# ClusterIP — accessible uniquement depuis le cluster (par nginx rubis-web).
apiVersion: v1
kind: Service
metadata:
  name: rubis-api
  namespace: rubis
spec:
  type: ClusterIP
  selector:
    app: rubis-api
  ports:
    - port: 3333
      targetPort: http
      name: http
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: rubis-api-config
  namespace: rubis
data:
  # Variables non-sensibles. Les secrets sont dans rubis-app-secrets.
  TZ: 'Europe/Paris'
  PORT: '3333'
  HOST: '0.0.0.0'
  NODE_ENV: 'production'
  LOG_LEVEL: 'info'
  APP_URL: 'https://app.rubis.pro'
  WEB_URL: 'https://app.rubis.pro'
  LANDING_URL: 'https://rubis.pro'
  SESSION_DRIVER: 'cookie'
  COOKIE_SECURE: 'true'
  COOKIE_DOMAIN: 'app.rubis.pro'

  DB_CONNECTION: 'postgres'
  PG_HOST: '10.10.10.3'
  PG_PORT: '5432'
  PG_USER: 'rubis'
  PG_DB_NAME: 'rubis_prod'

  REDIS_HOST: 'rubis-redis.rubis.svc.cluster.local'
  REDIS_PORT: '6379'
  LIMITER_STORE: 'redis'

  DRIVE_DISK: 's3'
  S3_ENDPOINT: 'http://minio.minio.svc.cluster.local:9000'
  S3_REGION: 'fr-par'
  S3_BUCKET: 'rubis-prod-invoices'
  S3_FORCE_PATH_STYLE: 'true'

  MAIL_DRIVER: 'resend'
  MAIL_FROM_ADDRESS: 'contact@rubis.pro'
  MAIL_FROM_NAME: "Rubis sur l'ongle"

  OCR_PROVIDER: 'mistral'

  ACCESS_TOKEN_TTL_MINUTES: '30'
  REFRESH_TOKEN_TTL_DAYS: '30'

  # Google SSO — GOOGLE_CLIENT_ID/SECRET sont dans rubis-app-secrets.
  # Le callback URL doit matcher EXACTEMENT ce qui est configuré dans
  # Google Cloud Console (OAuth Client → Authorized redirect URIs).
  GOOGLE_CALLBACK_URL: 'https://app.rubis.pro/api/v1/auth/google/callback'

  # Microsoft SSO — MICROSOFT_CLIENT_ID/SECRET sont dans rubis-app-secrets.
  # MICROSOFT_TENANT : 'common' (work + perso), 'organizations' (M365 only),
  # ou un tenant ID Azure AD spécifique. Le callback URL doit matcher
  # EXACTEMENT le redirect URI configuré côté Azure App registration.
  MICROSOFT_TENANT: 'common'
  MICROSOFT_CALLBACK_URL: 'https://app.rubis.pro/api/v1/auth/microsoft/callback'
