# bex-api's managed-Postgres surface, Render dashboard-shaped
# (backend/internal/postgres/graphql.go, docs/ADR006-bex-api.md §Managed Postgres).
# Render's dashboard GraphQL noun is `database` (its REST noun is `postgres`);
# bex mirrors that split. `status` is Render's databaseStatus enum
# (available/unavailable/creating); `suspended` is the string enum shared with
# services; `plan` is the Database CRD's spec.plan spelling (the
# databaseInstanceTypes id). `databaseName`/`databaseUser` are the normalized
# unquoted-identifier db + owner role (list omits them; the detail query reads them).

# ownerId scopes the list to one workspace (w6/m2/t004's REST/GraphQL filter) —
# the dashboard's workspace switcher (w6/m3) passes the selected workspace's id;
# omitted, bex-api falls back to the caller's default workspace.
query Databases($ownerId: String) {
  databases(ownerId: $ownerId) {
    id
    name
    plan
    version
    status
    diskSizeGB
    suspended
    createdAt
    public
  }
}

query Database($id: String!) {
  database(id: $id) {
    id
    name
    plan
    version
    status
    databaseName
    databaseUser
    diskSizeGB
    highAvailabilityEnabled
    suspended
    createdAt
    externalHost
    public
    poolerEnabled
    backupsEnabled
    ipAllowList
  }
}

# The one place the DB password is surfaced (docs/ADR006-bex-api.md §Managed Postgres):
# fetched on demand from the detail page's "Reveal", never in the bulk list and
# never on page load. Returns the Render-shaped `PostgresConnectionInfo`.
query DatabaseConnectionInfo($id: String!) {
  databaseConnectionInfo(id: $id) {
    password
    internalConnectionString
    externalConnectionString
    internalConnectionPoolString
    externalConnectionPoolString
    psqlCommand
  }
}

# --- advanced managed-Postgres surface (w1/m17) ---

query DatabaseRecoveryInfo($id: String!) {
  databaseRecoveryInfo(id: $id) {
    enabled
    earliestRecoveryTime
    latestRecoveryTime
    backups {
      id
      status
      createdAt
    }
  }
}

query DatabaseExports($id: String!) {
  databaseExports(id: $id) {
    id
    status
    createdAt
  }
}

query DatabaseUsers($id: String!) {
  databaseUsers(id: $id) {
    name
  }
}

query DatabaseIpAllowList($id: String!) {
  databaseIpAllowList(id: $id)
}

mutation SuspendDatabase($id: String!) {
  suspendDatabase(id: $id) {
    id
    suspended
    status
  }
}

mutation ResumeDatabase($id: String!) {
  resumeDatabase(id: $id) {
    id
    suspended
    status
  }
}

mutation RestartDatabase($id: String!) {
  restartDatabase(id: $id) {
    id
    suspended
    status
  }
}

mutation RecoverDatabase(
  $id: String!
  $name: String!
  $targetTime: String
  $plan: String
  $version: String
) {
  recoverDatabase(
    id: $id
    name: $name
    targetTime: $targetTime
    plan: $plan
    version: $version
  ) {
    id
    name
    plan
    status
  }
}

mutation CreateDatabaseExport($id: String!) {
  createDatabaseExport(id: $id) {
    id
    status
    createdAt
  }
}

mutation SetDatabaseIpAllowList($id: String!, $cidrs: [String]) {
  setDatabaseIpAllowList(id: $id, cidrs: $cidrs) {
    id
    ipAllowList
  }
}

mutation CreateDatabaseUser($id: String!, $name: String!) {
  createDatabaseUser(id: $id, name: $name) {
    name
    password
  }
}

mutation DeleteDatabaseUser($id: String!, $name: String!) {
  deleteDatabaseUser(id: $id, name: $name)
}

# The create dialog's plan-picker source (w5/m8): the shared lego/types/tiers
# Postgres family, read live — never a hardcoded ladder in the frontend.
query DatabaseInstanceTypes {
  databaseInstanceTypes {
    id
    name
    cpu
    memory
    storageGB
  }
}

mutation CreateDatabase(
  $name: String!
  $plan: String
  $version: String
  $diskSizeGB: Int
  $public: Boolean
) {
  createDatabase(
    name: $name
    plan: $plan
    version: $version
    diskSizeGB: $diskSizeGB
    public: $public
  ) {
    id
    name
    plan
    status
  }
}

mutation DeleteDatabase($id: String!) {
  deleteDatabase(id: $id)
}
