All files / src/routes config.js

91.45% Statements 353/386
91.48% Branches 258/282
97.56% Functions 40/41
92.3% Lines 324/351

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818                            3x 3x 3x 3x 3x 3x         129x                   129x         2322x             148x 142x 135x 129x       7x 7x 28x   28x 7x         112x         10x   7x 7x       3x       118x 118x 118x 112x 112x 112x       3x 137x 137x 136x   132x 132x 132x 132x 132x 132x   132x 132x 152x 152x 148x   129x 129x 11x 11x     129x                                 132x   132x   132x   121x     121x 121x 1x       131x   1x 1x         3x 5x 5x   4x 4x   3x                 3x 3x 3x 3x   3x                                               86x 86x 86x 86x                 80x   80x 80x 80x 80x               16x 15x 13x 13x       73x 8x 3x           70x 8x           8x 8x 2x     68x 5x 2x     66x       24x 24x 1x         23x 4x 1x           22x       22x 22x 1x         21x 6x 3x         3x 1x           17x       24x     24x             103x 2x   101x 7x   94x 2x   92x 2x   90x 2x   88x         76x         1x   75x 2x 1x           74x 74x         1x         73x                 103x 103x   88x 2x   86x 86x 86x 6x         80x     80x 80x 80x 4x           76x 76x   73x 73x   66x 24x 24x   59x         59x 59x 59x 885x 59x 826x 59x 767x 59x     708x 515x 193x 16x 177x       59x       3x 108x 108x   107x 107x 1x     106x 106x     106x 106x 108x 5x           103x 103x 44x     59x       57x     57x 68x   57x   8x 8x             49x 49x 51x 51x     49x   49x           49x               3x 8x 8x   7x 7x 1x     6x 5x 1x     4x 4x 1x     3x 1x     2x 2x 2x 2x 2x   2x   2x         2x   1x 1x             3x 13x 13x   12x 12x 1x     11x 10x 1x     9x 9x 1x     8x   8x   4x 4x   4x           4x     4x 4x 4x   4x   4x   5x       4x     1x 1x         3x 3x       3x 5x 5x 5x   3x 1x   2x 1x                             1x 1x   1x     2x 2x 2x                 3x 5x 5x   4x 4x   4x                                     4x 4x 64x     4x 1x       3x 3x 3x   3x   3x               3x                       3x 5x 5x   4x 4x     4x 4x 1x           3x 3x 15x   3x 1x     2x   2x               3x 1x 1x 1x 1x 1x               3x 1x 1x 1x 1x               3x 1x 1x     1x 1x               3x 1x 1x     1x 1x               3x 2x 2x 2x   2x                           3x  
/**
 * Config routes — read-only app/economy/moderation configuration.
 *
 * GET /api/config/:key     -> Get a config value (app, economy, moderation)
 * PUT /api/config/:key     -> Admin update config (merge)
 * GET /api/gifts           -> Get gift catalog (store-visible)
 * GET /api/gifts/all       -> Get all gifts (including hidden)
 * GET /api/coin-packages   -> Get active coin packages
 * GET /api/broadcasts      -> Get recent broadcasts
 * GET /api/gift-rankings/:giftId -> Get gift rankings
 * PUT /api/config/economy  -> Admin update economy config (merge)
 * DELETE /api/config/startingScreens/:screenId -> Admin delete a starting screen
 */
 
const crypto = require('node:crypto');
const router = require('express').Router();
const { db } = require('../utils/firebase');
const { requireAdmin } = require('../middleware/auth');
const { queryDocs } = require('../utils/firestore-helpers');
const log = require('../utils/log');
 
// -- Starting screens helpers --
 
function computeContentHash(screen) {
  const hashFields = {
    title: screen.title,
    message: screen.message,
    template: screen.template,
    imageType: screen.imageType || null,
    backgroundImage: screen.backgroundImage || null,
    backgroundImageFit: screen.backgroundImageFit || 'cover',
    dismissable: screen.dismissable,
    frequency: screen.frequency,
  };
  return crypto
    .createHash('sha256')
    .update(
      JSON.stringify(
        hashFields,
        Object.keys(hashFields).sort((a, b) => a.localeCompare(b)),
      ),
    )
    .digest('hex');
}
 
