#!/usr/bin/env bash
# Build the AppSync subscription-only schema from the canonical GraphQL source.
#
# AppSync doesn't support `extend type` — we must merge all Query/Mutation/Subscription
# fields into single type blocks. This script extracts only the subscription-related
# types from packages/database-pg/graphql/types/subscriptions.graphql and builds
# a self-contained AppSync schema.

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SRC="$REPO_ROOT/packages/database-pg/graphql/types/subscriptions.graphql"
DST="$REPO_ROOT/terraform/schema.graphql"

echo "Building AppSync subscription schema → $DST"

# Extract event payload types (everything before "extend type")
EVENT_TYPES=$(sed -n '1,/^extend type/p' "$SRC" | sed '$d')

# Extract mutation fields (between "extend type Mutation {" and "}")
MUTATION_FIELDS=$(sed -n '/^extend type Mutation {$/,/^}$/p' "$SRC" | grep -v '^extend type\|^}$')

# Extract subscription fields (between "extend type Subscription {" and "}")
SUBSCRIPTION_FIELDS=$(sed -n '/^extend type Subscription {$/,/^}$/p' "$SRC" | grep -v '^extend type\|^}$')

cat > "$DST" <<SCHEMA
# Auto-generated AppSync subscription-only schema.
# DO NOT EDIT — regenerate with: pnpm schema:build
#
# Source: packages/database-pg/graphql/types/subscriptions.graphql

scalar AWSDateTime
scalar AWSJSON

schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}

type Query {
  _empty: String
}

$EVENT_TYPES

type Mutation {
  _empty: String
$MUTATION_FIELDS
}

type Subscription {
  _empty: String
$SUBSCRIPTION_FIELDS
}
SCHEMA

# Add AppSync auth directives to all mutation and subscription fields
# Every field needs @aws_api_key @aws_cognito_user_pools @aws_iam for multi-auth
AUTH="@aws_api_key @aws_cognito_user_pools @aws_iam"

# Add to mutation return types (lines with "): XxxEvent" or "): XxxEvent!")
sed -i '' "s/): \([A-Za-z]*Event\)\(!\{0,1\}\)$/): \1\2 ${AUTH}/" "$DST"

# Add to subscription fields — on the line with the return type (before @aws_subscribe)
# Pattern: "  onXxx(tenantId: ID!): XxxEvent"
sed -i '' "s/): \([A-Za-z]*Event\)\(!\{0,1\}\)$/): \1\2 ${AUTH}/" "$DST"

echo "Done — $(wc -l < "$DST" | tr -d ' ') lines written"
