name: Langflow Nightly Build
run-name: Langflow Nightly Release by @${{ github.actor }}

on:
  workflow_dispatch:
    inputs:
      build_docker_base:
        description: "Build Docker Image for Langflow Nightly Base"
        required: true
        type: boolean
        default: false
      build_docker_main:
        description: "Build Docker Image for Langflow Nightly"
        required: true
        type: boolean
        default: false
      build_docker_ep:
        description: "Build Docker Image for Langflow Nightly with Entrypoint"
        required: false
        type: boolean
        default: false
      nightly_tag_main:
        description: "Tag for the nightly main build"
        required: true
        type: string
      nightly_tag_base:
        description: "Tag for the nightly base build"
        required: true
        type: string
  workflow_call:
    inputs:
      build_docker_base:
        description: "Build Docker Image for Langflow Nightly Base"
        required: true
        type: boolean
        default: false
      build_docker_main:
        description: "Build Docker Image for Langflow Nightly"
        required: true
        type: boolean
        default: false
      build_docker_ep:
        description: "Build Docker Image for Langflow Nightly with Entrypoint"
        required: false
        type: boolean
        default: false
      nightly_tag_main:
        description: "Tag for the nightly main build"
        required: true
        type: string
      nightly_tag_base:
        description: "Tag for the nightly base build"
        required: true
        type: string

env:
  POETRY_VERSION: "1.8.3"
  PYTHON_VERSION: "3.12"

jobs:
  release-nightly-base:
    name: Release Langflow Nightly Base
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    outputs:
      version: ${{ steps.verify.outputs.version }}
    steps:
      - name: Check out the code at a specific ref
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.nightly_tag_main }}
          persist-credentials: true
      - name: "Setup Environment"
        uses: ./.github/actions/setup-uv
      - name: Install the project
        run: uv sync --dev

      - name: Verify Nightly Name and Version
        id: verify
        run: |
          name=$(uv tree | grep 'langflow-base' | awk '{print $2}')
          version=$(uv tree | grep 'langflow-base' | awk '{print $3}')
          if [ "$name" != "langflow-base-nightly" ]; then
            echo "Name $name does not match langflow-base-nightly. Exiting the workflow."
            exit 1
          fi
          if [ "$version" != "${{ inputs.nightly_tag_base }}" ]; then
            echo "Version $version does not match nightly tag ${{ inputs.nightly_tag_base }}. Exiting the workflow."
            exit 1
          fi
          # Strip the leading `v` from the version
          version=$(echo $version | sed 's/^v//')
          echo "version=$version" >> $GITHUB_OUTPUT

      - name: Build project for distribution
        run: make build base=true args="--wheel"

      - name: Test CLI
        run: |
          # TODO: Unsure why the whl is not built in src/backend/base/dist
          mkdir src/backend/base/dist
          mv dist/*.whl src/backend/base/dist/
          uv pip install src/backend/base/dist/*.whl
          uv run python -m langflow run --host 127.0.0.1 --port 7860 --backend-only &
          SERVER_PID=$!
          # Wait for the server to start
          timeout 120 bash -c 'until curl -f http://127.0.0.1:7860/api/v1/auto_login; do sleep 2; done' || (echo "Server did not start in time" && kill $SERVER_PID && exit 1)
          # Terminate the server
          kill $SERVER_PID || (echo "Failed to terminate the server" && exit 1)
          sleep 20 # give the server some time to terminate
          # Check if the server is still running
          if kill -0 $SERVER_PID 2>/dev/null; then
            echo "Failed to terminate the server"
            exit 0
          else
            echo "Server terminated successfully"
          fi

      - name: Publish to PyPI
        env:
          POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_API_TOKEN }}
          UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        run: |
          make publish base=true

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: dist-base
          path: src/backend/base/dist

  release-nightly-main:
    name: Release Langflow Nightly Main
    needs: [release-nightly-base]
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.verify.outputs.version }}
    defaults:
      run:
        shell: bash
    steps:
      - name: Check out the code at a specific ref
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.nightly_tag_main}}
          persist-credentials: true
      - name: "Setup Environment"
        uses: ./.github/actions/setup-uv
      - name: Install the project
        run: uv sync --dev

      - name: Verify Nightly Name and Version
        id: verify
        run: |
          name=$(uv tree | grep 'langflow' | grep -v 'langflow-base' | awk '{print $1}')
          version=$(uv tree | grep 'langflow' | grep -v 'langflow-base' | awk '{print $2}')
          if [ "$name" != "langflow-nightly" ]; then
            echo "Name $name does not match langflow-nightly. Exiting the workflow."
            exit 1
          fi
          if [ "$version" != "${{ inputs.nightly_tag_main }}" ]; then
            echo "Version $version does not match nightly tag ${{ inputs.nightly_tag_main }}. Exiting the workflow."
            exit 1
          fi
          # Strip the leading `v` from the version
          version=$(echo $version | sed 's/^v//')
          echo "version=$version" >> $GITHUB_OUTPUT
      - name: Wait for PyPI Propagation
        run: sleep 300 # wait for 5 minutes to ensure PyPI propagation of base

      - name: Build project for distribution
        run: make build main=true args="--no-sources --wheel"
      - name: Test CLI
        run: |
          uv pip install dist/*.whl
          uv run python -m langflow run --host 127.0.0.1 --port 7860 --backend-only &
          SERVER_PID=$!
          # Wait for the server to start
          timeout 120 bash -c 'until curl -f http://127.0.0.1:7860/health_check; do sleep 2; done' || (echo "Server did not start in time" && kill $SERVER_PID && exit 1)
          # Terminate the server
          kill $SERVER_PID || (echo "Failed to terminate the server" && exit 1)
          sleep 20 # give the server some time to terminate
          # Check if the server is still running
          if kill -0 $SERVER_PID 2>/dev/null; then
            echo "Failed to terminate the server"
            exit 0
          else
            echo "Server terminated successfully"
          fi
      - name: Publish to PyPI
        env:
          POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_API_TOKEN }}
          UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        run: |
          make publish main=true
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: dist-main
          path: dist

  call_docker_build_base:
    name: Call Docker Build Workflow for Langflow Base
    if: always() && ${{ inputs.build_docker_base == 'true' }}
    needs: [release-nightly-base, release-nightly-main]
    uses: ./.github/workflows/docker-build.yml
    with:
      release_type: nightly-base
      base_version: ${{ inputs.nightly_tag_base }}
      main_version: ${{ inputs.nightly_tag_main }}
    secrets: inherit

  call_docker_build_main:
    name: Call Docker Build Workflow for Langflow
    if: always() && ${{ inputs.build_docker_main == 'true' }}
    needs: [release-nightly-main]
    uses: ./.github/workflows/docker-build.yml
    with:
      release_type: nightly-main
      main_version: ${{ inputs.nightly_tag_main }}
    secrets: inherit

  call_docker_build_main_ep:
    name: Call Docker Build Workflow for Langflow with Entrypoint
    if: always() && ${{ inputs.build_docker_ep == 'true' }}
    needs: [release-nightly-main]
    uses: ./.github/workflows/docker-build.yml
    with:
      release_type: main-ep
      main_version: ${{ inputs.nightly_tag_main }}
    secrets: inherit