{{- if .Values.crds.install }}
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: ingressresources.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: IngressResources
    listKind: IngressResourcesList
    singular: ingressresources
    plural: ingressresources
    shortNames:
      - ingress
      - ir
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          required: ['spec']
          properties:
            spec:
              type: object
              required:
                - projectId
                - environment
                - domain
              properties:
                projectId:
                  type: string
                  description: "Reference to DevOpsProject ID"
                environment:
                  type: string
                  enum: ["dev", "staging", "prod"]
                  description: "Target environment"
                domain:
                  type: string
                  description: "Domain name for the ingress"
                serviceName:
                  type: string
                  description: "Backend service name"
                servicePort:
                  type: integer
                  description: "Backend service port"
                  default: 80
                tlsEnabled:
                  type: boolean
                  description: "Enable TLS/SSL"
                  default: true
                certificateIssuer:
                  type: string
                  description: "cert-manager ClusterIssuer name"
                  default: "letsencrypt-prod"
                ingressClassName:
                  type: string
                  description: "Ingress class name"
                  default: "nginx"
                annotations:
                  type: object
                  additionalProperties:
                    type: string
                  description: "Additional ingress annotations"
                paths:
                  type: array
                  items:
                    type: object
                    required:
                      - path
                    properties:
                      path:
                        type: string
                        description: "Path pattern"
                      pathType:
                        type: string
                        enum: ["Exact", "Prefix", "ImplementationSpecific"]
                        default: "Prefix"
                      serviceName:
                        type: string
                        description: "Override service name for this path"
                      servicePort:
                        type: integer
                        description: "Override service port for this path"
            status:
              type: object
              properties:
                ingressResources:
                  type: object
                  properties:
                    status:
                      type: string
                      enum: ["invalid", "creating", "ready", "failed"]
                    ready:
                      type: boolean
                    lastProbeTime:
                      type: string
                      format: date-time
                    domain:
                      type: object
                      properties:
                        name:
                          type: string
                        created:
                          type: boolean
                        dnsReady:
                          type: boolean
                    certificate:
                      type: object
                      properties:
                        name:
                          type: string
                        issued:
                          type: boolean
                        ready:
                          type: boolean
                    ingress:
                      type: object
                      properties:
                        name:
                          type: string
                        created:
                          type: boolean
                        ready:
                          type: boolean
                        loadBalancerIP:
                          type: string
      subresources:
        status: {}
      additionalPrinterColumns:
        - name: Project ID
          type: string
          jsonPath: .spec.projectId
        - name: Environment
          type: string
          jsonPath: .spec.environment
        - name: Domain
          type: string
          jsonPath: .spec.domain
        - name: TLS
          type: boolean
          jsonPath: .spec.tlsEnabled
        - name: Status
          type: string
          jsonPath: .status.ingressResources.status
        - name: Ready
          type: boolean
          jsonPath: .status.ingressResources.ready
        - name: Age
          type: date
          jsonPath: .metadata.creationTimestamp
{{- end }}