package com.ghost.technic.server.card;

import com.ghost.technic.server.card.effect.impl.SibylWaimareAbility;
import com.ghost.technic.server.game.GameTurnManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.ghost.technic.server.Player;
import com.ghost.technic.server.board.Flank;
import com.ghost.technic.server.card.misc.House;
import com.ghost.technic.server.card.playable.usable.impl.CreatureCard;
import com.ghost.technic.server.game.GameActionService;
import com.ghost.technic.server.game.GameState;
import com.ghost.technic.server.game.event.EventBus;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class SibylWaimareTest {

    GameActionService gameActionService;
    GameState gameState;
    Player opponent;
    Player controller;
    CreatureCard topCreatureCard;

    @BeforeEach
    void setUp() {
        gameState = new GameState(new EventBus());
        gameActionService = new GameActionService(gameState);

        controller = Player.builder().name("Waimare Controller").build();
        opponent = Player.builder().name("Target Player").build();

        gameState.setPlayers(controller, opponent);

        // Creature that will be exhausted
        CreatureCard brobnarCreature = CreatureCard.builder()
                .name("Brobnar Bruiser")
                .house(House.BROBNAR)
                .power(4)
                .armor(1)
                .ready(true)
                .build();

        opponent.getPlayArea().getBattleline().addToFlank(brobnarCreature, Flank.LEFT);
        gameState.setOwner(brobnarCreature, opponent);

        // Deck top card is BROBNAR
        topCreatureCard = CreatureCard.builder().name("Top card").house(House.BROBNAR).build();
        opponent.getDeck().add(topCreatureCard);
        gameState.setOwner(topCreatureCard, opponent);

        // Sibyl Waimare in play
        CreatureCard sibyl = CreatureCard.builder()
                .name("Sibyl Waimare")
                .house(House.DIS)
                .power(4)
                .armor(2)
                .abilities(List.of(new SibylWaimareAbility()))
                .build();

        controller.getPlayArea().getBattleline().addToFlank(sibyl, Flank.RIGHT);
        gameState.setOwner(sibyl, controller);
    }

    @Test
    void testSibylWaimareExhaustsOpponentCreaturesOfDiscardedHouse() {
        assertTrue(opponent.getPlayArea().getBattleline().getCreatures().getFirst().isReady());

        GameTurnManager turnManager = new GameTurnManager(gameState, gameActionService);
        turnManager.handleStartOfTurnAbilities(opponent);

        CreatureCard brobnarCreature = opponent.getPlayArea().getBattleline().getCreatures().getFirst();
        assertFalse(brobnarCreature.isReady(), "Creature of discarded house should be exhausted");
        assertEquals(topCreatureCard, opponent.getDiscardPile().getFirst(), "Top card should be discarded");
    }
}
