import {randomUUID} from 'node:crypto'; import {generateOrganisationKey,hashOrganisationKey,initializeOrganisationKeys} from './organisation-keys.mjs'; export const CLAN_CHARTER_SCHEMA=`CREATE TABLE IF NOT EXISTS site_clan_charters (clan_id CHAR(36) PRIMARY KEY,original_account_id CHAR(36) NOT NULL,key_id CHAR(36) NOT NULL UNIQUE,charter_kind VARCHAR(16) NOT NULL,created_at BIGINT NOT NULL,FOREIGN KEY(clan_id) REFERENCES site_clans(id))`; export const readClanCharter=async(db,id)=>(await db.prepare('SELECT charter_kind FROM site_clan_charters WHERE clan_id=?').bind(id).first())?.charter_kind||'basic'; export const heirClause="EXISTS (SELECT 1 FROM site_clan_charters entitlement WHERE entitlement.clan_id=site_clans.id AND entitlement.charter_kind IN ('original','royal'))"; export const heirJoinClause="EXISTS (SELECT 1 FROM site_clan_charters entitlement WHERE entitlement.clan_id=c.id AND entitlement.charter_kind IN ('original','royal'))"; export async function initializeClanCharters(db,{mysql=true}={}){ await db.prepare(CLAN_CHARTER_SCHEMA+(mysql?' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4':'')).run(); } // Explicit owner-approved live rollout, 17 Nov 2026. Exact account AND clan tag; // no name-based mass upgrades, no replacement codes on restart. Both grants are // applied atomically, so only their non-redeemable receipts need to be retained. export async function applyRoyalClanRollout(db){ await initializeOrganisationKeys(db); return db.transaction(async tx=>{ const clan=await tx.first("SELECT c.id,c.owner_id FROM site_clans c JOIN site_accounts a ON a.id=c.owner_id JOIN site_clan_members m ON m.clan_id=c.id AND m.user_id=a.id AND m.clan_role='owner' JOIN site_game_links l ON l.user_id=m.user_id AND l.minecraft_uuid=m.minecraft_uuid WHERE a.username='unimatrix999' AND c.tag='UART' AND c.active=1 FOR UPDATE"); if(!clan)return {applied:false,reason:'target-not-found'}; const scope='royal-rollout-2026-11-17:'+clan.id; const previous=await tx.first("SELECT id,target_id,used_at FROM organisation_keys WHERE account_id=? AND key_type='political-appointment' AND scope_id=? FOR UPDATE",[clan.owner_id,scope]); if(previous)return {applied:false,reason:'already-applied'}; // Never replace another entitlement or consume another account's original key. if(await tx.first('SELECT clan_id FROM site_clan_charters WHERE clan_id=? FOR UPDATE',[clan.id]))return {applied:false,reason:'existing-charter'}; if(await tx.first("SELECT id FROM organisation_keys WHERE account_id=? AND key_type='clan-founding' AND scope_id='original' FOR UPDATE",[clan.owner_id]))return {applied:false,reason:'existing-founding-key'}; const now=Date.now(),ids={}; for(const keyType of ['clan-founding','political-appointment']){ const code=generateOrganisationKey(keyType),id=randomUUID(),requestId=randomUUID();ids[keyType]=id; await tx.query('INSERT INTO organisation_keys(id,account_id,key_type,scope_id,code_hash,code_sealed,created_at,used_at,target_id,request_id) VALUES(?,?,?,?,?,?,?,?,?,?)',[id,clan.owner_id,keyType,keyType==='clan-founding'?'original':scope,hashOrganisationKey(code,keyType),'',now,now,clan.id,requestId]); for(const event of ['issued','used'])await tx.query('INSERT INTO organisation_key_events(id,key_id,account_id,event_type,target_id,created_at) VALUES(?,?,?,?,?,?)',[randomUUID(),id,clan.owner_id,event,clan.id,now]); } await tx.query("INSERT INTO site_clan_charters(clan_id,original_account_id,key_id,charter_kind,created_at) VALUES(?,?,?,'royal',?)",[clan.id,clan.owner_id,ids['clan-founding'],now]); await tx.query('UPDATE site_clans SET version=version+1,updated_at=? WHERE id=?',[now,clan.id]); await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),clan.owner_id,'clan-royal-rollout',JSON.stringify({clanId:clan.id,reason:'Explicit owner instruction',keyIds:Object.values(ids)}),now]); return {applied:true}; }); }