function isScreenActive(screen, now) {
  if (!screen.enabled) return false;
  if (screen.startDate && new Date(screen.startDate).getTime() > now) return false;
  if (screen.endDate && new Date(screen.endDate).getTime() <= now) return false;
  return true;
}
 
function cidrMatch(ip, cidr) {
  const [range, bits] = cidr.split('/');
  const mask = ~(2 ** (32 - Number.parseInt(bits, 10)) - 1) >>> 0;
  const ipNum = ip.split('.').reduce((acc, oct) => (acc << 8) + Number.parseInt(oct, 10), 0) >>> 0;
  const rangeNum =
    range.split('.').reduce((acc, oct) => (acc << 8) + Number.parseInt(oct, 10), 0) >>> 0;
  return (ipNum & mask) === (rangeNum & mask);
}
 
function normalizeIp(ip) {
  // Strip IPv6-mapped IPv4 prefix (e.g. ::ffff:127.0.0.1 → 127.0.0.1)
  Eif (ip?.startsWith('::ffff:')) return ip.slice(7);
  return ip;
}
 
function matchesNetwork(normalizedIp, network) {
  if (network.includes('/')) {
    // Only attempt CIDR match if both IP and network are IPv4
    Eif (!normalizedIp.includes(':') && !network.includes(':')) {
      return cidrMatch(normalizedIp, network);
    }
    return false; // Skip IPv6 CIDR matching (not implemented)
  }
  return normalizedIp === network;
}
 
function isAllowlisted(screen, deviceId, ip) {
  Iif (!screen.allowlist) return false;
  const { deviceIds = [], networks = [] } = screen.allowlist;
  if (deviceId && deviceIds.includes(deviceId)) return true;
  const normalizedIp = normalizeIp(ip);
  Iif (!normalizedIp) return false;
  return networks.some((network) => matchesNetwork(normalizedIp, network));
}
 
