// === REDWOOD HORROR RADIO LOG SCRIPT (Final Form, Part 1: Days 2–19) ===

const STAGES = {
  2: "radio_log_2_given",
  3: "radio_log_3_given",
  4: "radio_log_4_given",
  5: "radio_log_5_given",
  6: "radio_log_6_given",
  7: "radio_log_7_given",
  8: "radio_log_8_given",
  9: "radio_log_9_given",
  10: "radio_log_10_given",
  11: "radio_log_11_given",
  12: "radio_log_12_given",
  13: "radio_log_13_given",
  14: "radio_log_14_given",
  15: "radio_log_15_given",
  16: "radio_log_16_given",
  17: "radio_log_17_given",
  18: "radio_log_18_given",
  19: "radio_log_19_given",
  20: "radio_log_20_given",
  21: "radio_log_21_given",
  22: "radio_log_22_given",
  23: "radio_log_23_given",
  24: "radio_log_24_given",
  25: "radio_log_25_given",
  26: "radio_log_26_given",
  27: "radio_log_27_given",
  28: "radio_log_28_given",
  29: "radio_log_29_given",
  30: "radio_log_30_given"
};

const DAY_TRACKING_STAGES = [10, 15, 23, 30];

const LOGS = {
  2: {
    title: "Radio Log 2",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nGood morning, Ranger. Supplies in the sector are scarce. Forage, hunt, but don’t wander too far from camp after dusk.\"}"
    ]
  },
  3: {
    title: "Radio Log 3",
    pages: [
      "{\"text\":\"[Ranger Unit 7 Log] – 2:40 AM. Heard snapping branches outside. Not an animal I know. Saw something at the treeline — tall, wrong somehow. Felt like it was watching. When I turned… nothing. Are there other patrols nearby?\"}",
      "{\"text\":\"[REPLY – TOWER COMMS]\\n\\nNegative. No patrols in your area.\\nProbably just a moose or wind through the trees. You’ve been out there a while, Ranger. Get some sleep. Don’t let the dark play tricks on you.\"}"
    ]
  },
  4: {
    title: "Radio Log 4",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nUnit 4 warns: caves aren’t safe. Stick to the open where you can see the sky. Don’t linger underground longer than you have to.\"}"
    ]
  },
  5: {
    title: "Radio Log 5",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nThe signal’s jittery today. If you hear anything strange, document it. Also, we started getting distress calles near you — it seems you really aren't alone.\"}"
    ]
  },
  6: {
    title: "Radio Log 6",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWeather scans show an incoming heavy fog. Might be a good day to fortify shelter and prepare for deadly nights. Who knows what will crawl out of the dark.\"}"
    ]
  },
  7: {
    title: "Radio Log 7",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWhispers intercepted on emergency frequencies last night. We assumed interference, but the words… weren’t in any language we know. Upgrade your tools. Stay sharp.\"}"
    ]
  },
  8: {
    title: "Radio Log 8",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nAbout 500 meters east of your position, something triggered motion sensors. We advise you to scout the area, but do not engage anything.\"}"
    ]
  },
  9: {
    title: "Radio Log 9",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nRanger Unit 2 went quiet today. Last contact he was talking about whisteling from outside. Be careful out there.\"}"
    ]
  },
  10: {
    title: "Radio Log 10",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\n[STATIC] Ranger? Can you hear me? Weird, the signal is on, but I don't hear you. Maybe the mic broke? No matter. Listen!\"}",
      "{\"text\":\" Upon your request 7 days ago, we sent more rangers out. However, they went silent a while ago. It’s been 5 days since they were dispatched. Locals report strange sounds—screeching, whispering... shadows moving.\"}",
      "{\"text\":\"Something is out there with you. We’ve dispatched a rescue team. Hold out 10 more days. Use your emergency flare when they arrive. Stay strong. Out.\"}"
    ]
  },
  11: {
    title: "Radio Log 11",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nEight campers went missing tonight. Signs of a slaughter. Something big and unlike anything we have seen before. Be careful until we can get you out of there.\"}"
    ]
  },
  12: {
    title: "Radio Log 12",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWe’re picking up echoes from your location. That shouldn’t happen unless someone is broadcasting nearby. Don’t investigate alone.\"}"
    ]
  },
  13: {
    title: "Radio Log 13",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nYou’ve survived nearly two weeks. Just 7 more nights before we get you out of there. Stay sharp.\"}"
    ]
  },
  14: {
    title: "Radio Log 14",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nThe birds stopped singing this morning. We don’t know why. Stay vigilant. And maybe gather a few extra torches.\"}"
    ]
  },
  15: {
    title: "Radio Log 15",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\n[STATIC] Ranger? Status report. We lost contact with the first rescue team. Their last ping came from the eastern trailhead.\"}",
      "{\"text\":\"A second team was prepared but refused to deploy. Something spooked them. These aren’t rookies, Ranger. They’re terrified.\"}",
      "{\"text\":\"We’re running out of options. If the signal cuts out again—just know we didn’t abandon you. Keep your head low. Watch the woods.\"}"
    ]
  },
  16: {
    title: "Radio Log 16",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nA heavy storm is coming your way again. Maybe stay inside or underground today. Hope its not just something intervering with our radars again.\"}"
    ]
  },
  17: {
    title: "Radio Log 17",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nEnergy spikes in your vicinity. We don’t know what it is, but it looks like hundreds of lifeforms around you suddenly appeared. Stay indoors.\"}"
    ]
  },
  18: {
    title: "Radio Log 18",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nLocals report more and more strange sightings. All deployed units have never come back. We are continuing to find corpses. I don't know anymore.\"}"
    ]
  },
  19: {
    title: "Radio Log 19",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\nJim’s recordings picked up something last night—voices in the static. Not echoes. Actual voices. Screams and the sound of eating... We had a bad feeling, so we sent a team over.\"}",
      "{\"text\":\"By the time the team arrived the source of the noise was gone. As if it was never there to begin with. Well, not quite. We found most of the first rescue team... or at least what was left of their bodies. Weirdly, one of the guys was missing completely.\"}",
      "{\"text\":\"We are still looking for him. For now, if you hear the woods whispering, do not respond. Do not follow. That is the last message the rescue team left. I hope you are alright out there. Try to stay sane in this nightmare. We are trying to get you out of there. Out.\"}"
    ]
  },
  20: {
    title: "Radio Log 20",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWatch for strange movement in the fog. We’ve lost visual on your area.\"}"
    ]
  },
  21: {
    title: "Radio Log 21",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWe are getting multiple life signs near you again. Too big to be an animal... It might be 'that'.\"}"
    ]
  },
  22: {
    title: "Radio Log 22",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nReports of mimicry increasing. They’re getting better at it. Double-check everything before you move.\"}"
    ]
  },
  23: {
    title: "Radio Log 23",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\nRanger... this is it. We can't reach you anymore. The last chopper sent into the valley never came back.\"}",
      "{\"text\":\"Everyone else is pulling out. They say the forest is cursed. They won’t even say your name anymore. They act like you were never even sent.\"}",
      "{\"text\":\"But I remember. If you can still hear this—run. Leave everything behind. Just run and don’t stop until the trees are gone.\"}"
    ]
  },
  24: {
    title: "Radio Log 24",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\nTHEY CAN MIMIC US. Ranger, return to shelter. Do not follow the last instructions! Whatever you do. DO NOT GO OUTSIDE. DON'T RUN. You can't leave the forest. Once you are in you CAN'T get out.\"}",
      "{\"text\":\"They want to play with you. Hunt you. Do not listen to them. I repeat, DO NOT RUN AWAY FROM YOUR SHELTER.\"}",
      "{\"text\":\"They listen to everything we say. I can't give you more information. STAY INSIDE. WATCH THE SHADOWS.\"}"
    ]
  },
  25: {
    title: "Radio Log 25",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nSomething moved past the camera facing your camp. It looked like you.\"}"
    ]
  },
  26: {
    title: "Radio Log 26",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWe recovered a body—wearing your uniform. It was malformed. Eyes... were wrong. Are you still out there or am I talking to a ghost?\"}"
    ]
  },
  27: {
    title: "Radio Log 27",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\nI... I heard your voice last night. You were calling for help over the static.\"}",
      "{\"text\":\"Except it wasn’t you. It laughed. Like it was trying to be you—but didn’t know how to feel human anymore. It's mimicking you now as well. Also, We found the missing person from rescue team 1. His body had morphed into some monster.\"}",
      "{\"text\":\"We already lost too many people to this forest. They know where you are, Ranger. They wear your face now. Whatever you see out there, it isn’t real. Don’t trust your eyes.\"}"
    ]
  },
  28: {
    title: "Radio Log 28",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nWe are again detecting multiple life signs near your camp. Highly recommend staying close to your camp. Don't move too far.\"}"
    ]
  },
  29: {
    title: "Radio Log 29",
    pages: [
      "{\"text\":\"[DAILY BRIEFING]\\n\\nLast night, the forest screamed. We lost contact with four more units. You are the last.\"}"
    ]
  },
  30: {
    title: "Radio Log 30",
    pages: [
      "{\"text\":\"[Radio Tower]\\n\\nRanger… we’ve been monitoring the signals all night. Something changed. The static is clearer now, and underneath it… we heard something new.\"}",
    "{\"text\":\"It didn’t sound like them. Not the whispers, not the laughter. This was… deliberate. Like someone was trying to reach you.\"}",
    "{\"text\":\"We couldn’t understand all of it, but parts were clear. Instructions. A way forward. Maybe even a way out.\"}",
    "{\"text\":\"Here’s what we managed to piece together:\\n\\nYou’ll need to gather:\\n\\n- 2 Diamond Blocks\\n- 6 Undead Skulls\\n- 1 Bed\"}",
    "{\"text\":\"It spoke of a pattern — precious stones under a place of rest surrounded by bones. When arranged correctly, it said, \\\"the forest will open.\\\"\"}",
    "{\"text\":\"We don’t know what lies beyond. The signal cut out before we could ask. But it sounded… different. Not like here. Maybe safer. Maybe not.\"}",
    "{\"text\":\"That’s all we have. Whatever this is, it’s the first real lead we’ve heard in weeks. If you’re going to try… now’s your chance. Trust is a weird concept... isn't it?\"}",
    ]
  }
};

// === RADIO LOG DISTRIBUTION EVENT ===
let lastProcessedDay = -1;

ServerEvents.tick(event => {
  const server = event.server;
  const day = Math.floor(server.overworld().getDayTime() / 24000);

  // === RADIO LOG SYSTEM ===
  if (day !== lastProcessedDay && LOGS[day]) {
    lastProcessedDay = day;

    server.players.forEach(player => {
      const stage = STAGES[day];

      if (!player.stages.has(stage)) {
        player.give(Item.of('minecraft:written_book', {
          title: LOGS[day].title,
          author: 'Unknown',
          pages: LOGS[day].pages
        }));

        // Radio static sound
        event.server.runCommandSilent(
          `execute as ${player.username} at ${player.username} run playsound veetoxic:radio_static ambient @s ~ ~ ~ 1 1`
        );

        player.stages.add(stage);

        // Keep your tracking system
        if (DAY_TRACKING_STAGES.includes(day)) {
          player.stages.add(`day_${day}`);
        }
      }
    });
  }

});