package fr.luc.gamingcore.model;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.EnumSet;
import java.util.UUID;

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

public class GuildModelTest {

    private Guild guild;
    private UUID leaderUuid;
    private UUID memberUuid;
    private GuildRank leaderRank;
    private GuildRank memberRank;

    @BeforeEach
    public void setUp() {
        UUID guildId = UUID.randomUUID();
        leaderUuid = UUID.randomUUID();
        memberUuid = UUID.randomUUID();

        guild = new Guild(guildId, "Valhalla", "VAL", "Guilde nordique", 500.0, System.currentTimeMillis());

        leaderRank = new GuildRank("leader", guildId, "Chef", 100, false, EnumSet.allOf(GuildPermission.class));
        memberRank = new GuildRank("member", guildId, "Membre", 1, true, EnumSet.of(GuildPermission.HOME, GuildPermission.BANK_DEPOSIT));

        guild.addRank(leaderRank);
        guild.addRank(memberRank);

        guild.addMember(new GuildMember(leaderUuid, guildId, "leader", System.currentTimeMillis(), System.currentTimeMillis(), 0.0));
        guild.addMember(new GuildMember(memberUuid, guildId, "member", System.currentTimeMillis(), System.currentTimeMillis(), 0.0));
    }

    @Test
    public void testGuildMembershipAndRanks() {
        assertEquals(2, guild.getMemberCount());
        assertTrue(guild.isMember(leaderUuid));
        assertTrue(guild.isMember(memberUuid));

        assertEquals(leaderRank, guild.getMemberRank(leaderUuid));
        assertEquals(memberRank, guild.getMemberRank(memberUuid));
        assertEquals(memberRank, guild.getDefaultRank());
        assertEquals(leaderRank, guild.getLeaderRank());
    }

    @Test
    public void testPermissions() {
        assertTrue(guild.hasPermission(leaderUuid, GuildPermission.CLAIM));
        assertTrue(guild.hasPermission(leaderUuid, GuildPermission.KICK));
        assertTrue(guild.hasPermission(leaderUuid, GuildPermission.BANK_WITHDRAW));

        assertTrue(guild.hasPermission(memberUuid, GuildPermission.HOME));
        assertTrue(guild.hasPermission(memberUuid, GuildPermission.BANK_DEPOSIT));
        assertFalse(guild.hasPermission(memberUuid, GuildPermission.CLAIM));
        assertFalse(guild.hasPermission(memberUuid, GuildPermission.BANK_WITHDRAW));
    }

    @Test
    public void testGuildColorAndTagFormatting() {
        assertEquals("#00C9FF", guild.getColor());
        assertEquals("<color:#00C9FF>[VAL]</color>", guild.getFormattedTag());
        assertEquals("<color:#00C9FF><bold>Valhalla</bold></color>", guild.getFormattedName());

        guild.setColor("#FF5555");
        guild.setTag("VALH");
        assertEquals("#FF5555", guild.getColor());
        assertEquals("<color:#FF5555>[VALH]</color>", guild.getFormattedTag());
    }
}
