// ─── Infra mocks ─────────────────────────────────────────────────────────────
// conversations.ts imports several modules that use import.meta.env (Vite-only).
// These mocks are a one-time setup — add more describe blocks freely below.

jest.mock('src/config/amplitude', () => ({ logEvent: jest.fn() }));
jest.mock('src/constants/env', () => ({ isDev: false, isStaging: false }));
jest.mock('src/pages/dashboard/Conversations/queries', () => ({
  getConversationInfo: jest.fn(),
  getConversationMembersData: jest.fn(),
}));
jest.mock('src/pages/dashboard/Conversations/stores/membersStore', () => ({
  getMembers: jest.fn(() => ({})),
}));
jest.mock('src/utils/attachments', () => ({
  isAudioAttachment: jest.fn(() => false),
  isFileAttachment: jest.fn(() => false),
  isImageAttachment: jest.fn(() => false),
  isVideoAttachment: jest.fn(() => false),
  splitFileName: jest.fn(() => ({ name: '', extension: '' })),
}));
jest.mock('src/utils/date', () => ({
  formatPillsDate: jest.fn(() => ''),
  isSameUTCDate: jest.fn(() => true),
}));
jest.mock('src/utils/userUtils', () => ({ getFullName: jest.fn(() => '') }));

// ─── Imports ──────────────────────────────────────────────────────────────────

import { ulid } from 'ulid';

import { CONVERSATIONS_SPACING } from 'src/pages/dashboard/Conversations/constants';
import {
  type BloackMessageForwarded,
  type BloackMessageReplied,
  type BlockCreated,
  BlockType,
  type HuDataConversationUser,
} from 'src/pages/dashboard/Conversations/types';

import {
  compareIds,
  formatTimeInPlayAudio,
  gifWidthAndHeightCalculator,
  isForwardedMessage,
  isKnownBlockType,
  isNewerMessage,
  isReplyMessage,
  isValidSearchQuery,
} from 'src/pages/dashboard/Conversations/utils';

// ─── compareIds ───────────────────────────────────────────────────────────────

describe('compareIds', () => {
  test('returns true when both ids are equal numbers', () => {
    expect(compareIds(1, 1)).toBe(true);
  });

  test('returns true when comparing a numeric string to a number', () => {
    expect(compareIds('42', 42)).toBe(true);
  });

  test('returns false when ids differ', () => {
    expect(compareIds(1, 2)).toBe(false);
  });

  test('returns false when either id is undefined', () => {
    expect(compareIds(undefined, 1)).toBe(false);
    expect(compareIds(1, undefined)).toBe(false);
  });

  test('returns false for falsy values like 0 or empty string', () => {
    expect(compareIds(0, 0)).toBe(false);
    expect(compareIds('', '')).toBe(false);
  });
});

// ─── gifWidthAndHeightCalculator ──────────────────────────────────────────────

describe('gifWidthAndHeightCalculator', () => {
  test('uses horizontal width when aspect ratio > 1', () => {
    const { width } = gifWidthAndHeightCalculator({ width: 400, height: 200 });
    expect(width).toBe(CONVERSATIONS_SPACING.gifHorizontalWidth);
  });

  test('uses vertical width when aspect ratio < 1', () => {
    const { width } = gifWidthAndHeightCalculator({ width: 100, height: 300 });
    expect(width).toBe(CONVERSATIONS_SPACING.gifVerticalWidth);
  });

  test('uses square width and keeps height equal when aspect ratio is 1', () => {
    const { width, height } = gifWidthAndHeightCalculator({
      width: 200,
      height: 200,
    });
    expect(width).toBe(CONVERSATIONS_SPACING.gifSquareWidth);
    expect(height).toBe(CONVERSATIONS_SPACING.gifSquareWidth);
  });
});

// ─── formatTimeInPlayAudio ────────────────────────────────────────────────────

