name: Deploy to Firebase Hosting

on:
  push:
    branches:
      - main
  workflow_dispatch:
    inputs:
      ref:
        description: "Branch/Ref ausführen"
        required: false
        default: "main"
      public_cms_url:
        description: "PUBLIC_CMS_URL"
        required: false
        default: "https://cms.pm86.de"
      public_site_url:
        description: "PUBLIC_SITE_URL"
        required: false
        default: "https://windwiderstand.de"
      public_formbricks_app_url:
        description: "PUBLIC_FORMBRICKS_APP_URL (leer = kein Formbricks)"
        required: false
        default: "https://forms.pm86.de"
      public_formbricks_environment_id:
        description: "PUBLIC_FORMBRICKS_ENVIRONMENT_ID"
        required: false
        default: "cmnlgrjle0008mr01k018oaiz"

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Enable Corepack (Yarn)
        run: corepack enable

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

      - name: Build
        env:
          PUBLIC_CMS_URL: ${{ inputs.public_cms_url || 'https://cms.pm86.de' }}
          PUBLIC_SITE_URL: ${{ inputs.public_site_url || 'https://windwiderstand.de' }}
          PUBLIC_FORMBRICKS_APP_URL: ${{ inputs.public_formbricks_app_url || 'https://forms.pm86.de' }}
          PUBLIC_FORMBRICKS_ENVIRONMENT_ID: ${{ inputs.public_formbricks_environment_id || 'cmnlgrjle0008mr01k018oaiz' }}
        run: yarn build

      - name: Deploy to Firebase
        env:
          FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
        run: npx firebase-tools deploy --only hosting --token "$FIREBASE_TOKEN" --non-interactive
