/**
 * admin_roles — admin-dashboard.
 *
 * Admin-plane RBAC: roles for Zync staff accounts (`admin_users`). Distinct
 * from tenant RBAC (roles / role_permissions / tenant_memberships).
 *
 * `permissions TEXT[]` stores an array of ADMIN_PERMISSIONS keys. System roles
 * (SUPER_ADMIN, SUPPORT, BILLING) are seeded and protected from deletion.
 *
 * admin_users.role_id FK is added here as a new column reference.
 * The migration controller adds the ALTER TABLE / backfill / NOT NULL in DDL.
 */
import { pgTable, uuid, text, boolean, timestamp } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'

export const adminRoles = pgTable('admin_roles', {
  id: uuid('id').primaryKey().defaultRandom(),
  name: text('name').notNull().unique(),
  // TEXT[] — array of AdminPermission keys
  permissions: text('permissions')
    .array()
    .notNull()
    .default(sql`'{}'::text[]`),
  isSystemRole: boolean('is_system_role').notNull().default(false),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export type AdminRoleRow = typeof adminRoles.$inferSelect
export type NewAdminRole = typeof adminRoles.$inferInsert
