import type { Kysely } from "kysely";

/**
 * RBAC features for the Kevo module (docs/kevo-flagging/PLAN.md).
 *
 * kevo:          read = view flags/knowledge/stats, create = manual flag +
 *                write corrections, update = approve/reject/route (reviewer),
 *                delete = retire knowledge entries.
 * kevo-publish:  update = publish approved knowledge to the ElevenLabs agent.
 *
 * Both slugs must also exist in AdminAccessFeatureType (console-role.ts) and
 * FEATURE_SLUGS in the admin console.
 */
const FEATURES = [
  { name: "Kevo", slug: "kevo" },
  { name: "Kevo Publishing", slug: "kevo-publish" },
];

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();
}
