import type { Kysely } from "kysely";

/**
 * RBAC features for the Subito AI Guru module (docs/subito-ai-guru.md).
 *
 * guru:        read = own tickets + chat, create = open tickets / chat,
 *              update = confirm own pending actions (allowlist still gates
 *              writes server-side), delete = unused.
 * guru-admin:  read = all tickets + analytics. Allowlist management and the
 *              audit log are super-admin only (spec §3), not slug-gated.
 *
 * Both slugs must also exist in AdminAccessFeatureType (console-role.ts) and
 * FEATURE_SLUGS in the admin console.
 */
const FEATURES = [
  { name: "Karma Subito AI Guru", slug: "guru" },
  { name: "Karma Subito AI Guru Admin", slug: "guru-admin" },
];

export async function up(db: Kysely<any>): Promise<void> {
  await db
    .insertInto("console_features")
    .values(
      FEATURES.map((f) => ({ name: f.name, slug: f.slug, is_active: true })),
    )
    .onConflict((oc) => oc.column("slug").doNothing())
    .execute();
}

export async function down(db: Kysely<any>): Promise<void> {
  await db
    .deleteFrom("console_features")
    .where(
      "slug",
      "in",
      FEATURES.map((f) => f.slug),
    )
    .execute();
}
