// Brass & Sigil — first-join welcome grant
//
// Gives each new player exactly 1 Waystone + 1 server manual book on
// first login. Gated by a per-player persistent flag so it fires once
// even if the player relogs.
//
// Future: replace the vanilla written book with a Patchouli book in a
// custom tweak jar once we have enough content to warrant the rich
// formatting. Patchouli is already in the pack so swapping later is
// purely a content+recipe change, no mod-add needed.

const WELCOME_FLAG = 'bnsWelcomeGranted';

const MANUAL_PAGES = [
    'Welcome to Brass & Sigil.\n\nThis manual covers the basics. Read it once, then keep it or toss it.',
    'WAYSTONES\n\nYou have one Waystone item. Place it at your base -- it is the only one you get.\n\nNeed to travel further? Build trains, roads, or Create Aeronautics planes.',
    'MONEY\n\nCoins cannot be crafted.\n\nTake valuables (diamonds etc.) to the bank at spawn. The exchange will trade them for coins.\n\nDynamic rates are planned.',
    'SPAWN PLOTS\n\nComing soon: buy a small commercial plot at spawn to set up your shop. One plot per player.\n\nYour home base goes OUT in the world -- claim it with FTB Chunks (free, anywhere).',
    'RULES\n\n- Be excellent to each other.\n- No griefing.\n- Lost your Waystone? Ask an OP or buy one from the bank (soon).\n- Report bugs to staff.\n\nHave fun.',
];

function giveManual(player) {
    // 1.21 components-based written book: pages are filterable text
    // components, title supports the same. Plain strings are accepted.
    const book = Item.of('minecraft:written_book');
    book.set('minecraft:written_book_content', {
        title: { raw: 'Brass & Sigil Manual' },
        author: 'Server Staff',
        pages: MANUAL_PAGES.map(text => ({ raw: text })),
        resolved: true,
    });
    player.give(book);
}

function giveStarterWaystone(player) {
    player.give(Item.of('waystones:waystone'));
}

PlayerEvents.loggedIn(event => {
    const player = event.player;
    const data = player.persistentData;
    if (data.getBoolean(WELCOME_FLAG)) return;

    try {
        giveStarterWaystone(player);
        giveManual(player);
        data.putBoolean(WELCOME_FLAG, true);
        player.tell(Text.gold('Welcome to Brass & Sigil. Check your inventory for a Waystone and the server manual.'));
        console.info(`[bns/welcome] granted starter packet to ${player.username}`);
    } catch (e) {
        // If something goes wrong, don't set the flag -- player gets another chance.
        console.error(`[bns/welcome] failed for ${player.username}: ${e}`);
    }
});
