import { describe, expect, it } from 'vitest';
import { emojiMatches, shouldIgnoreMessage } from './service.js';

function reactionEmoji(partial: { id?: string | null; name?: string | null }) {
  return {
    id: partial.id ?? null,
    name: partial.name ?? null,
    identifier: partial.id ? `${partial.name}:${partial.id}` : (partial.name ?? ''),
    toString() {
      if (partial.id) {
        return `<:${partial.name}:${partial.id}>`;
      }
      return partial.name ?? '';
    }
  } as unknown as import('discord.js').Emoji;
}

describe('starboard emojiMatches', () => {
  it('matches unicode emoji', () => {
    expect(emojiMatches('⭐', reactionEmoji({ name: '⭐' }))).toBe(true);
  });

  it('matches custom emoji forms', () => {
    const emoji = reactionEmoji({ id: '123', name: 'star' });
    expect(emojiMatches('<:star:123>', emoji)).toBe(true);
    expect(emojiMatches('star:123', emoji)).toBe(true);
  });
});

describe('starboard shouldIgnoreMessage', () => {
  const baseConfig = {
    id: 'cfg',
    guildId: 'g1',
    enabled: true,
    channelId: 'star-channel',
    emoji: '⭐',
    threshold: 3,
    allowSelfStar: false,
    ignoredChannelIds: ['ignored'],
    createdAt: new Date(),
    updatedAt: new Date()
  };

  it('ignores bot authors', () => {
    const message = {
      author: { bot: true },
      guild: { id: 'g1' },
      channel: {
        id: 'c1',
        isTextBased: () => true,
        isDMBased: () => false,
        nsfw: false
      }
    } as unknown as import('discord.js').Message;
    expect(shouldIgnoreMessage(message, baseConfig)).toBe(true);
  });

  it('ignores the starboard channel itself', () => {
    const message = {
      author: { bot: false, id: 'u1' },
      guild: { id: 'g1' },
      channel: {
        id: 'star-channel',
        isTextBased: () => true,
        isDMBased: () => false,
        nsfw: false
      }
    } as unknown as import('discord.js').Message;
    expect(shouldIgnoreMessage(message, baseConfig)).toBe(true);
  });

  it('ignores listed channels', () => {
    const message = {
      author: { bot: false, id: 'u1' },
      guild: { id: 'g1' },
      channel: {
        id: 'ignored',
        isTextBased: () => true,
        isDMBased: () => false,
        nsfw: false
      }
    } as unknown as import('discord.js').Message;
    expect(shouldIgnoreMessage(message, baseConfig)).toBe(true);
  });
});
