import { Events, type Client } from 'discord.js';
import { t } from '@nexumi/shared';
import type { BotContext } from '../../types.js';
import { getGuildLocale } from '../../i18n.js';
import { logger } from '../../logger.js';
import {
  buildTagReply,
  findTriggeredTag,
  memberCanUseTag,
  TagError
} from './service.js';

export function registerTagEvents(client: Client, context: BotContext): void {
  client.on(Events.MessageCreate, async (message) => {
    try {
      if (!message.guild || message.author.bot || !message.content.trim()) {
        return;
      }

      const tag = await findTriggeredTag(context, message.guild.id, message.content);
      if (!tag) {
        return;
      }

      const member = await message.guild.members.fetch(message.author.id).catch(() => null);
      if (!member) {
        return;
      }

      if (!memberCanUseTag(tag, member, message.channel.id)) {
        return;
      }

      await context.prisma.tag.update({
        where: { id: tag.id },
        data: { useCount: { increment: 1 } }
      });

      const payload = buildTagReply(tag, member, message.guild, '');
      await message.reply(payload);
    } catch (error) {
      if (error instanceof TagError) {
        const locale = await getGuildLocale(context.prisma, message.guild?.id ?? null);
        logger.debug({ code: error.code, tag: error.message }, t(locale, `tag.error.${error.code}`));
        return;
      }
      logger.error({ error, messageId: message.id }, 'Tag auto-responder failed');
    }
  });
}
