{{- if .Values.crds.install }}
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: deploymentrecords.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: DeploymentRecord
    listKind: DeploymentRecordList
    singular: deploymentrecord
    plural: deploymentrecords
    shortNames:
      - deploy
      - dr
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          required: ['spec']
          properties:
            spec:
              type: object
              required:
                - projectId
                - version
                - environment
              properties:
                projectId:
                  type: string
                  description: "Reference to DevOpsProject ID"
                version:
                  type: string
                  description: "Application version to deploy"
                environment:
                  type: string
                  enum: ["dev", "staging", "prod"]
                  description: "Target environment"
                gitCommitHash:
                  type: string
                  description: "Git commit hash for this deployment"
                buildTrigger:
                  type: string
                  enum: ["manual", "webhook", "schedule", "api"]
                  default: "manual"
                  description: "What triggered this deployment"
                operation:
                  type: string
                  enum: ["start", "terminate", "restart"]
                  default: "start"
                  description: "Deployment operation to perform"
                ttlSeconds:
                  type: integer
                  minimum: 0
                  description: "TTL for this deployment in seconds"
                parameters:
                  type: object
                  additionalProperties:
                    type: string
                  description: "Additional deployment parameters"
            status:
              type: object
              properties:
                deploymentRecord:
                  type: object
                  properties:
                    status:
                      type: string
                      enum: ["running", "success", "failed", "terminated"]
                    phase:
                      type: string
                      enum: ["initializing", "commit-message-linting", "execute-mode-detection", "code-changes-detection", "build-agent-setup", "dependencies-resolving", "semantic-releasing", "compilation-packaging", "image-builder-setup", "image-building", "app-version-updating", "deployment-triggering", "deployment-syncing", "deployment-verification", "resource-cleanup", "finished"]
                    startTime:
                      type: string
                      format: date-time
                    completionTime:
                      type: string
                      format: date-time
                    lastProbeTime:
                      type: string
                      format: date-time
                    jenkinsBuildNumber:
                      type: integer
                      description: "Jenkins build number"
                    jenkinsBuildUrl:
                      type: string
                      description: "Jenkins build URL"
                    argoSyncStatus:
                      type: string
                      description: "Argo CD sync status"
                    message:
                      type: string
                      description: "Status message"
                    networkResources:
                      type: object
                      properties:
                        domain:
                          type: string
                        certificate:
                          type: string
                        ingress:
                          type: string
      subresources:
        status: {}
      additionalPrinterColumns:
        - name: Project ID
          type: string
          jsonPath: .spec.projectId
        - name: Version
          type: string
          jsonPath: .spec.version
        - name: Environment
          type: string
          jsonPath: .spec.environment
        - name: Status
          type: string
          jsonPath: .status.deploymentRecord.status
        - name: Phase
          type: string
          jsonPath: .status.deploymentRecord.phase
        - name: Build
          type: integer
          jsonPath: .status.deploymentRecord.jenkinsBuildNumber
        - name: Age
          type: date
          jsonPath: .metadata.creationTimestamp
{{- end }}