# On push to fxhnt main/master in Gitea, submit the fxhnt-cockpit workflow (Phase 2B + git-sync fast deploys).
# Gitea push payload: { "ref": "refs/heads/main", "after": "<new-sha>", "commits":[{added,removed,modified}] }
#
# TWO routes (mutually exclusive, complete):
#   dep-build  -> mode=build  : a dep/Dockerfile file changed (pyproject.toml / uv.lock / infra/docker/) OR the
#                               changed-file info is missing (FAIL-OPEN — never miss a real deploy). Full kaniko rebuild.
#   dep-deploy -> mode=deploy : src/ or README changed AND no dep/Dockerfile change. SKIPS the build — git-sync
#                               re-pulls src/ on the rollout restart (~30s vs ~5min). (docs/infra-only -> neither, skipped.)
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: gitea-deploy
  namespace: foxhunt
  labels:
    app.kubernetes.io/name: gitea-deploy
    app.kubernetes.io/part-of: foxhunt
spec:
  template:
    # pod intentionally NOT part-of:foxhunt (avoids default-deny-all; matches working sensors)
    serviceAccountName: argo-workflow
    nodeSelector:
      k8s.scaleway.com/pool-name: platform
  dependencies:
    - name: dep-build
      eventSourceName: gitea-push
      eventName: push
      filters:
        data:
          - path: body.ref
            type: string
            value: ["refs/heads/main", "refs/heads/master"]
        # Rebuild iff a dep/Dockerfile file changed, or we can't tell (fail-open).
        script: |
          local b = event.body or event
          if b == nil or b.commits == nil then return true end
          local saw = false
          local function isdep(p)
            return type(p) == "string" and (p == "pyproject.toml" or p == "uv.lock" or p:find("^infra/docker/") ~= nil)
          end
          local function scan(lst)
            if type(lst) ~= "table" then return false end
            for _, f in ipairs(lst) do saw = true; if isdep(f) then return true end end
            return false
          end
          for _, c in ipairs(b.commits) do
            if scan(c.added) or scan(c.modified) or scan(c.removed) then return true end
          end
          if not saw then return true end
          return false
    - name: dep-deploy
      eventSourceName: gitea-push
      eventName: push
      filters:
        data:
          - path: body.ref
            type: string
            value: ["refs/heads/main", "refs/heads/master"]
        # Fast deploy iff src/README changed AND no dep/Dockerfile change AND file info is present (else dep-build fail-opens).
        script: |
          local b = event.body or event
          if b == nil or b.commits == nil then return false end
          local saw, hasdep, hassrc = false, false, false
          local function classify(lst)
            if type(lst) ~= "table" then return end
            for _, f in ipairs(lst) do
              if type(f) == "string" then
                saw = true
                if f == "pyproject.toml" or f == "uv.lock" or f:find("^infra/docker/") ~= nil then hasdep = true end
                if f:find("^src/") ~= nil or f == "README.md" then hassrc = true end
              end
            end
          end
          for _, c in ipairs(b.commits) do classify(c.added); classify(c.modified); classify(c.removed) end
          if not saw then return false end
          return hassrc and not hasdep
  triggers:
    - template:
        name: build-cockpit
        conditions: dep-build
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata: { generateName: fxhnt-cockpit-, namespace: foxhunt }
              spec:
                serviceAccountName: argo-workflow
                podGC: { strategy: OnPodCompletion }
                ttlStrategy: { secondsAfterCompletion: 3600 }
                workflowTemplateRef: { name: fxhnt-cockpit }
                arguments:
                  parameters:
                    - name: commit-sha
                    - { name: mode, value: build }
          parameters:
            - src: { dependencyName: dep-build, dataKey: body.after }
              dest: spec.arguments.parameters.0.value
    - template:
        name: deploy-cockpit
        conditions: dep-deploy
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata: { generateName: fxhnt-cockpit-deploy-, namespace: foxhunt }
              spec:
                serviceAccountName: argo-workflow
                podGC: { strategy: OnPodCompletion }
                ttlStrategy: { secondsAfterCompletion: 3600 }
                workflowTemplateRef: { name: fxhnt-cockpit }
                arguments:
                  parameters:
                    - name: commit-sha
                    - { name: mode, value: deploy }
          parameters:
            - src: { dependencyName: dep-deploy, dataKey: body.after }
              dest: spec.arguments.parameters.0.value
