# VAPORA Woodpecker Pipeline - Validate & Build
# Validates all configurations and generates deployment artifacts
# Triggers on: push to main/develop, pull requests, manual dispatch

trigger:
  event: [push, pull_request, manual]
  branch: [main, develop]
  paths:
    include:
      - provisioning/schemas/**
      - provisioning/scripts/**
      - .woodpecker/validate-and-build.yml

variables:
  ARTIFACTS_DIR: provisioning/artifacts
  LOG_DIR: provisioning/logs

stages:
  setup:
    steps:
      - name: prepare
        image: alpine:latest
        commands:
          - mkdir -p ${ARTIFACTS_DIR} ${LOG_DIR}
          - echo "🔧 VAPORA CI/CD Pipeline - Validate & Build"
          - echo "Commit: ${CI_COMMIT_SHA:0:8}"
          - echo "Branch: ${CI_COMMIT_BRANCH}"
          - echo "Event: ${CI_PIPELINE_EVENT}"

  install_dependencies:
    steps:
      - name: install_tools
        image: rust:latest
        commands:
          - apt-get update && apt-get install -y curl jq yq
          - cargo install nu --locked
          - cargo install nickel --locked
          - pip install jinja2-cli
          - nickel --version
          - nu --version
          - jinja2 --version
          - yq --version

  validate_solo:
    depends_on: [install_dependencies]
    steps:
      - name: validate_solo
        image: rust:latest
        commands:
          - apt-get update && apt-get install -y curl jq yq
          - cargo install nu --locked > /dev/null 2>&1
          - cargo install nickel --locked > /dev/null 2>&1
          - pip install jinja2-cli > /dev/null 2>&1
          - cd provisioning
          - nu scripts/validate-config.nu --mode solo 2>&1 | tee ../${LOG_DIR}/validate-solo.log
        environment:
          RUST_LOG: warn

  validate_multiuser:
    depends_on: [install_dependencies]
    steps:
      - name: validate_multiuser
        image: rust:latest
        commands:
          - apt-get update && apt-get install -y curl jq yq
          - cargo install nu --locked > /dev/null 2>&1
          - cargo install nickel --locked > /dev/null 2>&1
          - pip install jinja2-cli > /dev/null 2>&1
          - cd provisioning
          - nu scripts/validate-config.nu --mode multiuser 2>&1 | tee ../${LOG_DIR}/validate-multiuser.log
        environment:
          RUST_LOG: warn

  validate_enterprise:
    depends_on: [install_dependencies]
    steps:
      - name: validate_enterprise
        image: rust:latest
        commands:
          - apt-get update && apt-get install -y curl jq yq
          - cargo install nu --locked > /dev/null 2>&1
          - cargo install nickel --locked > /dev/null 2>&1
          - pip install jinja2-cli > /dev/null 2>&1
          - cd provisioning
          - nu scripts/validate-config.nu --mode enterprise 2>&1 | tee ../${LOG_DIR}/validate-enterprise.log
        environment:
          RUST_LOG: warn

  build_artifacts:
    depends_on: [validate_solo, validate_multiuser, validate_enterprise]
    steps:
      - name: install_tools
        image: rust:latest
        commands:
          - apt-get update && apt-get install -y curl jq yq
          - cargo install nu --locked > /dev/null 2>&1
          - cargo install nickel --locked > /dev/null 2>&1
          - pip install jinja2-cli > /dev/null 2>&1
          - echo "✓ Tools installed"

      - name: build_artifacts
        image: rust:latest
        commands:
          - cd provisioning
          - nu scripts/ci-pipeline.nu --artifact-dir ../artifacts --mode multiuser 2>&1 | tee ../${LOG_DIR}/build.log
        environment:
          RUST_LOG: warn

      - name: verify_artifacts
        image: alpine:latest
        commands:
          - ls -la artifacts/
          - echo "Validating JSON outputs..."
          - for f in artifacts/config-*.json; do jq . "$$f" > /dev/null && echo "✓ $$f"; done
          - echo "Validating YAML outputs..."
          - yq eval '.' artifacts/*.yaml > /dev/null && echo "✓ YAML files valid"
          - echo "Validating TOML outputs..."
          - test -f artifacts/*.toml && echo "✓ TOML files generated"

      - name: generate_manifest
        image: alpine:latest
        commands:
          - cat > artifacts/README.md << 'EOF'
            # VAPORA Deployment Artifacts

            Generated: $(date -u +'%Y-%m-%dT%H:%M:%SZ')
            Commit: ${CI_COMMIT_SHA}
            Branch: ${CI_COMMIT_BRANCH}
            Pipeline: ${CI_BUILD_LINK}

            ## Files Generated

            ### Configurations (JSON)
            - config-solo.json - Solo mode configuration
            - config-multiuser.json - Multiuser mode configuration
            - config-enterprise.json - Enterprise mode configuration

            ### Configuration Formats
            - vapora-solo.toml / vapora-solo.yaml
            - vapora-multiuser.toml / vapora-multiuser.yaml
            - vapora-enterprise.toml / vapora-enterprise.yaml

            ### Kubernetes Manifests
            - configmap.yaml - Kubernetes ConfigMap
            - deployment.yaml - Kubernetes Deployments

            ### Docker Compose
            - docker-compose.yml - Docker Compose stack

            ## Status
            ✅ All configurations generated and validated
            ✅ All templates rendered successfully
            ✅ Ready for deployment
            EOF
          - cat artifacts/README.md

  publish:
    depends_on: [build_artifacts]
    steps:
      - name: publish_artifacts
        image: alpine:latest
        commands:
          - echo "📦 Artifacts ready for deployment"
          - ls -lah artifacts/
          - echo ""
          - echo "Total files: $(find artifacts -type f | wc -l)"
          - du -sh artifacts/

notify:
  slack:
    enabled: true
    when:
      status: [success, failure]
    webhook_id: ${SLACK_WEBHOOK}
    channel: deployments
    template: |
      {{#success build.status}}
      ✅ **VAPORA Validate & Build Successful**
      Commit: {{commit.sha}}
      Branch: {{commit.branch}}
      Author: {{commit.author}}
      {{else}}
      ❌ **VAPORA Validate & Build Failed**
      Commit: {{commit.sha}}
      Branch: {{commit.branch}}
      {{/success}}
