import { describe, it, expect, vi, beforeEach } from 'vitest';

// Mock postgresClient BEFORE importing the module under test — mirrors
// campaign-classifier.test.ts's vi.mock() factory-mocking discipline.
const queryMock = vi.fn();
vi.mock('./postgres-client', () => ({
  postgresClient: {
    query: (...args: unknown[]) => queryMock(...args),
  },
}));

// eslint-disable-next-line import/first -- imported after vi.mock hoisting
import { resolveTicketToCampaign } from './phishing-ticket-resolver';

describe('resolveTicketToCampaign', () => {
  beforeEach(() => {
    queryMock.mockReset();
  });

  it('returns { found: false } when no reports row exists for the ticket id', async () => {
    queryMock.mockResolvedValue({ rows: [], rowCount: 0 });

    const result = await resolveTicketToCampaign(999);

    expect(result).toEqual({ found: false });
  });

  it('returns { found: true, campaignId: null } for an ungrouped report', async () => {
    queryMock.mockResolvedValue({
      rows: [{ id: 'report-1', campaign_id: null, ticket_number: 'T20260716.0001' }],
      rowCount: 1,
    });

    const result = await resolveTicketToCampaign(123);

    expect(result).toEqual({
      found: true,
      reportId: 'report-1',
      campaignId: null,
      ticketNumber: 'T20260716.0001',
    });
  });

  it('returns { found: true, reportId, campaignId, ticketNumber } for a grouped report', async () => {
    queryMock.mockResolvedValue({
      rows: [
        {
          id: 'report-2',
          campaign_id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
          ticket_number: 'T20260716.0002',
        },
      ],
      rowCount: 1,
    });

    const result = await resolveTicketToCampaign(456);

    expect(result).toEqual({
      found: true,
      reportId: 'report-2',
      campaignId: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
      ticketNumber: 'T20260716.0002',
    });
  });

  it('queries reports filtered by ticket_id with a parameterized query', async () => {
    queryMock.mockResolvedValue({ rows: [], rowCount: 0 });

    await resolveTicketToCampaign(1);

    expect(queryMock).toHaveBeenCalledWith(
      expect.stringContaining('FROM reports WHERE ticket_id = $1'),
      [1]
    );
  });
});