// -- Get starting screens (public, auth-exempt) --
router.get('/config/startingScreens', async (req, res) => {
  try {
    const snap = await db.doc('config/startingScreens').get();
    if (!snap.exists) return res.json({});
 
    const allScreens = snap.data();
    const now = Date.now();
    const deviceId = req.headers['x-device-id'];
    const ip = req.ip;
    const result = {};
    let hasAllowlistOverride = false;
 
    const sortedIds = Object.keys(allScreens).sort((a, b) => a.localeCompare(b));
    for (const id of sortedIds) {
      const screen = allScreens[id];
      if (screen.deleted) continue;
      if (!isScreenActive(screen, now)) continue;
 
      let dismissable = screen.dismissable;
      if (screen.dismissable === false && isAllowlisted(screen, deviceId, ip)) {
        hasAllowlistOverride = true;
        dismissable = true;
      }
 
      result[id] = {
        enabled: screen.enabled,
        dismissable,
        frequency: screen.frequency,
        template: screen.template,
        title: screen.title,
        message: screen.message,
        imageType: screen.imageType || null,
        backgroundImage: screen.backgroundImage || null,
        backgroundImageFit: screen.backgroundImageFit || 'cover',
        startDate: screen.startDate || null,
        endDate: screen.endDate || null,
        contentHash: computeContentHash(screen),
        lastModifiedAt: screen.lastModifiedAt || null,
      };
    }
 
    res.set('X-Content-Type-Options', 'nosniff');
 
    res.set('Cache-Control', 'public, max-age=60');
 
    if (!hasAllowlistOverride) {
      const etag =
        '"' +
        crypto.createHash('sha256').update(JSON.stringify(result)).digest('hex').slice(0, 16) +
        '"';
      res.set('ETag', etag);
      if (req.headers['if-none-match'] === etag) {
        return res.status(304).end();
      }
    }
 
    return res.json(result);
  } catch (err) {
    log.error('config', 'Error fetching starting screens', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get starting screens for admin (includes allowlist + lastModifiedBy) --
router.get('/config/startingScreens/admin', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const snap = await db.doc('config/startingScreens').get();
    if (!snap.exists) return res.json({});
 
    return res.json(snap.data());
  } catch (err) {
    log.error('config', 'Error fetching starting screens (admin)', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Starting screens PUT helpers --
 
const VALID_FREQUENCIES = ['every_launch', 'once'];
const VALID_TEMPLATES = ['warning', 'promotional', 'announcement', 'info'];
const VALID_IMAGE_TYPES = ['police_duck'];
const SCREEN_ID_REGEX = /^[a-zA-Z0-9_-]+$/;
 
const SCREEN_FIELDS = [
  'enabled',
  'dismissable',
  'frequency',
  'template',
  'title',
  'message',
  'imageType',
  'backgroundImage',
  'backgroundImageFit',
  'startDate',
  'endDate',
  'allowlist',
  'deleted',
  'deletedAt',
  'deletedBy',
];
 
/**
 * Strip zero-width chars except ZWJ (U+200D), trim, NFC normalise.
 */
function sanitiseTitle(title) {
  // Remove zero-width chars: U+200B, U+200C, U+200E, U+200F, U+FEFF, U+2060
  // Keep U+200D (ZWJ)
  let result = title.replaceAll(/[\u200B\u200C\u200E\u200F\uFEFF\u2060]/g, '');
  result = result.trim();
  result = result.normalize('NFC');
  return result;
}
 
/**
 * Strip control chars except \n \r \t, collapse >2 consecutive newlines to 2, trim, NFC normalise.
 */
function sanitiseMessage(message) {
  // Remove control characters except \n (0x0A), \r (0x0D), \t (0x09)
  // eslint-disable-next-line no-control-regex
  let result = message.replaceAll(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '');
  // Collapse >2 consecutive newlines to 2
  result = result.replaceAll(/\n{3,}/g, '\n\n');
  result = result.trim();
  result = result.normalize('NFC');
  return result;
}
 
/**
 * Validate an ISO 8601 date string with time component ('T' required).
 * Returns true if valid, false otherwise.
 */
function isValidIsoDate(str) {
  if (typeof str !== 'string') return false;
  if (!str.includes('T')) return false;
  const d = new Date(str);
  return !Number.isNaN(d.getTime());
}
 
function validateDates(id, screen, existingEndDate) {
  if (screen.startDate !== null && screen.startDate !== undefined) {
    if (!isValidIsoDate(screen.startDate)) {
      return {
        error: `Screen "${id}": startDate must be a valid ISO 8601 string with time`,
        field: 'startDate',
      };
    }
  }
  if (screen.endDate !== null && screen.endDate !== undefined) {
    Iif (!isValidIsoDate(screen.endDate)) {
      return {
        error: `Screen "${id}": endDate must be a valid ISO 8601 string with time`,
        field: 'endDate',
      };
    }
    const endDateChanged = screen.endDate !== existingEndDate;
    if (endDateChanged && new Date(screen.endDate).getTime() <= Date.now()) {
      return { error: `Screen "${id}": endDate must be in the future`, field: 'endDate' };
    }
  }
  if (screen.startDate && screen.endDate) {
    if (new Date(screen.startDate).getTime() >= new Date(screen.endDate).getTime()) {
      return { error: `Screen "${id}": startDate must be before endDate`, field: 'startDate' };
    }
  }
  return null;
}
 
function validateDeviceIds(id, deviceIds) {
  Iif (deviceIds === undefined) return null;
  if (!Array.isArray(deviceIds)) {
    return {
      error: `Screen "${id}": allowlist.deviceIds must be an array`,
      field: 'allowlist.deviceIds',
    };
  }
  for (const did of deviceIds) {
    if (typeof did !== 'string' || did === '') {
      return {
        error: `Screen "${id}": allowlist.deviceIds must contain non-empty strings`,
        field: 'allowlist.deviceIds',
      };
    }
  }
  return null;
}
 
function validateNetworks(id, networks) {
  Iif (networks === undefined) return null;
  if (!Array.isArray(networks)) {
    return {
      error: `Screen "${id}": allowlist.networks must be an array`,
      field: 'allowlist.networks',
    };
  }
  for (const net of networks) {
    if (typeof net !== 'string' || net === '') {
      return {
        error: `Screen "${id}": each allowlist network must be a non-empty string`,
        field: 'allowlist.networks',
      };
    }
    if (net.includes('/') && net.split('/')[1] === '0') {
      return {
        error: `Screen "${id}": CIDR /0 not allowed in allowlist.networks`,
        field: 'allowlist.networks',
      };
    }
  }
  return null;
}
 
function validateAllowlist(id, allowlist) {
  Iif (typeof allowlist !== 'object' || Array.isArray(allowlist)) {
    return { error: `Screen "${id}": allowlist must be an object`, field: 'allowlist' };
  }
  return (
    validateDeviceIds(id, allowlist.deviceIds) || validateNetworks(id, allowlist.networks) || null
  );
}
 
/** Validate required screen fields (enabled, dismissable, frequency, template). */
function validateRequiredScreenFields(id, screen) {
  if (!screen || typeof screen !== 'object' || Array.isArray(screen)) {
    return { error: `Screen "${id}" must be an object`, field: id };
  }
  if (typeof screen.enabled !== 'boolean') {
    return { error: `Screen "${id}": enabled must be a boolean`, field: 'enabled' };
  }
  if (typeof screen.dismissable !== 'boolean') {
    return { error: `Screen "${id}": dismissable must be a boolean`, field: 'dismissable' };
  }
  if (!VALID_FREQUENCIES.includes(screen.frequency)) {
    return { error: `Screen "${id}": invalid frequency`, field: 'frequency' };
  }
  if (!VALID_TEMPLATES.includes(screen.template)) {
    return { error: `Screen "${id}": invalid template`, field: 'template' };
  }
  return null;
}
 
/** Validate optional screen fields (imageType, backgroundImage, backgroundImageFit). */
function validateOptionalScreenFields(id, screen) {
  if (
    screen.imageType !== null &&
    screen.imageType !== undefined &&
    !VALID_IMAGE_TYPES.includes(screen.imageType)
  ) {
    return { error: `Screen "${id}": invalid imageType`, field: 'imageType' };
  }
  if (screen.backgroundImage !== null && screen.backgroundImage !== undefined) {
    if (typeof screen.backgroundImage !== 'string' || screen.backgroundImage === '') {
      return {
        error: `Screen "${id}": backgroundImage must be a non-empty string or null`,
        field: 'backgroundImage',
      };
    }
  }
  const VALID_BG_FITS = ['cover', 'contain', '100% 100%'];
  if (
    screen.backgroundImageFit !== null &&
    screen.backgroundImageFit !== undefined &&
    !VALID_BG_FITS.includes(screen.backgroundImageFit)
  ) {
    return {
      error: `Screen "${id}": backgroundImageFit must be "cover", "contain", or "100% 100%"`,
      field: 'backgroundImageFit',
    };
  }
  return null;
}
 
/**
 * Validate a single screen entry.
 * On success: returns { sanitisedTitle, sanitisedMessage }.
 * On failure: returns { error, field }.
 */
function validateScreen(id, screen, existingEndDate) {
  const requiredErr = validateRequiredScreenFields(id, screen);
  if (requiredErr) return requiredErr;
 
  if (typeof screen.title !== 'string') {
    return { error: `Screen "${id}": title must be a string`, field: 'title' };
  }
  const sanitisedTitle = sanitiseTitle(screen.title);
  const titleLength = [...sanitisedTitle].length;
  if (titleLength < 3 || titleLength > 100) {
    return {
      error: `Screen "${id}": title must be 3-100 characters (got ${titleLength})`,
      field: 'title',
    };
  }
  Iif (typeof screen.message !== 'string') {
    return { error: `Screen "${id}": message must be a string`, field: 'message' };
  }
  const sanitisedMessage = sanitiseMessage(screen.message);
  const messageLength = [...sanitisedMessage].length;
  if (messageLength < 10 || messageLength > 500) {
    return {
      error: `Screen "${id}": message must be 10-500 characters (got ${messageLength})`,
      field: 'message',
    };
  }
 
  const optionalErr = validateOptionalScreenFields(id, screen);
  if (optionalErr) return optionalErr;
 
  const dateErr = validateDates(id, screen, existingEndDate);
  if (dateErr) return dateErr;
 
  if (screen.allowlist !== null && screen.allowlist !== undefined) {
    const allowlistErr = validateAllowlist(id, screen.allowlist);
    if (allowlistErr) return allowlistErr;
  }
  return { sanitisedTitle, sanitisedMessage };
}
 
/** Build a clean screen object from validated data. */
function buildCleanScreen(screen, result) {
  const clean = {};
  const NULL_DEFAULT_FIELDS = new Set(['imageType', 'backgroundImage', 'startDate', 'endDate']);
  for (const field of SCREEN_FIELDS) {
    if (field === 'title') {
      clean.title = result.sanitisedTitle;
    } else if (field === 'message') {
      clean.message = result.sanitisedMessage;
    } else if (field === 'allowlist') {
      clean.allowlist = screen.allowlist
        ? { deviceIds: screen.allowlist.deviceIds || [], networks: screen.allowlist.networks || [] }
        : { deviceIds: [], networks: [] };
    } else if (field in screen) {
      clean[field] = screen[field];
    } else if (field === 'backgroundImageFit') {
      clean[field] = 'cover';
    } else Iif (NULL_DEFAULT_FIELDS.has(field)) {
      clean[field] = null;
    }
  }
  return clean;
}
 
// -- Update starting screens (admin) --
router.put('/config/startingScreens', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const body = req.body;
    if (!body || typeof body !== 'object' || Array.isArray(body)) {
      return res.status(400).json({ error: 'Request body must be a JSON object' });
    }
 
    const snap = await db.doc('config/startingScreens').get();
    const existing = snap.exists ? snap.data() : {};
 
    // Validate all screen entries first
    const validatedScreens = {};
    for (const [id, screen] of Object.entries(body)) {
      if (!id || !SCREEN_ID_REGEX.test(id)) {
        return res.status(400).json({
          error: `Invalid screen ID: "${id}". Must match ${SCREEN_ID_REGEX}`,
          field: 'screenId',
        });
      }
 
      const result = validateScreen(id, screen, existing[id]?.endDate || undefined);
      if (result.error) {
        return res.status(400).json({ error: result.error, field: result.field });
      }
 
      validatedScreens[id] = buildCleanScreen(screen, result);
    }
 
    // Build merged state
    const merged = { ...existing, ...validatedScreens };
 
    // Blocking constraint: max 1 non-dismissable screen enabled at a time
    const nonDismissable = Object.keys(merged).filter(
      (id) => merged[id].enabled && merged[id].dismissable === false && !merged[id].deleted,
    );
    if (nonDismissable.length > 1) {
      const existingBlocker =
        nonDismissable.find((id) => !(id in validatedScreens)) || nonDismissable[0];
      return res.status(409).json({
        error: 'Only one non-dismissable screen can be enabled at a time',
        existingBlocker,
      });
    }
 
    // Set audit fields
    const now = new Date().toISOString();
    for (const id of Object.keys(validatedScreens)) {
      merged[id].lastModifiedBy = req.auth.uniqueId;
      merged[id].lastModifiedAt = now;
    }
 
    await db.doc('config/startingScreens').set(merged);
 
    log.info('config', 'Starting screens updated', {
      updatedIds: Object.keys(validatedScreens),
      totalScreens: Object.keys(merged).length,
      admin: req.auth.uniqueId,
    });
 
    return res.json({ success: true, updated: Object.keys(validatedScreens) });
  } catch (err) {
    log.error('config', 'Error updating starting screens', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Restore a soft-deleted starting screen (admin) --
router.post('/config/startingScreens/:screenId/restore', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const { screenId } = req.params;
    if (!screenId || !/^[a-zA-Z0-9_-]+$/.test(screenId)) {
      return res.status(400).json({ error: 'Invalid screen ID' });
    }
 
    const snap = await db.doc('config/startingScreens').get();
    if (!snap.exists) {
      return res.status(404).json({ error: 'No starting screens configured' });
    }
 
    const existing = snap.data();
    if (!(screenId in existing)) {
      return res.status(404).json({ error: `Screen "${screenId}" not found` });
    }
 
    if (!existing[screenId].deleted) {
      return res.status(400).json({ error: `Screen "${screenId}" is not deleted` });
    }
 
    existing[screenId].deleted = false;
    delete existing[screenId].deletedAt;
    delete existing[screenId].deletedBy;
    existing[screenId].restoredAt = new Date().toISOString();
    existing[screenId].restoredBy = req.auth.uniqueId;
 
    await db.doc('config/startingScreens').set(existing);
 
    log.info('config', 'Starting screen restored', {
      screenId,
      admin: req.auth.uniqueId,
    });
 
    return res.json({ success: true, restored: screenId });
  } catch (err) {
    log.error('config', 'Error restoring starting screen', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Delete a single starting screen (admin) --
// Without ?permanent=true: soft-delete (sets deleted: true)
// With ?permanent=true: hard-delete (removes from Firestore)
router.delete('/config/startingScreens/:screenId', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const { screenId } = req.params;
    if (!screenId || !/^[a-zA-Z0-9_-]+$/.test(screenId)) {
      return res.status(400).json({ error: 'Invalid screen ID' });
    }
 
    const snap = await db.doc('config/startingScreens').get();
    if (!snap.exists) {
      return res.status(404).json({ error: 'No starting screens configured' });
    }
 
    const existing = snap.data();
    if (!(screenId in existing)) {
      return res.status(404).json({ error: `Screen "${screenId}" not found` });
    }
 
    const permanent = req.query.permanent === 'true';
 
    if (permanent) {
      // Hard-delete: remove from Firestore entirely
      delete existing[screenId];
      await db.doc('config/startingScreens').set(existing);
 
      log.info('config', 'Starting screen permanently deleted', {
        screenId,
        remainingScreens: Object.keys(existing).length,
        admin: req.auth.uniqueId,
      });
 
      return res.json({ success: true, deleted: screenId, permanent: true });
    } else {
      // Soft-delete: mark as deleted
      existing[screenId].deleted = true;
      existing[screenId].deletedAt = new Date().toISOString();
      existing[screenId].deletedBy = req.auth.uniqueId;
 
      await db.doc('config/startingScreens').set(existing);
 
      log.info('config', 'Starting screen soft-deleted', {
        screenId,
        remainingScreens: Object.keys(existing).filter((id) => !existing[id]?.deleted).length,
        admin: req.auth.uniqueId,
      });
 
      return res.json({ success: true, deleted: screenId, permanent: false });
    }
  } catch (err) {
    log.error('config', 'Error deleting starting screen', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- 405 catch-all for startingScreens --
router.all('/config/startingScreens', (req, res) => {
  return res.status(405).json({ error: `Method ${req.method} not allowed` });
});
 
// -- Get config value --
router.get('/config/:key', async (req, res) => {
  try {
    const snap = await db.doc(`config/${req.params.key}`).get();
    if (!snap.exists) {
      // Return defaults for known config keys
      if (req.params.key === 'app') {
        return res.json({ minVersionCode: 1, latestVersionCode: 1, latestVersionName: '' });
      }
      if (req.params.key === 'economy') {
        const defaults = {
          beanConversionRate: 0.6,
          beanRedeemBonusThreshold: 2000,
          beanRedeemBonusMultiplier: 1.1,
          pullCosts: { 1: 10, 10: 100, 100: 1000 },
          broadcastSendThreshold: 0,
          broadcastWinThreshold: 5000,
          dropRateExponent: 1.5,
          pitySoftStart: 80,
          pityHardLimit: 120,
          pitySoftMaxShift: 0.15,
          pityHighValueThreshold: 5000,
          dailyBase: 50,
          milestoneRewards: { 7: 100, 14: 200, 30: 500, 60: 1000, 90: 2000 },
        };
        await db.doc('config/economy').set(defaults);
        return res.json(defaults);
      }
      return res.status(404).json({ error: 'Config not found' });
    }
    // Return plain config object
    const config = snap.data();
    res.set('Cache-Control', 'public, max-age=300');
    return res.json(config);
  } catch (err) {
    log.error('config', 'Error fetching config', { key: req.params.key, error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Economy config (admin, with field whitelist) --
// Must be defined BEFORE the generic PUT /config/:key route
router.put('/config/economy', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const body = req.body;
    Iif (!body) return res.status(400).json({ error: 'Invalid JSON body' });
 
    const ECONOMY_CONFIG_FIELDS = [
      'beanConversionRate',
      'beanRedeemBonusThreshold',
      'beanRedeemBonusMultiplier',
      'pullCosts',
      'broadcastSendThreshold',
      'broadcastWinThreshold',
      'dropRateExponent',
      'pitySoftStart',
      'pityHardLimit',
      'pitySoftMaxShift',
      'pityHighValueThreshold',
      'dailyBase',
      'milestoneRewards',
      'wheelInnerThreshold',
      'maxRoomDurationMinutes',
      'superShyRoomDurationMinutes',
    ];
 
    const filtered = {};
    for (const key of ECONOMY_CONFIG_FIELDS) {
      if (key in body) filtered[key] = body[key];
    }
 
    if (Object.keys(filtered).length === 0) {
      return res.status(400).json({ error: 'No valid economy config fields' });
    }
 
    // Merge with existing config
    const snap = await db.doc('config/economy').get();
    const currentConfig = snap.exists ? snap.data() : {};
    const merged = { ...currentConfig, ...filtered };
 
    await db.doc('config/economy').set(merged);
 
    return res.json(merged);
  } catch (err) {
    log.error('config', 'Error updating economy config', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// Allowed fields per config key to prevent mass assignment
const CONFIG_ALLOWED_FIELDS = {
  app: [
    'minVersionCode',
    'latestVersionCode',
    'latestVersionName',
    'maintenanceMode',
    'maintenanceMessage',
  ],
  moderation: ['maxWarnings', 'suspensionDays', 'autoModEnabled', 'bannedWords', 'reportThreshold'],
};
 
// -- Update config value (admin) --
router.put('/config/:key', async (req, res) => {
  try {
    if (requireAdmin(req, res)) return;
 
    const body = req.body;
    Iif (!body || typeof body !== 'object')
      return res.status(400).json({ error: 'Invalid JSON body' });
 
    const allowedFields = CONFIG_ALLOWED_FIELDS[req.params.key];
    if (!allowedFields) {
      return res.status(400).json({
        error: `Unknown config key: ${req.params.key}. Use a dedicated endpoint for economy config.`,
      });
    }
 
    // Filter to only allowed fields
    const filtered = {};
    for (const field of allowedFields) {
      if (field in body) filtered[field] = body[field];
    }
    if (Object.keys(filtered).length === 0) {
      return res.status(400).json({ error: 'No valid fields provided' });
    }
 
    await db.doc(`config/${req.params.key}`).set(filtered, { merge: true });
 
    return res.json({ success: true });
  } catch (err) {
    log.error('config', 'Error updating config', { key: req.params.key, error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get gift catalog (store-visible) --
router.get('/gifts', async (req, res) => {
  try {
    const results = await queryDocs(db.collection('gifts').where('showInStore', '==', true));
    results.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
    res.set('Cache-Control', 'public, max-age=300');
    return res.json(results);
  } catch (err) {
    log.error('config', 'Error fetching gifts', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get all gifts (including hidden) --
router.get('/gifts/all', async (req, res) => {
  try {
    const results = await queryDocs(db.collection('gifts'));
    results.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
    return res.json(results);
  } catch (err) {
    log.error('config', 'Error fetching all gifts', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get active coin packages --
router.get('/coin-packages', async (req, res) => {
  try {
    const results = await queryDocs(
      db.collection('coinPackages').where('isActive', '==', true).orderBy('order'),
    );
    res.set('Cache-Control', 'public, max-age=300');
    return res.json(results);
  } catch (err) {
    log.error('config', 'Error fetching coin packages', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get recent broadcasts --
router.get('/broadcasts', async (req, res) => {
  try {
    const results = await queryDocs(
      db.collection('broadcasts').orderBy('timestamp', 'desc').limit(50),
    );
    res.set('Cache-Control', 'public, max-age=60');
    return res.json(results);
  } catch (err) {
    log.error('config', 'Error fetching broadcasts', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// -- Get gift rankings --
router.get('/gift-rankings/:giftId', async (req, res) => {
  try {
    const snap = await db.doc(`giftRankings/${req.params.giftId}`).get();
    const doc = snap.exists ? snap.data() : null;
 
    return res.json({
      rankings: doc?.rankings || [],
      totalSent: doc?.totalSent || 0,
      lastUpdated: doc?.lastUpdated || null,
    });
  } catch (err) {
    log.error('config', 'Error fetching gift rankings', {
      giftId: req.params.giftId,
      error: err.message,
    });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
module.exports = router;