name: Publish Web (Docker)

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

    secrets:
      NETLIFY_AUTH_TOKEN:
        required: true

jobs:
  docker:
    name: Docker
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_deployment@master
        with:
          channel: docker
          ghcr-actor: ${{ github.actor }}
          ghcr-token: ${{ github.token }}

      - name: Download Docker Image
        uses: actions/download-artifact@v3
        with:
          name: web-docker
          path: ${{ github.workspace }}/output

      - name: Load Docker Image
        run: docker load --input ${{ github.workspace}}/output/lunasea-web-docker.tar

      - name: Deploy to GitHub Container Registry
        run: docker image push --all-tags ghcr.io/jagandeepbrar/lunasea

  netlify:
    name: Netlify
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_deployment@master
        with:
          channel: netlify

      - name: Download Web Package
        uses: actions/download-artifact@v3
        with:
          name: web-hosted
          path: ${{ github.workspace }}/output

      - name: Determine Release Channel
        id: channel
        uses: actions/github-script@v6
        env:
          FLAVOR: ${{ inputs.build-flavor }}
        with:
          result-encoding: string
          script: |
            const ref = process.env.FLAVOR || 'edge';
            switch (ref) {
              case 'beta': return '11ef2676-fc31-41b5-897b-fd21273d87ed';
              case 'candidate': return '4636e6a1-17ef-45a2-a2b6-3d1c166fd1df';
              case 'stable': return '6634f0b1-323c-4a2f-bd0b-8f1f388673a9';
              case 'edge':
              default: return '325e197e-55f4-449a-b2bb-6831fe47bf2a';
            }

      - name: Deploy to Netlify
        uses: nwtgck/actions-netlify@v1.2
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ steps.channel.outputs.result }}
        with:
          publish-dir: ${{ github.workspace}}/output
          production-deploy: true
          deploy-message: ${{ inputs.build-title }}
