games:
  - type: bug-hunt
    title: "Code Smell Detective"
    snippets:
      - code: |
          function process(d) {
            if (d < 18) return "minor";
            if (d < 65) return "adult";
            return "senior";
          }
          const result = process(25);
        bugLine: 2
        explanation: "Magic number 18 — age thresholds should be named constants (e.g. MIN_ADULT_AGE) so intent is clear and changes are centralized."
        hint: "What does the number 18 represent?"
      - code: |
          function doStuff(a, b, c, x, y) {
            const t = a * x + b * y;
            const r = c ? t * 1.1 : t;
            return Math.round(r * 100) / 100;
          }
        bugLine: 2
        explanation: "God function — does too much: computes a formula, applies a flag-based adjustment, and formats. Split into focused functions with descriptive names."
        hint: "How many responsibilities does this function have?"
      - code: |
          const d = new Date();
          const x = users.filter(u => u.active).length;
          const discount = price > 100 ? 0.1 : 0.05;
        bugLine: 1
        explanation: "Unclear variable names — 'd', 'x' tell nothing. Use descriptive names: currentDate, activeUserCount, discountRate."
        hint: "What does 'd' or 'x' convey to a reader?"
      - code: |
          function sendEmail(user, body, notify = true) {
            if (notify) {
              log("Sent to " + user.email);
            }
            return mailer.send(user.email, body);
          }
        bugLine: 2
        explanation: "Boolean parameter — 'notify' obscures intent. Prefer separate methods (sendEmail vs sendEmailQuietly) or an options object."
        hint: "What does notify=true mean at the call site?"
      - code: |
          function calcTotal(items) {
            let t = 0;
            for (let i = 0; i < items.length; i++) {
              if (items[i].onSale) {
                t += items[i].price * 0.8;
              } else {
                t += items[i].price;
              }
            }
            return t;
          }
        bugLine: 5
        explanation: "Magic number 0.8 — discount factor should be a named constant (e.g. SALE_DISCOUNT = 0.8) for clarity and easy maintenance."
        hint: "What does 0.8 mean in this context?"
      - code: |
          function validateOrder(order) {
            if (order.items) {
              if (order.items.length > 0) {
                if (order.shipping) {
                  if (order.shipping.address) {
                    return true;
                  }
                }
              }
            }
            return false;
          }
        bugLine: 3
        explanation: "Deeply nested conditionals — hard to follow. Use guard clauses or early returns to flatten and improve readability."
        hint: "How many levels of nesting?"
      - code: |
          function fetchUser(id) {
            const user = db.query("SELECT * FROM users WHERE id = ?", id);
            return user;
            console.log("Fetched user:", id);
          }
        bugLine: 4
        explanation: "Dead code — the console.log is unreachable after the return statement. Remove it or move before the return."
        hint: "Can this line ever execute?"
      - code: |
          const data = await fetch("/api/config");
          const cfg = JSON.parse(data);
          const t = cfg.timeout || 5000;
          return t;
        bugLine: 3
        explanation: "Magic number 5000 — default timeout should be a named constant (e.g. DEFAULT_TIMEOUT_MS) so milliseconds intent is clear."
        hint: "What unit is 5000? Is that obvious?"

  - type: speed-round
    title: "Clean Code Quick Fire"
    rounds:
      - question: "Which variable name is better for a user's email address?"
        options:
          - "e"
          - "em"
          - "userEmail"
          - "ue"
        answer: 2
        timeLimit: 14
      - question: "What code smell does a function with 8 parameters and 50 lines typically have?"
        options:
          - "Magic numbers"
          - "Dead code"
          - "God function / too many responsibilities"
          - "Unclear naming"
        answer: 2
        timeLimit: 15
      - question: "A literal number 86400000 appears in code. What should you do?"
        options:
          - "Leave it as is — it's self-explanatory"
          - "Replace with a named constant (e.g. MS_PER_DAY)"
          - "Add a comment explaining it"
          - "Move it to a config file only"
        answer: 1
        timeLimit: 16
      - question: "Which is the better practice for a boolean parameter like sendNotification?"
        options:
          - "Keep the boolean — it's clear"
          - "Split into two functions (e.g. send vs sendQuietly)"
          - "Use 0 or 1 instead"
          - "Add more boolean parameters for related options"
        answer: 1
        timeLimit: 15
      - question: "You find code that runs after a return statement. What is it?"
        options:
          - "A feature for logging"
          - "Dead code"
          - "A fallback path"
          - "Defensive programming"
        answer: 1
        timeLimit: 12
      - question: "What should you refactor first when improving readability?"
        options:
          - "Add more comments"
          - "Rename unclear variables and extract magic numbers"
          - "Increase indentation for clarity"
          - "Split the file into more files"
        answer: 1
        timeLimit: 16
      - question: "A function has 4 levels of nested if statements. What's the main issue?"
        options:
          - "Too many parameters"
          - "Deep nesting reduces readability — use guard clauses"
          - "Missing type annotations"
          - "Wrong programming language"
        answer: 1
        timeLimit: 15
      - question: "Which name better describes a function that formats a price for display?"
        options:
          - "doIt"
          - "format"
          - "formatPriceForDisplay"
          - "fn"
        answer: 2
        timeLimit: 14
      - question: "Code has numbers 30, 60, 90 scattered throughout. What smell is this?"
        options:
          - "Dead code"
          - "Magic numbers — extract to named constants"
          - "Bad naming"
          - "Long function"
        answer: 1
        timeLimit: 14
      - question: "A 200-line function validates input, transforms data, calls 5 APIs, and formats output. What's wrong?"
        options:
          - "Nothing — monoliths are fine"
          - "God function — does too many things; should be split"
          - "Needs more comments"
          - "Should use a different framework"
        answer: 1
        timeLimit: 16
