name: Deploy to Cloudflare Workers

on:
  push:
    branches:
      - '**'

permissions:
  contents: read
  deployments: write
  pull-requests: write

jobs:
  deploy:
    runs-on: ubuntu-latest
    name: Deploy
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Setup Wrangler Types
        uses: cloudflare/wrangler-action@v3
        with:
          command: types

      - name: Build
        run: bun run build
        env:
          NUXT_DISCORD_USER_ID: ${{ secrets.NUXT_DISCORD_USER_ID }}
          NUXT_WAKATIME_CODING: ${{ secrets.NUXT_WAKATIME_CODING }}
          NUXT_WAKATIME_EDITORS: ${{ secrets.NUXT_WAKATIME_EDITORS }}
          NUXT_WAKATIME_LANGUAGES: ${{ secrets.NUXT_WAKATIME_LANGUAGES }}
          NUXT_WAKATIME_OS: ${{ secrets.NUXT_WAKATIME_OS }}
          NUXT_WAKATIME_USER_ID: ${{ secrets.NUXT_WAKATIME_USER_ID }}
          NUXT_STATUS_PAGE: ${{ secrets.NUXT_STATUS_PAGE }}
          STUDIO_GITHUB_CLIENT_ID: ${{ secrets.STUDIO_GITHUB_CLIENT_ID }}
          STUDIO_GITHUB_CLIENT_SECRET: ${{ secrets.STUDIO_GITHUB_CLIENT_SECRET }}

      - name: Determine Deployment Target
        id: target
        run: |
          if [ "${{ github.ref_name }}" = "main" ]; then
            echo "env_flag=" >> $GITHUB_OUTPUT
            echo "env_name=Production" >> $GITHUB_OUTPUT
          else
            echo "env_flag=--env preview" >> $GITHUB_OUTPUT
            echo "env_name=Preview" >> $GITHUB_OUTPUT
          fi

      - name: Publish to Cloudflare Workers
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: deploy ${{ steps.target.outputs.env_flag }}
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

      - name: Discord Notification
        uses: sarisia/actions-status-discord@v1
        if: always()
        with:
          webhook: ${{ secrets.DISCORD_WEBHOOK }}
          status: ${{ job.status }}
          title: "Déploiement Portfolio (${{ steps.target.outputs.env_name }})"
          description: |
            Build terminé sur la branche **${{ github.ref_name }}**.
            Environnement cible : **${{ steps.target.outputs.env_name }}**.
            Commit: `${{ github.sha }}` par ${{ github.actor }}.
          nofail: false
          nodetail: false
          image: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
          username: "GitHub Actions"