name: Prepare

on:
  workflow_call:
    inputs:
      flavor:
        type: string
        required: true

    outputs:
      build-changelog:
        description: "Build Changelog"
        value: ${{ jobs.build-details.outputs.build-changelog }}
      build-flavor:
        description: "Build Flavor"
        value: ${{ jobs.build-details.outputs.build-flavor }}
      build-number:
        description: "Build Number"
        value: ${{ jobs.build-details.outputs.build-number }}
      build-title:
        description: "Build Title"
        value: ${{ jobs.build-details.outputs.build-title }}
      build-version:
        description: "Build Version"
        value: ${{ jobs.build-details.outputs.build-version }}

    secrets:
      FIREBASE_TOKEN:
        required: true

jobs:
  build-details:
    name: Build Details
    runs-on: ubuntu-latest
    outputs:
      build-changelog: ${{ steps.build-changelog.outputs.result }}
      build-flavor: ${{ steps.build-flavor.outputs.result }}
      build-number: ${{ steps.build-number.outputs.result }}
      build-title: ${{ steps.build-title.outputs.output }}
      build-version: ${{ steps.build-version.outputs.current-version }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Count Commits
        id: commit-count
        run: |
          COUNT=`git rev-list HEAD --count`
          echo "::set-output name=commit-count::$COUNT"
      
      - name: Build Flavor
        id: build-flavor
        uses: actions/github-script@v6
        env:
          FLAVOR: ${{ inputs.flavor }}
        with:
          result-encoding: string
          script: |
            const flavor = process.env.FLAVOR;
            if (flavor) switch (flavor) {
              case 'edge':
              case 'beta':
              case 'candidate':
              case 'stable':
                return flavor;
            }
            return 'edge';

      - name: Build Changelog
        id: build-changelog
        uses: actions/github-script@v6
        env:
          FLAVOR: ${{ steps.build-flavor.outputs.result }}
        with:
          result-encoding: string
          script: return `Thank you for testing ${process.env.FLAVOR} release builds! All changes can be found within the application.`;

      - name: Build Number
        id: build-number
        uses: actions/github-script@v6
        env:
          COUNT: ${{ steps.commit-count.outputs.commit-count }}
        with:
          result-encoding: string
          script: |
            const count = process.env.COUNT || "0";
            const base = 1000000000;
            return base + Number(count);

      - name: Build Version
        id: build-version
        uses: martinbeentjes/npm-get-version-action@v1.1.0

      - name: Build Title
        id: build-title
        run: |
          HASH=`git rev-parse --short ${{ github.sha }}`
          TITLE="v${{ steps.build-version.outputs.current-version }}-${{ steps.build-flavor.outputs.result }}-${{ steps.build-number.outputs.result }}-$HASH"
          echo "::set-output name=output::$TITLE"

      - name: Log Build Details
        env:
          CHANGELOG: ${{ steps.build-changelog.outputs.result }}
          FLAVOR: ${{ steps.build-flavor.outputs.result }}
          NUMBER: ${{ steps.build-number.outputs.result }}
          TITLE: ${{ steps.build-title.outputs.output }}
          VERSION: ${{ steps.build-version.outputs.current-version }}
        run: |
          echo "Build Changelog: $CHANGELOG"
          echo "Build Flavor: $FLAVOR"
          echo "Build Number: $NUMBER"
          echo "Build Title: $TITLE"
          echo "Build Version: $VERSION"

  generate-files:
    name: Generate Files
    needs: build-details
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_build@master
        with:
          firebase-token: ${{ secrets.FIREBASE_TOKEN }}
          platform: test
          skip-core: true

      - name: Generate Environment Configuration
        run: |
          export FLAVOR=${{ needs.build-details.outputs.build-flavor }}
          export COMMIT=${{ github.sha }}
          export BUILD=${{ needs.build-details.outputs.build-number }}
          flutter packages pub run environment_config:generate
     
      - name: Generate Localization
        run: dart ${{github.workspace }}/scripts/generate_localization.dart

      - name: Generate Changelog
        run: dart ${{github.workspace }}/scripts/generate_changelog.dart ${{ needs.build-details.outputs.build-flavor }}

      - name: Generate Build Runner Files
        run: flutter packages pub run build_runner build

      - name: Upload Core Files
        uses: actions/upload-artifact@v3
        with:
          name: core-files
          path: |
            ${{ github.workspace }}/assets/changelog.json
            ${{ github.workspace }}/assets/localization
            ${{ github.workspace }}/lib/**/*.g.dart
            ${{ github.workspace }}/lib/system/environment.dart
