apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
  name: base-streamlined-experience-skeleton-template.1
  title: Minimal Information Output Port (Skeleton Entity)
  description: |
    Skeleton Entity variant of the Streamlined Output Port template.
    Uses parameters.yaml for derived fields (editSchemaColumns) and a %SKELETON catalog-info.yaml
    so that schema edits via the Editor Wizard are always reflected without re-creation.
  tags:
    - output-port
    - skeleton
spec:
  generates: componenttype:default/outputport
  owner: agilelab
  type: outputport
  parameters:
    - title: Component metadata
      description: Basic information for this component.
      required:
        - name
        - domain
        - dataproduct
        - description
      properties:
        name:
          title: Name
          type: string
          description: Required name used for display purposes
          ui:field: EntityNamePicker
          default: Output Port
          ui:options:
            allowArbitraryValues: true
        description:
          title: Description
          type: string
          description: Help others understand what this Output Port is for
          default: Output Port that exposes information of this Data Product to users and other data products.
        domain:
          title: Domain
          type: string
          description: Domain of the Data Product this Output Port belongs to
          ui:field: EntityPicker
          ui:options:
            allowArbitraryValues: false
            allowedKinds:
              - Domain
        dataproduct:
          title: Data Product
          type: string
          description: Data Product this Output Port belongs to
          ui:field: EntityPicker
          ui:filter:
            - fieldName: domain
              entityPath: spec.domain
          ui:options:
            allowArbitraryValues: false
            allowedKinds:
              - System
        identifier:
          title: Identifier
          type: string
          description: A unique identifier for the entity inside the domain. It will not be editable after creation and is expected to be a string that is sequences of [a-zA-Z] separated by any of [-_]
          ui:field: ComponentIdentifierPicker
          ui:options:
            allowArbitraryValues: false
        owner:
          title: Owner
          type: string
          description: Automatically selected from the Data Product metadata. Data Product owner.
          ui:field: EntitySelectionPicker
          ui:fieldName: dataproduct
          ui:property: spec.owner
          ui:options:
            allowArbitraryValues: false
        dependsOn:
          title: Depends on
          type: array
          default: []
          items:
            type: string
            ui:field: EntityRelationsPicker
            ui:fieldName: dataproduct
            relation: hasPart
          description: An Output Port could depend on other Output Ports or storage areas
        domainName:
          type: string
          ui:field: EntitySelectionPicker
          ui:fieldName: domain
          ui:property: spec.mesh.name
          ui:options:
            allowArbitraryValues: false
          ui:widget: hidden
        dataproductName:
          type: string
          ui:field: EntitySelectionPicker
          ui:fieldName: dataproduct
          ui:property: spec.mesh.name
          ui:options:
            allowArbitraryValues: false
          ui:widget: hidden

    - title: Data contract schema
      description: |
        Choose the schema for this Output Port that will be inserted on the Data Contract. 
        You can either inherit it from an existing component of a certain kind, or leave it empty
        and add it manually at a later stage using the Editor Wizard.
      required: [ inheritSchema ]
      properties:
        inheritSchema:
          title: Choose one
          type: boolean
          ui:widget: select
          default: false
          oneOf:
            - const: true
              title: Inherit schema from an existing component
            - const: false
              title: Leave schema empty
      dependencies:
        inheritSchema:
          oneOf:
            - properties:
                inheritSchema:
                  const: false
            - required:
                - schemaDefinition
              properties:
                inheritSchema:
                  const: true
                schemaDefinition:
                  ui:options:
                    displayTitle: false
                  type: object
                  required:
                    - inheritSchemaComponent
                    - schemaColumns
                  properties:
                    inheritSchemaComponent:
                      title: Storage component
                      type: string
                      description: Choose a storage component from which the schema will be inherited.
                      ui:field: EntityRelationsPicker
                      ui:filters:
                        - key: spec.type
                          value: storage
                      ui:fieldName: dataproduct
                      relation: hasPart
                    schemaColumns:
                      title: Is schema selected?
                      ui:field: EntitySelectionPicker
                      ui:fieldName: schemaDefinition.inheritSchemaComponent
                      ui:property: spec.mesh.specific.tableSchema # Modify based on where the schema is stored on your source entity
                      ui:options:
                        allowArbitraryValues: false

    - title: Provide Output port deployment information
      description: |-
        Customize this section and further sections based on the requirements for the specific technology you're using.
        Follow the best practices guidelines to understand which fields you should or should not add.
        In this particular case, we're asking only for two fields with a default value so that the user is not forced to
        input them if not needed right away. They can always be edited at a later point using the Editor Wizard.
      properties:
        databaseName:
          title: Database name
          type: string
          default: data_product_db
          description: Database name where the Output Port table will be created
        tableName:
          title: Table name
          type: string
          default: output_port_table
          description: Table name created by this component to act as Output Port

  steps:
    - id: template
      name: Fetch Skeleton + Template
      action: fetch:template
      input:
        url: ./skeleton
        targetPath: '.'
        values:
          name: '${{ parameters.name }}'
          domainName: '${{ parameters.domainName }}'
          dataproductName: '${{ parameters.dataproductName }}'
          description: '${{ parameters.description }}'
          domain: '${{ parameters.domain }}'
          dataproduct: '${{ parameters.dataproduct }}'
          identifier: '${{ parameters.identifier }}'
          owner: '${{ parameters.owner }}'
          dependsOn: '${{ parameters.dependsOn }}'
          databaseName: '${{ parameters.databaseName }}'
          tableName: '${{ parameters.tableName }}'
          creationDate: '${{ parameters.creationDate }}'
          useCaseTemplateId: urn:dmb:utm:base-streamlined-experience-skeleton-template:0.0.0
          infrastructureTemplateId: urn:dmb:itm:output-port-tech-adapter:0
          destination: &destination 'gitlab.com?owner=<YOUR-GITLAB-GROUP>%2F${{ parameters.domain | replace(r/domain:| |-/, "") }}%2F${{ parameters.dataproduct.split(".")[1] | replace(r/ |-/g, "") }}&repo=${{ parameters.name.split(" ") | join("") | lower }}'
          gitOwner: '<YOUR-GITLAB-GROUP>/${{ parameters.domain | replace(r/domain:| |-/, "") }}/${{ parameters.dataproduct.split(".")[1] | replace(r/ |-/g, "") }}'
          repo: '${{ parameters.name.split(" ") | join("") | lower }}'
          artifact_id: '${{ parameters.name }}'
          useCaseTemplateVersion: 0.0.0

    - id: publish
      name: Publish
      action: witboostMeshComponent:publish:gitlab
      input:
        allowedHosts: ['gitlab.com']
        description: 'This is ${{ parameters.name }}'
        repoUrl: *destination
        rootDirectory: '.'
        dataproduct: '${{ parameters.dataproduct }}'

    - id: register
      name: Register
      action: catalog:register
      input:
        repoContentsUrl: '${{ steps.publish.output.repoContentsUrl }}'
        catalogInfoPath: '/catalog-info.yaml'

  output:
    links:
      - title: Repository
        url: '${{ steps.publish.output.remoteUrl }}'
      - title: Open in catalog
        icon: catalog
        entityRef: '${{ steps.register.output.entityRef }}'
