name: Move Assets to Release

on:
  workflow_run:
    workflows: ["build-macos.yml"] # This ensures `move-assets.yml` runs AFTER `build-macos.yml`
    types:
      - completed
  push:
    tags:
    - '*'

jobs:
  download_assets:
    runs-on: ubuntu-latest

    steps:
    - name: Fetch Windows exe
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-windows.yml
        name: nlpw.exe

    - name: Fetch Windows icu1
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-windows.yml
        name: "icudt74.dll"

    - name: Fetch Windows icu2
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-windows.yml
        name: "icuuc74.dll"
        
    - name: Fetch Linux exe
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-linux.yml
        name: nlpl.exe
        
    # - name: Fetch MacOS exe
    #   uses: dawidd6/action-download-artifact@v8
    #   with:
    #     workflow: build-macos.yml
    #     name: nlpm.exe
    
    - name: Download MacOS Executable
      uses: actions/download-artifact@v3
      with:
        name: nlpm.exe
        path: ./

    - name: Fetch Engine Files
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-enginefiles.yml
        name: "nlpengine.zip"

    - name: Fetch VisualText Files
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-visualfiles.yml
        name: "visualtext.zip"

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1.0.0
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        draft: false
        prerelease: false

    - name: Add nlpw asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./nlpw.exe"

    - name: Add nlpl asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./nlpl.exe"

    - name: Fetch Linux icu1
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-linux.yml
        name: "libicutu.a"

    - name: Fetch Linux icu2
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-linux.yml
        name: "libicuuc.a"

    # - name: Add nlpm.exe asset
    #   uses: shogo82148/actions-upload-release-asset@v1
    #   with:
    #     upload_url: ${{ steps.create_release.outputs.upload_url }}
    #     asset_path: "./nlpm.exe"
    - name: Add nlpm asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./nlpm.exe"

    - name: Fetch MacOS icu1
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-macos.yml
        name: "libicutum.a"

    - name: Fetch MacOS icu2
      uses: dawidd6/action-download-artifact@v8
      with:
        workflow: build-macos.yml
        name: "libicuucm.a"      
        
    - name: Add icudt74.dll asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./icudt74.dll"
                
    - name: Add icuuc74.dll asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./icuuc74.dll"

    - name: Add libicutu.a asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./libicutu.a"

    - name: Add libicuuc.a asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./libicuuc.a"

    - name: Add libicutum.a asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./libicutum.a"

    - name: Add libicuucm.a asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./libicuucm.a"
                
    - name: Add nlpengine.zip asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./nlpengine.zip"
                
    - name: Add visualtext.zip asset
      uses: shogo82148/actions-upload-release-asset@v1
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: "./visualtext.zip"

    - name: Trigger nlp-engine-windows workflow
      uses: benc-uk/workflow-dispatch@v1
      with:
        workflow: nlp-engine-build.yml
        token: ${{ secrets.GITHUB_TOKEN }}
        repo: VisualText/nlp-engine-windows
        ref: main

    - name: Trigger nlp-engine-linux workflow
      uses: benc-uk/workflow-dispatch@v1
      with:
        workflow: nlp-engine-build.yml
        token: ${{ secrets.GITHUB_TOKEN }}
        repo: VisualText/nlp-engine-linux
        ref: main

    - name: Trigger nlp-engine-mac workflow
      uses: benc-uk/workflow-dispatch@v1
      with:
        workflow: nlp-engine-build.yml
        token: ${{ secrets.GITHUB_TOKEN }}
        repo: VisualText/nlp-engine-mac
        ref: main
