# Sensor for manual workflow triggers.
#
# Listens on workflow-trigger EventSource and submits the appropriate workflow
# when a webhook endpoint is hit. Each trigger maps JSON body fields to
# workflow parameters.
#
# Trigger reference:
#   compile-deploy    — POST /compile-deploy    {"commit_sha": "..."}
#   train-dqn         — POST /train/dqn         {"binary_tag": "..."}
#   train-ppo         — POST /train/ppo         {"binary_tag": "..."}
#   train-supervised  — POST /train/supervised  {"model": "...", "binary_tag": "..."}
#   build-image       — POST /build-image       {"dockerfile": "...", "image_name": "...", "commit_sha": "..."}
---
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: workflow-trigger
  namespace: foxhunt
  labels:
    app.kubernetes.io/name: workflow-trigger
    app.kubernetes.io/part-of: foxhunt
spec:
  template:
    serviceAccountName: argo-workflow
    nodeSelector:
      k8s.scaleway.com/pool-name: platform
  eventBusName: default

  dependencies:
    - name: compile-deploy-dep
      eventSourceName: workflow-trigger
      eventName: compile-deploy
    - name: train-dqn-dep
      eventSourceName: workflow-trigger
      eventName: train-dqn
    - name: train-ppo-dep
      eventSourceName: workflow-trigger
      eventName: train-ppo
    - name: train-supervised-dep
      eventSourceName: workflow-trigger
      eventName: train-supervised
    - name: build-image-dep
      eventSourceName: workflow-trigger
      eventName: build-image

  triggers:
    # ── Compile + Deploy ──
    # POST /compile-deploy {"commit_sha": "abc1234"}
    # Omit service_packages to build all 7 services (default).
    - template:
        name: compile-deploy
        conditions: compile-deploy-dep
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: compile-deploy-
                namespace: foxhunt
              spec:
                serviceAccountName: argo-workflow
                podGC:
                  strategy: OnPodCompletion
                ttlStrategy:
                  secondsAfterCompletion: 3600
                workflowTemplateRef:
                  name: compile-and-deploy
                arguments:
                  parameters:
                    - name: commit-sha
          parameters:
            - src:
                dependencyName: compile-deploy-dep
                dataKey: body.commit_sha
              dest: spec.arguments.parameters.0.value

    # ── Train DQN ──
    # POST /train/dqn {"binary_tag": "latest"}
    - template:
        name: train-dqn
        conditions: train-dqn-dep
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: train-dqn-
                namespace: foxhunt
              spec:
                serviceAccountName: argo-workflow
                podGC:
                  strategy: OnPodCompletion
                ttlStrategy:
                  secondsAfterCompletion: 3600
                workflowTemplateRef:
                  name: train-dqn
                arguments:
                  parameters:
                    - name: binary-tag
          parameters:
            - src:
                dependencyName: train-dqn-dep
                dataKey: body.binary_tag
              dest: spec.arguments.parameters.0.value

    # ── Train PPO ──
    # POST /train/ppo {"binary_tag": "latest"}
    - template:
        name: train-ppo
        conditions: train-ppo-dep
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: train-ppo-
                namespace: foxhunt
              spec:
                serviceAccountName: argo-workflow
                podGC:
                  strategy: OnPodCompletion
                ttlStrategy:
                  secondsAfterCompletion: 3600
                workflowTemplateRef:
                  name: train-ppo
                arguments:
                  parameters:
                    - name: binary-tag
          parameters:
            - src:
                dependencyName: train-ppo-dep
                dataKey: body.binary_tag
              dest: spec.arguments.parameters.0.value

    # ── Train Supervised ──
    # POST /train/supervised {"model": "tft", "binary_tag": "latest"}
    # model: one of tft, mamba2, tggn, tlob, liquid, kan, xlstm, diffusion
    - template:
        name: train-supervised
        conditions: train-supervised-dep
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: train-supervised-
                namespace: foxhunt
              spec:
                serviceAccountName: argo-workflow
                podGC:
                  strategy: OnPodCompletion
                ttlStrategy:
                  secondsAfterCompletion: 3600
                workflowTemplateRef:
                  name: train-supervised
                arguments:
                  parameters:
                    - name: model
                    - name: binary-tag
          parameters:
            - src:
                dependencyName: train-supervised-dep
                dataKey: body.model
              dest: spec.arguments.parameters.0.value
            - src:
                dependencyName: train-supervised-dep
                dataKey: body.binary_tag
              dest: spec.arguments.parameters.1.value

    # ── Build Image ──
    # POST /build-image {"dockerfile": "Dockerfile.ci-builder", "image_name": "ci-builder", "commit_sha": "HEAD"}
    - template:
        name: build-image
        conditions: build-image-dep
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: build-image-
                namespace: foxhunt
              spec:
                serviceAccountName: argo-workflow
                podGC:
                  strategy: OnPodCompletion
                ttlStrategy:
                  secondsAfterCompletion: 3600
                workflowTemplateRef:
                  name: build-ci-image
                arguments:
                  parameters:
                    - name: commit-sha
                    - name: dockerfile
                    - name: image-name
          parameters:
            - src:
                dependencyName: build-image-dep
                dataKey: body.commit_sha
              dest: spec.arguments.parameters.0.value
            - src:
                dependencyName: build-image-dep
                dataKey: body.dockerfile
              dest: spec.arguments.parameters.1.value
            - src:
                dependencyName: build-image-dep
                dataKey: body.image_name
              dest: spec.arguments.parameters.2.value
