import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { shouldReplyInWhatsAppGroup } from './groupTalkbackGate.js';

describe('shouldReplyInWhatsAppGroup', () => {
  const channelPhone = '+27821234567';
  const channelLid = '74097446932518';

  it('replies when channel is @mentioned via mentionedPhones', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'Can you check this?',
        channelPhone,
        mentionedPhones: ['27821234567'],
      }),
      true,
    );
  });

  it('replies when channel LID is in mentionedPhones', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'I need support',
        channelPhone,
        mentionedPhones: [channelLid],
        channelMentionIds: [channelLid],
      }),
      true,
    );
  });

  it('replies when text contains @LID matching channelMentionIds', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: `@${channelLid} i need to book a call out please`,
        channelPhone,
        channelMentionIds: [channelLid],
      }),
      true,
    );
  });

  it('replies when text contains @silicon support', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: '@Silicon Support please help with the pump',
        channelPhone,
      }),
      true,
    );
  });

  it('replies to a clear question without mention', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'How do I reset the unit?',
        channelPhone,
      }),
      true,
    );
  });

  it('stays silent for casual group chatter', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'See you on site tomorrow',
        channelPhone,
        channelMentionIds: [channelLid],
      }),
      false,
    );
  });

  it('stays silent when a different LID is tagged', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: '@99988877766655 hey',
        channelPhone,
        mentionedPhones: ['99988877766655'],
        channelMentionIds: [channelLid],
      }),
      false,
    );
  });

  it('replies to untagged follow-up when prior outbound asked a question', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'The printer shows error 49 and will not print',
        channelPhone,
        priorOutbound: {
          content: "Could you please describe the printer issue you're facing?",
        },
      }),
      true,
    );
  });

  it('stays silent for casual chatter even after channel question', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'lol',
        channelPhone,
        priorOutbound: {
          content: 'Could you describe the issue?',
        },
      }),
      false,
    );
  });

  it('stays silent when last outbound did not ask a question', () => {
    assert.equal(
      shouldReplyInWhatsAppGroup({
        text: 'tomorrow at 3',
        channelPhone,
        priorOutbound: {
          content: 'Thanks — we will follow up shortly.',
        },
      }),
      false,
    );
  });
});
