# This ensures the pipeline only runs on the main branch
when:
  event: push
  branch: main

depends_on:
  - ci

steps:
  # Build and Push Docker Image
  # We use the 'kaniko' plugin to build images without needing a Docker daemon (safer/simpler)
  build:
    image: plugins/kaniko
    settings:
      registry: registry.fly.io
      repo: registry.fly.io/spazio-solazzo
      tags: sha-${CI_COMMIT_SHA}
      # You'll need to provide Fly credentials to Kaniko
      username: x
      password:
        from_secret: FLY_API_TOKEN

  # 3. Set Secrets and Deploy
  deploy:
    image: codeberg.org/jasterv/flyctl:latest
    pull: true
    environment:
      FLY_API_TOKEN:
        from_secret: FLY_API_TOKEN
      # Passing the other app secrets
      TOKEN_SIGNING_SECRET: { from_secret: TOKEN_SIGNING_SECRET }
      ADMIN_EMAIL: { from_secret: ADMIN_EMAIL }
      SPAZIO_SOLAZZO_EMAIL: { from_secret: SPAZIO_SOLAZZO_EMAIL }
      FRONT_OFFICE_PHONE_NUMBER: { from_secret: FRONT_OFFICE_PHONE_NUMBER }
      RESEND_API_KEY: { from_secret: RESEND_API_KEY }
      DATABASE_URL: { from_secret: DATABASE_URL }
    commands:
      - |
        flyctl secrets set \
          TOKEN_SIGNING_SECRET="$TOKEN_SIGNING_SECRET" \
          ADMIN_EMAIL="$ADMIN_EMAIL" \
          SPAZIO_SOLAZZO_EMAIL="$SPAZIO_SOLAZZO_EMAIL" \
          FRONT_OFFICE_PHONE_NUMBER="$FRONT_OFFICE_PHONE_NUMBER" \
          RESEND_API_KEY="$RESEND_API_KEY" \
          DATABASE_URL="$DATABASE_URL"
      - flyctl deploy --image registry.fly.io/spazio-solazzo:sha-${CI_COMMIT_SHA}
