name: Build

on:
  push:
    branches:
      - main

jobs:
  build-web:
    runs-on: docker

    defaults:
      run:
        working-directory: quibot-web

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install dependencies
        run: |
          apt update
          apt install -y zip
          npm install

      - name: Build Nuxt app
        run: npm run build

      - name: Prepare package
        run: |
          mkdir -p dist_package
          cp -r .output dist_package/
          cp package.json dist_package/
          cp package-lock.json dist_package/ || true

      - name: Create zip
        run: |
          cd dist_package
          zip -r ../quibot-web.zip .

      - name: Upload Web artifact
        uses: actions/upload-artifact@v3
        with:
          name: quibot-web
          path: quibot-web/quibot-web.zip

  build-backend:
    runs-on: docker
    defaults:
      run:
        working-directory: backend

    steps:
      - name: Checkout
        uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          apt update
          apt install -y zip

      - name: Create zip
        run: |
          zip -r backend.zip .

      - name: Upload Backend artifact
        uses: actions/upload-artifact@v3
        with:
          name: backend
          path: backend/backend.zip


  
  release:
    runs-on: docker
    needs: [build-web, build-backend]
    steps:
      - name: Download Web Artifact
        uses: actions/download-artifact@v3
        with:
          name: quibot-web
          path: dist

      - name: Download Backend Artifact
        uses: actions/download-artifact@v3
        with:
          name: backend
          path: dist

      - name: Create Release
        uses: https://gitea.com/actions/gitea-release-action@v1
        with:
          tag_name: latest
          name: Latest Build
          overwrite_files: true
          files: |
            dist/quibot-web.zip
            dist/backend.zip
        env:
          GITEA_TOKEN: ${{ secrets.GITEATOKEN }}
