spec:
  inputs:
    # Job and Stage name
    as:
      default: 'delete-state'
      description: 'Defines the name of this job.'
    stage:
      default: 'cleanup'
      description: 'Defines the stage that this job will belong to.'

    # Configuration
    state_name:
      default: default
      description: 'Remote OpenTofu state name.'
    create_delete_state_job:
      default: 'true'
      description: 'Wheather the delete-state job should be created or not.'
    rules:
      # FIXME: eventually, we'll want to define `null` as the default,
      # but this is NOT support yet, see
      # https://gitlab.com/gitlab-org/gitlab/-/issues/440468
      default: [{when: on_success}]
      type: array
      description: 'Defines the `rules` of the job.'

---

'$[[ inputs.as ]]':
  stage: $[[ inputs.stage ]]
  resource_group: $TF_STATE_NAME
  image: curlimages/curl:latest
  variables:
    TF_STATE_NAME: $[[ inputs.state_name ]]
  script:
    - curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$TF_STATE_NAME"
  rules: $[[ inputs.rules ]]
