{{- if .Values.crds.install }}
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: argosettings.freeleaps.com
  labels:
    {{- include "freeleaps-devops-reconciler.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-weight": "-5"
    {{- if .Values.crds.keep }}
    "helm.sh/resource-policy": keep
    {{- end }}
spec:
  group: freeleaps.com
  scope: Namespaced
  names:
    kind: ArgoSetting
    listKind: ArgoSettingList
    singular: argosetting
    plural: argosettings
    shortNames:
      - argo
      - argos
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          required: ['spec']
          properties:
            spec:
              type: object
              required:
                - projectId
                - environments
              properties:
                projectId:
                  type: string
                  description: "Reference to DevOpsProject ID"
                environments:
                  type: array
                  items:
                    type: object
                    required:
                      - name
                      - namespace
                      - repoUrl
                    properties:
                      name:
                        type: string
                        enum: ["dev", "staging", "prod"]
                      namespace:
                        type: string
                        description: "Target Kubernetes namespace"
                      repoUrl:
                        type: string
                        description: "Helm chart repository URL"
                      path:
                        type: string
                        description: "Path to chart in repository"
                        default: "."
                      targetRevision:
                        type: string
                        description: "Git branch or tag"
                        default: "HEAD"
                      syncPolicy:
                        type: object
                        properties:
                          automated:
                            type: object
                            properties:
                              prune:
                                type: boolean
                                default: false
                              selfHeal:
                                type: boolean
                                default: false
                          syncOptions:
                            type: array
                            items:
                              type: string
            status:
              type: object
              properties:
                argoSettings:
                  type: object
                  properties:
                    status:
                      type: string
                      enum: ["invalid", "valid", "synced"]
                    synced:
                      type: boolean
                    ready:
                      type: boolean
                    lastProbeTime:
                      type: string
                      format: date-time
      subresources:
        status: {}
      additionalPrinterColumns:
        - name: Project ID
          type: string
          jsonPath: .spec.projectId
        - name: Environments
          type: string
          jsonPath: .spec.environments[*].name
        - name: Status
          type: string
          jsonPath: .status.argoSettings.status
        - name: Ready
          type: boolean
          jsonPath: .status.argoSettings.ready
        - name: Age
          type: date
          jsonPath: .metadata.creationTimestamp
{{- end }}