{{- if .Values.crds.install }}
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: devopsprojects.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: DevOpsProject
    listKind: DevOpsProjectList
    singular: devopsproject
    plural: devopsprojects
    shortNames:
      - dop
      - dops
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          required: ['spec']
          properties:
            metadata:
              type: object
              properties:
                name:
                  type: string
                  maxLength: 63
            spec:
              type: object
              required:
                - projectName
                - projectId
                - git
                - registry
                - environments
              properties:
                projectName:
                  type: string
                  description: "Human readable project name"
                projectId:
                  type: string
                  description: "Unique project identifier"
                  pattern: "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
                git:
                  type: object
                  required:
                    - url
                    - branch
                  properties:
                    url:
                      type: string
                      description: "Git repository URL"
                    branch:
                      type: string
                      description: "Default git branch"
                      default: "main"
                    credentialsRef:
                      type: object
                      properties:
                        name:
                          type: string
                        namespace:
                          type: string
                registry:
                  type: object
                  required:
                    - url
                    - project
                  properties:
                    url:
                      type: string
                      description: "Container registry URL"
                    project:
                      type: string
                      description: "Registry project/namespace"
                    credentialsRef:
                      type: object
                      properties:
                        name:
                          type: string
                        namespace:
                          type: string
                environments:
                  type: array
                  items:
                    type: object
                    required:
                      - name
                      - branch
                    properties:
                      name:
                        type: string
                        enum: ["dev", "staging", "prod"]
                      branch:
                        type: string
                      autoSync:
                        type: boolean
                        default: false
            status:
              type: object
              properties:
                devopsProject:
                  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: Git URL
          type: string
          jsonPath: .spec.git.url
        - name: Status
          type: string
          jsonPath: .status.devopsProject.status
        - name: Ready
          type: boolean
          jsonPath: .status.devopsProject.ready
        - name: Age
          type: date
          jsonPath: .metadata.creationTimestamp
{{- end }}