# MARI — Bitbucket Pipelines (staging)
# Firebase project: mari-f7b13 → https://mari-f7b13.web.app (staging.marihq.co.za when DNS is set)
#
# Required repository variable (secured):
#   FIREBASE_TOKEN  — run `firebase login:ci` locally to generate
#
# Env for web build: apps/web/.env.staging (committed in repo)

image: node:20

definitions:
  caches:
    npm: ~/.npm

  steps:
    - step: &install
        name: Install dependencies
        caches:
          - npm
        script:
          - npm ci

    - step: &build-and-test
        name: Build and test
        caches:
          - npm
        script:
          - npm ci
          - npm run build --workspace=@fssc/shared
          - npm run smoke
          - bash scripts/ci-write-web-env.sh
          - npm run build:staging --workspace=web
          - npm run build:functions

    - step: &lint
        name: Lint
        caches:
          - npm
        script:
          - npm ci
          - npm run lint

    - step: &deploy-staging
        name: Deployment to Staging
        deployment: staging
        caches:
          - npm
        script:
          - npm ci
          - npm run build --workspace=@fssc/shared
          - bash scripts/ci-write-web-env.sh
          - npm run build:staging --workspace=web
          - npm run build:functions
          - npm install -g firebase-tools@14
          - firebase deploy --only firestore:rules,functions,hosting,storage --project mari-f7b13 --token "$FIREBASE_TOKEN" --non-interactive
          - firebase deploy --only firestore:indexes --project mari-f7b13 --token "$FIREBASE_TOKEN" --non-interactive || true

pipelines:
  default:
    - parallel:
        - step: *build-and-test
        - step: *lint

  pull-requests:
    '**':
      - parallel:
          - step: *build-and-test
          - step: *lint

  branches:
    staging:
      - parallel:
          - step: *build-and-test
          - step: *lint
      - step: *deploy-staging

  custom:
    deploy-staging:
      - parallel:
          - step: *build-and-test
          - step: *lint
      - step: *deploy-staging

    # Production — enable when portal.marihq.co.za Firebase project is ready
    deploy-production:
      - step:
          name: Deployment to Production
          deployment: production
          trigger: manual
          script:
            - echo "Production deploy not configured yet. Set up a production Firebase project first."
