name: 'Build Web'

on:
  workflow_call:
    inputs:
      build-flavor:
        required: true
        type: string
      build-title:
        required: true
        type: string

    secrets:
      FIREBASE_TOKEN:
        required: true
      FIREBASE_SERVICE_WORKER:
        required: true

jobs:
  build-hosted:
    name: Hosted
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_build@master
        with:
          firebase-token: ${{ secrets.FIREBASE_TOKEN }}
          firebase-service-worker: ${{ secrets.FIREBASE_SERVICE_WORKER }}
          platform: web

      - name: Build LunaSea
        run: npm run build:web

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: web-hosted
          path: ${{ github.workspace }}/build/web

  build-archive:
    name: Archive
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_build@master
        with:
          platform: web

      - name: Build LunaSea
        run: npm run build:web

      - name: Create Archive
        uses: thedoctor0/zip-release@0.6.2
        with:
          type: zip
          directory: ${{ github.workspace }}/build/web
          filename: ../../output/lunasea-web-canvaskit.zip

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: web-archive
          path: ${{ github.workspace }}/output

  build-docker:
    name: Docker
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_build@master
        with:
          platform: web

      - name: Determine Tags
        id: tags
        uses: actions/github-script@v6
        env:
          BUILD_FLAVOR: ${{ inputs.build-flavor }}
          BUILD_TITLE: ${{ inputs.build-title }}
        with:
          result-encoding: string
          script: |
            const tags = [];
            tags.push(process.env.BUILD_FLAVOR);
            tags.push(process.env.BUILD_TITLE);
            if (process.env.BUILD_FLAVOR === 'stable') tags.push('latest');
            return tags.map((tag) => `ghcr.io/jagandeepbrar/lunasea:${tag}`).join(',');

      - name: Build LunaSea
        uses: docker/build-push-action@v2
        with:
          context: .
          outputs: type=docker,dest=${{ github.workspace}}/output/lunasea-web-docker.tar
          tags: ${{ steps.tags.outputs.result }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: web-docker
          path: ${{ github.workspace }}/output
