import { PrismaClient } from '@prisma/client';
import {
  bindResourceToPage,
  getPageResourceIds,
  grantRoleAccess,
  upsertPage,
} from '../scripts/lib/page-helpers';

/**
 * Game 模块 seed
 *
 * 职责：game page (admin 子节点) + game resource + 自家 SYSTEM_ADMIN 授权。
 * 前置：seedBootstrap 已建好 admin 与 SYSTEM_ADMIN。
 */
export async function seedGameModule(prisma: PrismaClient): Promise<void> {
  const role = await prisma.role.findUniqueOrThrow({
    where: { name: 'SYSTEM_ADMIN' },
  });
  const admin = await prisma.page.findUniqueOrThrow({
    where: { code: 'admin' },
  });

  const page = await upsertPage(prisma, {
    code: 'game',
    name: 'game',
    zhName: '业务管理',
    enName: 'Business Mgnt',
    sortOrder: 1050,
    parentId: admin.id,
  });
  console.log(`[game] Page: ${page.code} (id=${page.id})`);

  await bindResourceToPage(
    prisma,
    page.id,
    'game',
    'Business / Game management',
  );
  console.log(`[game] PageResource: game ↔ game`);

  const resourceIds = await getPageResourceIds(prisma, page.id);
  await grantRoleAccess(prisma, role.id, page.id, resourceIds);
  console.log(`[game] Grant: SYSTEM_ADMIN → game`);
}