describe('formatTimeInPlayAudio', () => {
  test('formats 0 seconds as "0:00"', () => {
    expect(formatTimeInPlayAudio(0)).toBe('0:00');
  });

  test('pads single-digit seconds with a leading zero', () => {
    expect(formatTimeInPlayAudio(5)).toBe('0:05');
  });

  test('formats minutes and seconds correctly', () => {
    expect(formatTimeInPlayAudio(90)).toBe('1:30');
  });
});

// ─── isValidSearchQuery ───────────────────────────────────────────────────────

describe('isValidSearchQuery', () => {
  test('returns false for empty string', () => {
    expect(isValidSearchQuery('')).toBe(false);
  });

  test('returns false for whitespace-only input', () => {
    expect(isValidSearchQuery('   ')).toBe(false);
  });

  test('returns false when input has only special characters', () => {
    expect(isValidSearchQuery('!!!')).toBe(false);
  });

  test('returns true for a normal word', () => {
    expect(isValidSearchQuery('hello')).toBe(true);
  });

  test('returns true for unicode letters', () => {
    expect(isValidSearchQuery('Ñandú')).toBe(true);
  });
});

// ─── isNewerMessage ───────────────────────────────────────────────────────────

describe('isNewerMessage', () => {
  test('returns true when newTs is more recent than currentTs', () => {
    const older = ulid(Date.now() - 5000);
    const newer = ulid(Date.now());
    expect(isNewerMessage({ newTs: newer, currentTs: older })).toBe(true);
  });

  test('returns false when newTs is older than currentTs', () => {
    const older = ulid(Date.now() - 5000);
    const newer = ulid(Date.now());
    expect(isNewerMessage({ newTs: older, currentTs: newer })).toBe(false);
  });

  test('returns false when either ts is missing', () => {
    const ts = ulid();
    expect(isNewerMessage({ newTs: ts, currentTs: undefined })).toBe(false);
    expect(isNewerMessage({ newTs: undefined, currentTs: ts })).toBe(false);
    expect(isNewerMessage({})).toBe(false);
  });
});

// ─── isKnownBlockType / isReplyMessage / isForwardedMessage ──────────────────

const blockCreated: BlockCreated = { type: BlockType.CREATED };
const blockReplied: BloackMessageReplied = {
  type: BlockType.MESSAGE_REPLIED,
  message_replied_id: '',
  message_replied_client_msg_id: '',
  assets: [],
  sender_membership: {
    id: '',
    user: {
      id: '',
      name: '',
      team_id: '',
      hu_data: {} as HuDataConversationUser,
    },
    version: 1,
  },
};
const blockForwarded: BloackMessageForwarded = {
  type: BlockType.MESSAGE_FORWARDED,
};

describe('isKnownBlockType', () => {
  test('returns true when all blocks have a known type', () => {
    expect(isKnownBlockType([blockCreated, blockReplied])).toBe(true);
  });

  test('returns false when a block has an unknown type', () => {
    expect(
      isKnownBlockType([
        { type: 'totally_unknown' } as unknown as BlockCreated,
      ]),
    ).toBe(false);
  });

  test('returns false for empty array', () => {
    expect(isKnownBlockType([])).toBe(false);
  });
});

describe('isReplyMessage', () => {
  test('returns true when at least one block is MESSAGE_REPLIED', () => {
    expect(isReplyMessage([blockReplied])).toBe(true);
  });

  test('returns false when no block is MESSAGE_REPLIED', () => {
    expect(isReplyMessage([blockCreated])).toBe(false);
  });

  test('returns false for empty array', () => {
    expect(isReplyMessage([])).toBe(false);
  });
});

describe('isForwardedMessage', () => {
  test('returns true when at least one block is MESSAGE_FORWARDED', () => {
    expect(isForwardedMessage([blockForwarded])).toBe(true);
  });

  test('returns false when no block is MESSAGE_FORWARDED', () => {
    expect(isForwardedMessage([blockCreated])).toBe(false);
  });

  test('returns false for empty array', () => {
    expect(isForwardedMessage([])).toBe(false);
  });
});
