const ROOM_CARDS = [
    { type: 'tile_4x8', width: 4, height: 8, exits: ['N', 'S'], name: "Pasillo Largo" },
    { type: 'tile_base', width: 4, height: 4, exits: ['N', 'E', 'W'], name: "Sala Pequeña" },
    { type: 'tile_base', width: 4, height: 4, exits: ['N', 'S', 'E', 'W'], name: "Intersección" },
    { type: 'tile_8x4', width: 8, height: 4, exits: ['N', 'S'], name: "Sala Ancha" }
];

export class DungeonDeck {
    constructor() {
        this.deck = [];
        this.discardPile = [];
        this.shuffleDeck();
    }

    shuffleDeck() {
        // Create a new deck with multiple copies of cards
        this.deck = [
            ...ROOM_CARDS, ...ROOM_CARDS, ...ROOM_CARDS, // 3 copies of each
            { type: 'tile_8x8', width: 8, height: 8, exits: ['N'], name: "Sala del Tesoro" } // 1 Objective Room
        ];

        // Fisher-Yates shuffle
        for (let i = this.deck.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
        }
        console.log("Dungeon Deck Shuffled:", this.deck.length, "cards");
    }

    drawCard() {
        if (this.deck.length === 0) {
            console.warn("Deck empty! Reshuffling discards...");
            if (this.discardPile.length === 0) {
                console.error("No cards left!");
                return null;
            }
            this.deck = [...this.discardPile];
            this.discardPile = [];
            this.shuffleDeck();
        }

        const card = this.deck.pop();
        this.discardPile.push(card);
        return card;
    }
}

export const dungeonDeck = new DungeonDeck();
