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 { getCompanyAutomationGate } from './phishing-automation-gate';

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

  it('resolves to all-false when no row exists for the company', async () => {
    queryMock.mockResolvedValueOnce({ rows: [] });

    const gate = await getCompanyAutomationGate(123);

    expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false });
    expect(queryMock).toHaveBeenCalledTimes(1);
  });

  it('resolves to the mapped camelCase values when a row exists', async () => {
    queryMock.mockResolvedValueOnce({
      rows: [{ auto_parse: true, auto_classify: false, auto_report: true }],
    });

    const gate = await getCompanyAutomationGate(456);

    expect(gate).toEqual({ autoParse: true, autoClassify: false, autoReport: true });
  });

  it('resolves to all-false without querying when companyId is null', async () => {
    const gate = await getCompanyAutomationGate(null);

    expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false });
    expect(queryMock).not.toHaveBeenCalled();
  });

  it('resolves to all-false without querying when companyId is NaN', async () => {
    const gate = await getCompanyAutomationGate(NaN);

    expect(gate).toEqual({ autoParse: false, autoClassify: false, autoReport: false });
    expect(queryMock).not.toHaveBeenCalled();
  });
});
