{{- if .Values.crds.install }}
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: jenkinssettings.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: JenkinsSetting
    listKind: JenkinsSettingList
    singular: jenkinssetting
    plural: jenkinssettings
    shortNames:
      - jenkins
      - jen
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          required: ['spec']
          properties:
            spec:
              type: object
              required:
                - projectId
                - gitUrl
                - environments
              properties:
                projectId:
                  type: string
                  description: "Reference to DevOpsProject ID"
                gitUrl:
                  type: string
                  description: "Git repository URL for Jenkins pipelines"
                gitCredentialsId:
                  type: string
                  description: "Jenkins credentials ID for Git access"
                environments:
                  type: array
                  items:
                    type: object
                    required:
                      - name
                      - branch
                    properties:
                      name:
                        type: string
                        enum: ["dev", "staging", "prod"]
                      branch:
                        type: string
                        description: "Git branch for this environment"
                      pipelineScript:
                        type: string
                        description: "Custom Jenkinsfile content"
                      buildTriggers:
                        type: object
                        properties:
                          webhook:
                            type: boolean
                            default: true
                          schedule:
                            type: string
                            description: "Cron schedule for builds"
                folder:
                  type: object
                  properties:
                    name:
                      type: string
                      description: "Jenkins folder name"
                    description:
                      type: string
                      description: "Jenkins folder description"
            status:
              type: object
              properties:
                jenkinsSettings:
                  type: object
                  properties:
                    status:
                      type: string
                      enum: ["invalid", "valid", "synced"]
                    synced:
                      type: boolean
                    ready:
                      type: boolean
                    lastProbeTime:
                      type: string
                      format: date-time
                    folderUrl:
                      type: string
                      description: "Jenkins folder URL"
                    jobs:
                      type: array
                      items:
                        type: object
                        properties:
                          name:
                            type: string
                          environment:
                            type: string
                          url:
                            type: string
      subresources:
        status: {}
      additionalPrinterColumns:
        - name: Project ID
          type: string
          jsonPath: .spec.projectId
        - name: Git URL
          type: string
          jsonPath: .spec.gitUrl
        - name: Environments
          type: string
          jsonPath: .spec.environments[*].name
        - name: Status
          type: string
          jsonPath: .status.jenkinsSettings.status
        - name: Ready
          type: boolean
          jsonPath: .status.jenkinsSettings.ready
        - name: Age
          type: date
          jsonPath: .metadata.creationTimestamp
{{- end }}