/* =====================================================================
   permissions.js — Vollständiger Permission-Katalog. 1:1 portiert aus
   cloudflare-worker/src/lib/permissions.js (keine Datenbank-Abhängigkeit).
   ===================================================================== */

export const PERMISSIONS = {
  APPLICATIONS_VIEW_CREATOR: "Creator-Bewerbungen ansehen",
  APPLICATIONS_VIEW_MANAGER_SCOUT: "Manager-/Scout-Bewerbungen ansehen",
  APPLICATIONS_VIEW_ARCHIVED: "Archivierte Bewerbungen ansehen",
  APPLICATIONS_OPEN_FULL: "Vollständige Bewerbungen öffnen",

  APPLICATIONS_TAKE_OVER: "Bewerbungen übernehmen",
  APPLICATIONS_ASSIGN: "Bewerbungen anderen Personen zuweisen",
  APPLICATIONS_CHANGE_STATUS: "Status verändern",
  APPLICATIONS_ARCHIVE: "Bewerbungen archivieren",
  APPLICATIONS_RESTORE: "Bewerbungen wiederherstellen",
  APPLICATIONS_DELETE: "Bewerbungen löschen",

  DRAFTS_CREATE: "Antwortentwürfe erstellen",
  DRAFTS_EDIT_OWN: "Eigene Entwürfe bearbeiten",
  DRAFTS_EDIT_OTHERS: "Entwürfe anderer Personen bearbeiten",
  REPLIES_COPY: "Fertige Antworten kopieren",
  EMAILS_COPY: "E-Mail-Adressen kopieren",
  APPLICATIONS_MARK_ANSWERED: "Bewerbungen als beantwortet markieren",

  NOTES_READ: "Notizen lesen",
  NOTES_WRITE: "Notizen schreiben",
  NOTES_EDIT_OWN: "Eigene Notizen bearbeiten",
  NOTES_EDIT_OTHERS: "Fremde Notizen bearbeiten",
  NOTES_DELETE: "Notizen löschen",

  TEAM_VIEW: "Andere Teammitglieder ansehen",
  TEAM_PREPARE_USER: "Neue Benutzer vorbereiten",
  TEAM_LOCK_USER: "Benutzer sperren",
  TEAM_ASSIGN_ROLE: "Rollen vergeben",
  TEAM_CREATE_ROLE: "Rollen erstellen",
  TEAM_EDIT_PERMISSIONS: "Allgemeine Berechtigungen verändern",

  SETTINGS_EDIT_FORMS: "Bewerbungsformulare bearbeiten",
  SETTINGS_EDIT_STATUS_TYPES: "Statusarten verändern",
  SETTINGS_VIEW_STATS: "Statistiken ansehen",
  SETTINGS_VIEW_ACTIVITY_LOG: "Aktivitätsverlauf ansehen",
  SETTINGS_EDIT_GENERAL: "Allgemeine Einstellungen bearbeiten",

  SUPPORTER_VIEW: "Supporter-Abonnenten ansehen",
  SUPPORTER_MANAGE_PREMIUMS: "Supporter-Prämien & Versand verwalten",
  SUPPORTER_MANAGE_COLLECTIONS: "Supporter-Kollektionen verwalten",
};

export const PERMISSION_GROUPS = [
  { label: "Bewerbungsbereiche", keys: ["APPLICATIONS_VIEW_CREATOR", "APPLICATIONS_VIEW_MANAGER_SCOUT", "APPLICATIONS_VIEW_ARCHIVED", "APPLICATIONS_OPEN_FULL"] },
  { label: "Bearbeitung", keys: ["APPLICATIONS_TAKE_OVER", "APPLICATIONS_ASSIGN", "APPLICATIONS_CHANGE_STATUS", "APPLICATIONS_ARCHIVE", "APPLICATIONS_RESTORE", "APPLICATIONS_DELETE"] },
  { label: "Antworten", keys: ["DRAFTS_CREATE", "DRAFTS_EDIT_OWN", "DRAFTS_EDIT_OTHERS", "REPLIES_COPY", "EMAILS_COPY", "APPLICATIONS_MARK_ANSWERED"] },
  { label: "Interne Notizen", keys: ["NOTES_READ", "NOTES_WRITE", "NOTES_EDIT_OWN", "NOTES_EDIT_OTHERS", "NOTES_DELETE"] },
  { label: "Teamverwaltung", keys: ["TEAM_VIEW", "TEAM_PREPARE_USER", "TEAM_LOCK_USER", "TEAM_ASSIGN_ROLE", "TEAM_CREATE_ROLE", "TEAM_EDIT_PERMISSIONS"] },
  { label: "Einstellungen", keys: ["SETTINGS_EDIT_FORMS", "SETTINGS_EDIT_STATUS_TYPES", "SETTINGS_VIEW_STATS", "SETTINGS_VIEW_ACTIVITY_LOG", "SETTINGS_EDIT_GENERAL"] },
  { label: "DogiCrew 🩵", keys: ["SUPPORTER_VIEW", "SUPPORTER_MANAGE_PREMIUMS", "SUPPORTER_MANAGE_COLLECTIONS"] },
];

export const OWNER_ONLY_ACTIONS = [
  "CODE_VIEW", "CODE_COPY", "CODE_CREATE", "CODE_EDIT", "CODE_RESET",
  "EDIT_OWNER_ACCOUNT", "DELETE_OR_LIMIT_OWNER_ROLE", "PROMOTE_TO_OWNER",
];

export function hasPermission(rolePermissions, overrides, key) {
  if (overrides && Object.prototype.hasOwnProperty.call(overrides, key)) {
    return !!overrides[key];
  }
  return Array.isArray(rolePermissions) && rolePermissions.includes(key);
}

export function isValidPermissionKey(key) {
  return Object.prototype.hasOwnProperty.call(PERMISSIONS, key);
}
