import { CollectionConfig } from "payload"; import { isAdminPanelRequest, requirePermission } from "@/utils/access-control/hasPermission"; import { canViewUndiscoveredFactions, maskUndiscoveredFaction, } from "@/lib/intelligence/factionDiscovery"; export const Factions: CollectionConfig = { slug: "factions", admin: { group: "Intelligence", useAsTitle: "name", }, access: { create: requirePermission("factions:create"), update: requirePermission("factions:update"), delete: requirePermission("factions:delete"), read: ({ req }) => !!req.user, }, hooks: { beforeValidate: [ async ({ data, operation, originalDoc, req }) => { if (data?.type !== "playerFaction") return data; const payload = req.payload; const existing = await payload.find({ collection: "factions", where: { type: { equals: "playerFaction" } }, limit: 1, depth: 0, overrideAccess: true, }); const conflict = existing.docs.find((doc) => doc.id !== originalDoc?.id); if (conflict) { throw new Error( `Only one faction can be the Player Faction at a time. "${conflict.name}" is already the Player Faction.`, ); } return data; }, ], afterRead: [ // Undiscovered factions stay readable; only their identity is masked. // Admin panel reads are exempt: a masked edit form would write the // placeholder name back over the record on save. async ({ doc, req }) => { const record = doc as unknown as { discovered?: boolean | null } | null; if (!record || record.discovered !== false) return doc; if (isAdminPanelRequest(req)) return doc; if (await canViewUndiscoveredFactions(req.payload, req.user)) return doc; return maskUndiscoveredFaction(doc); }, ], }, fields: [ { name: "name", type: "text", required: true, unique: true, }, { name: "type", type: "select", options: [ { label: "Player Faction", value: "playerFaction", }, { label: "Major Faction", value: "majorFaction", }, { label: "Minor Faction", value: "minorFaction", }, { label: "Corporation", value: "corporation", }, { label: "Special", value: "special", }, ], required: true, defaultValue: "minorFaction", admin: { description: "Player Faction = the faction our unit is currently playing as. Only one faction can carry this type at a time.", }, }, { name: "discovered", type: "checkbox", defaultValue: true, index: true, admin: { description: "Uncheck while players should not know this faction yet. The entry still appears in lists as UNDISCOVERED; name, description, standing, campaign links, cover image and the playable/starting flags are hidden from anyone without the View Undiscovered Factions permission.", }, }, { name: "description", type: "richText", }, { name: "reputation", type: "number", admin: { description: "This faction's standing in the world, from -100 (reviled) to 100 (revered). 0 is neutral. Adjusted manually by admins/GMs as campaign events unfold.", }, defaultValue: 0, min: -100, max: 100, }, { name: "playableFaction", type: "checkbox", admin: { description: "Whether or not this faction is playable. Some factions may be specifically intended to be unplayable based on narrative requirements.", }, }, { name: "startingFaction", type: "checkbox", admin: { description: "Whether or not this faction is/was available as a starting faction at the beginning of the campaign.", }, }, { name: "associatedCampaigns", type: "relationship", relationTo: "campaigns", hasMany: true, }, { name: "coverImage", type: "upload", relationTo: "media", admin: { description: "Background image for the faction detail page hero. Renders translucent with a top-to-bottom fade to black. Recommended aspect ratio: 16:9 or wider.", }, }, ], };