# Main CI Pipeline for Bakery-IA
# This pipeline orchestrates the build, test, and deploy process
# Includes: fetch -> detect changes -> test -> build -> update gitops
# Supports environment-configurable base images for dev/prod flexibility

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: bakery-ia-ci
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ .Values.labels.app.name }}
    app.kubernetes.io/component: pipeline
spec:
  workspaces:
    - name: shared-workspace
      description: Shared workspace for source code
    - name: docker-credentials
      description: Docker registry credentials
    - name: git-credentials
      description: Git credentials for pushing GitOps updates
  params:
    - name: git-url
      type: string
      description: Repository URL
    - name: git-revision
      type: string
      description: Git revision/commit hash
    - name: registry
      type: string
      description: Container registry URL for pushing built images
    - name: git-branch
      type: string
      description: Target branch for GitOps updates
      default: "main"
    - name: skip-tests
      type: string
      description: Skip tests if "true"
      default: "false"
    - name: dry-run
      type: string
      description: Dry run mode - don't push changes
      default: "false"
    # Base image configuration for environment-specific builds
    - name: base-registry
      type: string
      description: "Base image registry URL (e.g., docker.io for prod, localhost:5000 for dev)"
      default: "{{ .Values.pipeline.build.baseRegistry }}"
    - name: python-image
      type: string
      description: "Python base image name and tag (e.g., python:3.11-slim for prod)"
      default: "{{ .Values.pipeline.build.pythonImage }}"

  tasks:
    # Stage 1: Fetch source code
    - name: fetch-source
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: shared-workspace
      params:
        - name: url
          value: $(params.git-url)
        - name: revision
          value: $(params.git-revision)

    # Stage 2: Detect which services changed
    - name: detect-changes
      runAfter: [fetch-source]
      taskRef:
        name: detect-changed-services
      workspaces:
        - name: source
          workspace: shared-workspace

    # Stage 3: Run tests on changed services
    - name: run-tests
      runAfter: [detect-changes]
      taskRef:
        name: run-tests
      when:
        - input: "$(tasks.detect-changes.results.changed-services)"
          operator: notin
          values: ["none", "infrastructure"]
        - input: "$(params.skip-tests)"
          operator: notin
          values: ["true"]
      workspaces:
        - name: source
          workspace: shared-workspace
      params:
        - name: services
          value: $(tasks.detect-changes.results.changed-services)
        - name: skip-tests
          value: $(params.skip-tests)

    # Stage 4: Build and push container images
    - name: build-and-push
      runAfter: [detect-changes]
      taskRef:
        name: kaniko-build
      when:
        - input: "$(tasks.detect-changes.results.changed-services)"
          operator: notin
          values: ["none", "infrastructure"]
      workspaces:
        - name: source
          workspace: shared-workspace
        - name: docker-credentials
          workspace: docker-credentials
      params:
        - name: services
          value: $(tasks.detect-changes.results.changed-services)
        - name: registry
          value: $(params.registry)
        - name: git-revision
          value: $(params.git-revision)
        # Environment-configurable base images
        - name: base-registry
          value: $(params.base-registry)
        - name: python-image
          value: $(params.python-image)

    # Stage 5: Update GitOps manifests
    - name: update-gitops-manifests
      runAfter: [build-and-push]
      taskRef:
        name: update-gitops
      when:
        - input: "$(tasks.detect-changes.results.changed-services)"
          operator: notin
          values: ["none", "infrastructure"]
        - input: "$(tasks.build-and-push.results.build-status)"
          operator: in
          values: ["success", "partial"]
      workspaces:
        - name: source
          workspace: shared-workspace
        - name: git-credentials
          workspace: git-credentials
      params:
        - name: services
          value: $(tasks.detect-changes.results.changed-services)
        - name: registry
          value: $(params.registry)
        - name: git-revision
          value: $(params.git-revision)
        - name: git-branch
          value: $(params.git-branch)
        - name: dry-run
          value: $(params.dry-run)

  # Final tasks that run regardless of pipeline success/failure
  finally:
    - name: pipeline-summary
      taskRef:
        name: pipeline-summary
      params:
        - name: changed-services
          value: $(tasks.detect-changes.results.changed-services)
        - name: git-revision
          value: $(params.git-revision)