package com.ghost.technic.server.card;

import com.ghost.technic.server.card.effect.impl.BulwarkAbility;
import com.ghost.technic.server.counter.Counters;
import lombok.Data;
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.*;

@Data
class BulwarkTest {

    private Player player;
    private CreatureCard bulwark;
    private CreatureCard leftNeighbor;
    private CreatureCard rightNeighbor;
    private GameActionService gameActionService;

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

        player = Player.builder().name("TestPlayer").build();
        gameActionService.getGameState().setPlayers(player, Player.builder().name("Opponent").build());

        bulwark = CreatureCard.builder()
                .name("Bulwark")
                .house(House.SANCTUM)
                .power(4)
                .armor(2)
                .traits(List.of("Human", "Knight"))
                .abilities(List.of(new BulwarkAbility()))
                .counters(new Counters())
                .build();

        leftNeighbor = CreatureCard.builder()
                .name("Lefty")
                .house(House.BROBNAR)
                .power(3)
                .armor(1)
                .traits(List.of("Beast"))
                .counters(new Counters())
                .build();

        rightNeighbor = CreatureCard.builder()
                .name("Righty")
                .house(House.BROBNAR)
                .power(3)
                .armor(1)
                .traits(List.of("Beast"))
                .counters(new Counters())
                .build();


        // Add cards to battleline: Lefty - Bulwark - Righty
        gameActionService.playCreature(player, leftNeighbor, Flank.LEFT);
        gameActionService.playCreature(player, bulwark, Flank.RIGHT);
        gameActionService.playCreature(player, rightNeighbor, Flank.RIGHT);
    }

    @Test
    void testBulwarkGrantsArmorToNeighbors() {
        assertEquals(3, leftNeighbor.getEffectiveArmor(gameActionService.getGameState()), "Left neighbor should have +2 armor");
        assertEquals(3, rightNeighbor.getEffectiveArmor(gameActionService.getGameState()), "Right neighbor should have +2 armor");
    }

    @Test
    void testBulwarkStaticEffectRemovedOnDestroy() {
        // BulwarkAbility should have already subscribed during setup
        gameActionService.getGameState().setOwner(bulwark, player);
        gameActionService.destroyCreature(bulwark);

        // Ensure Bulwark's effect has been unregistered
        assertFalse(gameActionService.getGameState().getStaticEffects().containsKey(bulwark),
                "Bulwark's static effect should be removed from game state");
    }
}
