package thb.jeanluc.adventure.model;

import org.junit.jupiter.api.Test;
import thb.jeanluc.adventure.model.item.Item;
import thb.jeanluc.adventure.model.item.PlainItem;

import static org.assertj.core.api.Assertions.assertThat;

class RoomTest {

    @Test
    void addExit_withNewDirection_storesNeighbour() {
        Room a = new Room("a", "A", "first");
        Room b = new Room("b", "B", "second");

        a.addExit(Direction.NORTH, b);

        assertThat(a.getExit(Direction.NORTH)).contains(b);
        assertThat(a.getExit(Direction.SOUTH)).isEmpty();
    }

    @Test
    void addItem_storesUnderItemId() {
        Room room = new Room("r", "R", "d");
        Item lamp = PlainItem.builder().id("lamp").name("Lamp").description("d").build();

        room.addItem(lamp);

        assertThat(room.findItem("lamp")).contains(lamp);
    }

    @Test
    void removeItem_returnsAndDetachesItem() {
        Room room = new Room("r", "R", "d");
        Item lamp = PlainItem.builder().id("lamp").name("Lamp").description("d").build();
        room.addItem(lamp);

        var removed = room.removeItem("lamp");

        assertThat(removed).contains(lamp);
        assertThat(room.findItem("lamp")).isEmpty();
    }

    @Test
    void removeItem_unknownId_returnsEmpty() {
        Room room = new Room("r", "R", "d");
        assertThat(room.removeItem("nope")).isEmpty();
    }

    @Test
    void addNpc_andFindById() {
        Room room = new Room("r", "R", "d");
        Npc npc = Npc.shell("man", "Man", "d", "hi");

        room.addNpc(npc);

        assertThat(room.findNpc("man")).contains(npc);
    }

    @Test
    void items_preserveInsertionOrder() {
        Room room = new Room("r", "R", "d");
        room.addItem(PlainItem.builder().id("a").name("A").description("d").build());
        room.addItem(PlainItem.builder().id("b").name("B").description("d").build());
        room.addItem(PlainItem.builder().id("c").name("C").description("d").build());

        assertThat(room.getItems().keySet()).containsExactly("a", "b", "c");
    }
}
