import {readFileSync,existsSync} from 'node:fs';
import {staffSealFor} from './native-seals.mjs';
import {honourMarkup,HONOUR_CSS} from './honours.mjs';
import {staffClosureStats,statsForAccount,PERSONAL_DEPARTMENT_THRESHOLD} from './staff-closure-stats.mjs';
import {publicProfileFor} from './profile.mjs';
import {accessFor,rankFor} from './permissions.mjs';
import {SHARED_HEADER,SHARED_HEAD} from '../scripts/portal-navigation.mjs';
const esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
const socialLabels={youtube:'YouTube',twitch:'Twitch',twitter:'X / Twitter',x:'X',facebook:'Facebook',discord:'Discord',instagram:'Instagram',tiktok:'TikTok',github:'GitHub',dunehub:'DuneHub',website:'Website'};
const socialArt={dunehub:' ',youtube:' ',twitch:' ',twitter:' ',facebook:' ',discord:' ',instagram:' ',tiktok:' ',github:' ',website:' '};
function socialIcon(key){return ''+(socialArt[key==='x'?'twitter':key]||socialArt.website)+' ';}
const teams=[
// Leadership first, then the operational departments. Members are ordered by effective rank level.
['Network staff','web.tickets.network','rank-network-staff-150.png'],
['Dev team','web.tickets.dev','team-dev-150.png'],
['Legal team','web.tickets.legal','department-legal-v2-150.png'],
['Player support','web.tickets.support','department-support-150.png'],
['Ban appeals','web.tickets.appeals','department-ban-appeals-150.png'],
['Forum team','web.forum.moderate','department-forum-support-150.png'],
['News team','web.posts.publish',null],
['Sales team','web.tickets.sales','department-sales-150.png'],
['Build Team',null,'rank-builder-150.png']
];
function sealFor(user,ranks){
if(ranks.includes('web_dev'))return '/assets/seals/rank-dev-150.png';
if(ranks.includes('owners')&&['unimatrix999','dunerealms','grmacken','richwjr'].includes(user.username.toLowerCase()))return '/assets/seals/owner-'+user.username.toLowerCase()+'-150.png';
if(ranks.includes('web_net_admin')&&user.username.toLowerCase()==='grmacken')return '/assets/seals/main-network-admin-grmacken-150.png';
if(ranks.includes('network_admin')||ranks.includes('network_admin'))return '/assets/seals/rank-network-staff-150.png';
if(ranks.includes('corestaff')&&user.username.toLowerCase()==='racoonvibz')return '/assets/seals/corestaff-racoonvibz-150.png';
if(ranks.includes('game_builder'))return '/assets/seals/rank-builder-150.png';
return '/assets/nav-staff-v2.webp';
}
export async function publicStaffRoster(db,manifest=null){
if(manifest===null){const file='/home/dunerealms/apps/integrations/seal-manifest.json';manifest=existsSync(file)?JSON.parse(readFileSync(file,'utf8')):{};}
const {results}=await db.prepare("SELECT DISTINCT a.id,a.username,a.role FROM site_accounts a LEFT JOIN cp_account_ranks r ON r.user_id=a.id WHERE a.role NOT IN ('user','vip','player','patron_caravan','patron_guild','patron_grand') OR r.rank_key NOT IN ('user','vip','patron_caravan','patron_guild','patron_grand')").all();
const roster=[],closureStats=await staffClosureStats(db);
for(const user of results){
const access=await accessFor(db,user);
if(access.ranks.includes('official_account'))continue;
if(!access.ranks.some(r=>!['user','vip','patron_caravan','patron_guild','patron_grand'].includes(r))||(!access.ranks.includes('game_builder')&&!access.nodes.some(n=>['web.tickets.staff','web.forum.moderate','web.posts.edit','web.ranks.manage'].includes(n))))continue;
const currentRanks=await Promise.all(access.ranks.filter(k=>!['user','vip','patron_caravan','patron_guild','patron_grand'].includes(k)).map(key=>rankFor(db,key)));
currentRanks.sort((a,b)=>b.level-a.level||a.label.localeCompare(b.label));
const rankLabels=[...new Set(currentRanks.map(rank=>rank.label))];
let publicProfile={};try{const profile=await publicProfileFor(db,user.id);publicProfile={honours:profile.honours,bio:profile.bio,socials:profile.socials,git:profile.git};}catch{}
let linked=false;try{linked=!!await db.prepare('SELECT 1 AS linked FROM site_game_links WHERE user_id=?').bind(user.id).first();}catch{}
const portrait={url:'https://cp.dunerealms.com/api/avatars/roster/'+encodeURIComponent(user.username),mode:linked?'skin':'image'};
roster.push({publicProfile,portrait,closedTickets:statsForAccount(closureStats,user.id),username:user.username,name:user.username,rankLabels,level:access.level,seal:sealFor(user,access.ranks),personalSeal:staffSealFor({...user,ranks:access.ranks},manifest),teams:teams.filter(([,node])=>node?access.nodes.includes(node):access.ranks.includes('game_builder')).map(([label])=>label)});
}
return roster.sort((a,b)=>b.level-a.level||a.username.localeCompare(b.username));
}
export function staffHierarchy(roster){
const bands=new Map();
for(const person of [...roster].sort((a,b)=>b.level-a.level||a.username.localeCompare(b.username))){
const key=person.level;
if(!bands.has(key))bands.set(key,{level:key,members:[]});
bands.get(key).members.push(person);
}
return [...bands.values()];
}
function hierarchyMarkup(roster){
if(!roster.length)return '';
return 'People of the realms
Staff hierarchy Ordered by current rank. Departments below show each person’s areas of service; this overview does not imply individual reporting lines.
'+staffHierarchy(roster).map(band=>''+[...new Set(band.members.map(p=>p.rankLabels[0]||'Staff'))].map(esc).join(' · ')+' '+band.members.map(p=>''+esc(p.username)+' '+esc(p.rankLabels[0]||'Staff')+' ').join('')+' ').join('')+' '+teams.filter(([name])=>roster.some(p=>p.teams.includes(name))).map(([name],i)=>''+esc(name)+' ').join('')+' ';
}
const styles=`.roster-hierarchy{padding:24px;border:1px solid #a58d4f;border-radius:9px;background:linear-gradient(135deg,#203f32,#10291f);margin:24px 0}.roster-hierarchy h2{color:#f0d89e;margin:0;font-size:26px}.roster-hierarchy>p{max-width:760px;color:#c4d2bc}.roster-rank-tree{list-style:none;padding:0 0 0 22px;margin:24px 0;border-left:2px solid #b69a55}.roster-rank-tree>li{position:relative;padding:0 0 20px 14px}.roster-rank-tree>li:before{content:'';position:absolute;left:-29px;top:9px;width:10px;height:10px;background:#e6c775;transform:rotate(45deg);border:1px solid #162e23}.roster-rank-tree h3{font-size:16px;color:#e8d29d;margin:0 0 9px}.roster-rank-tree ul{list-style:none;display:flex;flex-wrap:wrap;gap:10px;padding:0}.roster-rank-tree ul li{border:1px solid #69774e;border-radius:6px;background:#102b21;padding:10px 15px;min-width:130px;max-width:100%;box-sizing:border-box;overflow-wrap:anywhere}.roster-rank-tree span,.roster-rank-tree small{display:block}.roster-rank-tree small{color:#b6c8af;font-size:11px}.roster-hierarchy nav{display:flex;flex-wrap:wrap;gap:8px}.roster-hierarchy nav a{border:1px solid #8a7b4a;border-radius:5px;padding:7px 11px;color:#ead3a0;text-decoration:none}.roster-hierarchy nav a:focus-visible{outline:2px solid #f2d991;outline-offset:3px}.roster-team{scroll-margin-top:20px}@media(max-width:540px){.roster-hierarchy{padding:17px}.roster-rank-tree{padding-left:15px}.roster-rank-tree>li:before{left:-22px}.roster-rank-tree ul li{min-width:0;width:100%}}.roster-ticket-stats{margin-top:12px;padding:11px 13px;border:1px solid #7c814f;border-radius:6px;background:#112d22}.roster-ticket-stats>div{display:flex;align-items:center;gap:12px}.roster-ticket-stats small{text-transform:uppercase;letter-spacing:.1em;font-size:9px;color:#c5ba87}.roster-ticket-stats strong{font-size:21px;color:#edd496}.roster-ticket-stats p{font-size:10px!important;color:#b9cdb3!important;margin:7px 0 0!important}.roster-ticket-stats>span{font-size:11px;color:#b0c4ae}
body{margin:0;background:#0c211a;color:#eee4cc;font:15px/1.65 system-ui,sans-serif}.roster-main{max-width:1120px;margin:0 auto;padding:32px 24px 48px}.roster-intro{padding:26px;border:1px solid #8a7b4a;border-radius:9px;background:linear-gradient(110deg,#203f32eb,#142e25eb),url('/assets/veiled-concord-banner-v1.png') center/cover;margin-bottom:26px}.roster-kicker{text-transform:uppercase;letter-spacing:.18em;color:#d8b86e;font-size:11px}.roster-intro h1{font-size:32px;line-height:1.2;margin:10px 0 12px;color:#f2e3b7}.roster-intro p{max-width:690px}.roster-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:16px}.roster-card{border:1px solid #5b704d;background:linear-gradient(145deg,#1f3e30,#112b21);border-radius:8px;padding:22px;display:flex;gap:16px;align-items:center;box-shadow:0 4px 12px #0002}.roster-card img{width:88px;height:88px;object-fit:contain;flex-shrink:0}.roster-card h2{font-size:19px;line-height:1.25;margin:0 0 7px;color:#f0d89e;overflow-wrap:anywhere}.roster-card p{font-size:12px;color:#c4d2bc;margin:5px 0}.roster-handle{color:#aebeab!important}.roster-teams{margin-top:32px;display:grid;grid-template-columns:repeat(auto-fit,minmax(290px,1fr));gap:16px}.roster-team{border:1px solid #526849;background:#142f24;border-radius:8px;padding:20px}.roster-team h2{font-size:18px;margin:0 0 14px;color:#e4ca8b;display:flex;align-items:center;gap:10px}.roster-team h2 img{width:44px;height:44px;object-fit:contain}.roster-team ul{list-style:none;display:flex;flex-wrap:wrap;gap:8px;margin:0;padding:0}.roster-team li{border:1px solid #4d6748;border-radius:5px;background:#203e2e;padding:5px 10px;color:#e5dec6;font-size:13px}.roster-help{margin-top:25px;padding:18px 22px;border-left:3px solid #b59b55;background:#183429}.roster-help a{color:#e4c478}.roster-footer{padding:22px 24px;text-align:center;border-top:1px solid #5a6949;background:#10271e;color:#c4c9ad;font-size:12px}.roster-footer a{color:#dec17c}@media(max-width:540px){.roster-main{padding:20px 14px}.roster-intro{padding:20px}.roster-intro h1{font-size:26px}.roster-grid{grid-template-columns:1fr}.roster-card{padding:17px}.roster-card img{width:74px;height:74px}}.roster-directory{border:1px solid #5a704c;border-radius:8px;background:#142f24;padding:20px;margin-bottom:24px}.roster-directory h2{font-size:21px;margin:0 0 16px;color:#e8d39e}.roster-table-scroll{overflow:auto}.roster-directory table{width:100%;border-collapse:collapse;font-size:12px;text-align:left}.roster-directory th,.roster-directory td{padding:11px;border-bottom:1px solid #3f5d43;vertical-align:top}.roster-directory thead{background:#254734;color:#ecd49a}.roster-directory tbody th{color:#ead7ab;white-space:nowrap}.roster-teams{display:block;margin-top:24px}.roster-team{padding:18px;margin-bottom:22px}.roster-team h2{margin-bottom:8px}.roster-team h2 small{margin-left:auto;color:#b9c8ac;font-size:11px;font-weight:400}.roster-team-table{width:100%;table-layout:fixed;border-collapse:separate;border-spacing:10px}.roster-team-table>tbody>tr>td{width:50%;padding:0;vertical-align:top}.roster-member-card{padding:18px;align-items:flex-start;gap:15px;height:100%;box-sizing:border-box}.roster-portrait{position:relative;flex:0 0 72px}.roster-portrait>img{width:72px;height:72px;border:1px solid #9b8c4c;border-radius:7px;background:#10281d}.roster-portrait>.roster-role-crest{position:absolute;width:32px;height:32px;right:-7px;bottom:-7px;border:0;background:#142e23;border-radius:50%}.roster-member-info{min-width:0;flex:1}.roster-member-info h3{margin:0 0 6px;font-size:18px;line-height:1.3;color:#efd49a;overflow-wrap:anywhere}.roster-rank-label{color:#dfc58c!important;font-weight:600}.roster-member-info dl{font-size:11px;line-height:1.6;margin:11px 0 0}.roster-member-info dt{color:#a9c2aa;font-size:10px;text-transform:uppercase;letter-spacing:.1em}.roster-member-info dd{margin:3px 0 0;color:#d0d8bf}.roster-public-profile{margin-top:11px;border-top:1px solid #496447;padding-top:8px;font-size:12px}.roster-public-profile summary{cursor:pointer;color:#dfc58c}.roster-socials{display:flex;flex-wrap:wrap;gap:7px;margin-top:10px}.roster-socials a{font-size:10px;padding:3px 7px;border:1px solid #64774d;border-radius:4px;color:#ead3a1;text-decoration:none}.roster-socials a:hover{background:#34553a}@media(max-width:700px){.roster-team-table,.roster-team-table tbody,.roster-team-table tr,.roster-team-table>tbody>tr>td{display:block;width:100%;box-sizing:border-box}.roster-team-table{border-spacing:0}.roster-team-table>tbody>tr>td{margin:10px 0}.roster-empty-cell{display:none!important}.roster-team{padding:14px}.roster-member-card{padding:15px}.roster-portrait{flex-basis:62px}.roster-portrait>img{width:62px;height:62px}.roster-directory{padding:14px}}
/* Every team owns one full-width register; keep cached earlier grid rules from squeezing tables. */
.roster-teams{display:block!important;width:100%!important;max-width:none!important}.roster-team{position:relative;isolation:isolate;width:100%;box-sizing:border-box;border:1px solid #a58d4f!important;padding:18px!important;margin:0 0 26px!important;background:linear-gradient(140deg,#1d3b2dd9,#102b21f2),url('/assets/ornament.webp') center/cover;box-shadow:inset 0 0 0 4px #d6b86c0a,0 5px 14px #0002;overflow:hidden}.roster-team::before{content:"";pointer-events:none;position:absolute;inset:5px;border:1px solid #b29b4a24;border-radius:4px;z-index:-1}.roster-team h2{margin:-18px -18px 10px!important;padding:15px 22px;border-bottom:1px solid #9f894b;background:linear-gradient(90deg,#284c35e8,#19362ade),url('/assets/ornament.webp') center/cover;box-shadow:inset 0 2px #d8bc6455;font-size:21px}.roster-team h2 img{width:47px;height:47px;filter:drop-shadow(0 2px 2px #0005)}.roster-team h2 small{padding:3px 8px;border:1px solid #a28d4b66;border-radius:4px;background:#0e261b66;font-size:11px}.roster-team-table{width:100%!important;border-spacing:12px;table-layout:fixed}.roster-team-table>tbody>tr>td{vertical-align:top}.roster-team-table>tbody>tr>.roster-single-cell{width:100%}.roster-member-card{position:relative;height:auto!important;min-height:125px;padding:18px!important;border:1px solid #7c814f!important;background:linear-gradient(140deg,#244735,#122e23);box-shadow:inset 0 0 0 3px #cbb15c0a,0 3px 8px #0002}.roster-member-card::before{content:"";position:absolute;pointer-events:none;inset:5px;border-top:1px solid #bd9f4c22;border-bottom:1px solid #bd9f4c22;border-radius:3px}.roster-portrait>img{border-color:#c7a75e;box-shadow:0 0 0 2px #091f17,0 0 0 3px #a08d4555}.roster-member-info h3{font-size:19px;margin:0 0 9px;line-height:1.25;color:#f0d99f}.roster-rank-label{display:inline-block!important;border:1px solid #ad96504d;border-radius:4px;background:#d2b35b12;padding:3px 7px;font-size:11px!important;line-height:1.5}.roster-public-profile{font-size:12px}.roster-member-info dd{overflow-wrap:anywhere}.roster-socials{position:relative}.roster-socials a:focus-visible{outline:2px solid #e0c77d;outline-offset:3px}@media(max-width:700px){.roster-team{padding:12px!important}.roster-team h2{margin:-12px -12px 8px!important;padding:12px 15px;font-size:18px}.roster-team-table{display:block;border-spacing:0}.roster-team-table>tbody>tr>.roster-single-cell{width:100%}.roster-member-card{min-height:0;padding:15px!important}.roster-member-info h3{font-size:17px}.roster-team h2 small{font-size:10px}}
.roster-socials a{display:inline-flex;align-items:center;gap:6px;padding:5px 9px;font-size:11px;background:#102b21}.roster-socials .roster-social-icon{width:17px;height:17px;flex:none;color:#e0be71}.roster-socials a:hover{border-color:#d5b969;color:#fff0c7} /* Minecraft heads are resolved by username; no linked UUID is exposed in portrait URLs. */
.roster-minecraft-avatar{display:block;position:relative;width:72px;height:72px;overflow:hidden;border:1px solid #c7a75e;border-radius:7px;box-sizing:border-box;background:#203d2c;box-shadow:0 0 0 2px #091f17,0 0 0 3px #a08d4555;image-rendering:pixelated}.roster-minecraft-avatar .roster-skin-layer{position:absolute!important;display:block!important;width:800%!important;max-width:none!important;height:auto!important;border:0!important;border-radius:0!important;box-shadow:none!important;object-fit:initial!important;margin:0!important;padding:0!important;image-rendering:pixelated;top:-100%!important}.roster-minecraft-avatar .roster-skin-face{left:-100%!important}.roster-minecraft-avatar .roster-skin-hat{left:-500%!important}.roster-portrait>.roster-steve-avatar{object-fit:contain;image-rendering:pixelated}@media(max-width:700px){.roster-minecraft-avatar{width:62px;height:62px}}
.roster-biobox{margin-top:9px;padding:12px 14px;border:1px solid #8c804655;border-left:2px solid #bd9f58;border-radius:5px;background:linear-gradient(110deg,#173a2b,#102b21);box-shadow:inset 0 1px #d2b35c18}.roster-biobox>small{display:block;text-transform:uppercase;letter-spacing:.14em;font-size:9px;color:#d9bd78;margin-bottom:7px}.roster-biobox p{margin:0!important;color:#dce1cb!important;font-size:12px;line-height:1.7;overflow-wrap:anywhere}.roster-biobox .roster-bio-placeholder{color:#aebfa6!important;font-style:italic}.roster-biobox details{border-top:1px solid #49613f;margin-top:10px;padding-top:8px;font-size:11px}.roster-biobox summary{color:#d9bd78;cursor:pointer}.roster-biobox details p{margin-top:8px!important}
`;
const cardLayoutStyles=".roster-team-table>tbody>tr{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px}.roster-team-table>tbody>tr>td{display:block;width:auto!important;padding:0!important}.roster-team-table>tbody>tr>td.roster-single-cell{grid-column:1/-1}.roster-member-card{height:100%!important;box-sizing:border-box}.roster-member-info>.honour-panel{min-height:370px;box-sizing:border-box;margin:22px 0}.roster-department-panel ul{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;padding:0;list-style:none}.roster-department-panel ul li{padding:5px;background:#102b2455;border:1px solid #bca35b55;border-radius:8px;display:flex;justify-content:center}.roster-department-panel ul li img{width:72px;height:72px;max-width:100%;object-fit:contain}.roster-department-panel li{break-inside:avoid;margin-bottom:8px}.roster-member-info h3 a,.roster-member-info h3 a:visited{color:#f1d58a;text-decoration:none}.roster-member-info h3 a:hover{color:#fff1c9;text-decoration:underline}.roster-profile-link,.roster-profile-link:visited{display:inline-flex;align-items:center;margin:0 0 12px;padding:7px 12px;border:1px solid #9b874d;border-radius:6px;background:#102b24;color:#ead49b;text-decoration:none;font-size:12px;font-weight:600}.roster-profile-link:hover{background:#284737;color:#fff1c9;border-color:#e1c279}.roster-profile-link:focus-visible,.roster-member-info h3 a:focus-visible{outline:3px solid #f2d78b;outline-offset:4px}@media(max-width:700px){.roster-team-table>tbody>tr{grid-template-columns:1fr}.roster-member-info>.honour-panel{min-height:0}.roster-department-panel ul{columns:1}}";
export function createStaffRoster(db){return async request=>{
if(!['GET','HEAD'].includes(request.method))return new Response('Method not allowed',{status:405,headers:{Allow:'GET, HEAD'}});
if(new URL(request.url).pathname==='/staff-roster.css')return new Response(request.method==='HEAD'?null:styles+cardLayoutStyles,{headers:{'Content-Type':'text/css; charset=utf-8','Cache-Control':'no-cache'}});
const roster=await publicStaffRoster(db);
const bioBox=p=>{const bio=(p.publicProfile.bio||'').trim(),characters=Array.from(bio),long=characters.length>280;return '
Introduction '+esc(bio?(long?characters.slice(0,280).join('')+'…':bio):'Introduction coming soon.').replaceAll('\n',' ')+'
'+(long?'
Read full introduction '+esc(bio).replaceAll('\n',' ')+'
':'')+'
';};
const departmentForTeam={'Network staff':'network-staff','Dev team':'dev','Legal team':'legal','Player support':'support','Ban appeals':'ban-appeals','Forum team':'forum-support','Sales team':'sales'};
const closureBlock=(p,team)=>{const stats=p.closedTickets;if(!stats)return 'Resolved tickets Statistics unavailable
';const key=departmentForTeam[team],count=key?(stats.departments[key]||0):null;return 'Tickets closed '+stats.totalClosed+'
'+(key?'
'+esc(team)+': '+count+' · '+(count>=PERSONAL_DEPARTMENT_THRESHOLD?'Personal department seal earned':count+' / '+PERSONAL_DEPARTMENT_THRESHOLD+' for a personal department seal')+'
':'')+'
';};
const departmentPanel=p=>'DUNEREALMS · IMPERIAL SERVICE
Departments & teams '+p.teams.map(name=>{const seal=teams.find(t=>t[0]===name)?.[2]||(name==='News team'?'rank-streamer-150.png':'rank-network-staff-150.png');return ' ';}).join('')+' ';
const memberCard=(p,team)=>''+(p.portrait.mode==='skin'?'
':'
')+'
View profile & honours → '+bioBox(p)+(p.publicProfile.honours?.length?honourMarkup(p.publicProfile.honours,{showLore:false}):departmentPanel(p))+closureBlock(p,team)+(Object.keys(p.publicProfile.socials||{}).length?'
':'')+'
';
const groups=teams.map(([name,,seal])=>{const members=roster.filter(p=>p.teams.includes(name));if(!members.length)return'';const rows=[];for(let i=0;i'+members.slice(i,i+2).map(p=>''+memberCard(p,name)+' ').join('')+'');return ' '+esc(name)+''+members.length+' members ';}).join('');
const html='DuneRealms Staff Roster | Community Teams '+SHARED_HEAD+' '+SHARED_HEADER+'The Imperial Service Register
Meet the DuneRealms staff Behind every safe caravan stands a watchful crew. Meet the people caring for our community, its worlds and the journey ahead.
Current roles and team access are drawn from the account rank system.
'+(!roster.length?'No staff members are currently listed.
':'')+hierarchyMarkup(roster)+''+groups+'
';
return new Response(request.method==='HEAD'?null:html,{headers:{'Content-Type':'text/html; charset=utf-8','Cache-Control':'public, max-age=30'}});
};}