import { describe, expect, it } from 'vitest';
import { PgDialect } from 'drizzle-orm/pg-core';

import { activeInterestsWhere } from '@/lib/services/active-interest';

/**
 * Locks in the canonical active-interest predicate per
 * `docs/PRE-DEPLOY-PLAN.md` § 1.1.2 / commit f86f511. The whole pipeline
 * report + recommender + reminder + restore + dossier surface depends on
 * this predicate matching the same set of rows on every read; if it
 * drifts, "active interest" stops meaning the same thing across
 * dashboard / kanban / hot deals / PDF reports.
 */
describe('activeInterestsWhere', () => {
  const PORT_ID = '11111111-1111-1111-1111-111111111111';
  const dialect = new PgDialect();

  it('compiles to: scoped to portId AND not archived AND no terminal outcome', () => {
    const fragment = activeInterestsWhere(PORT_ID);
    const compiled = dialect.sqlToQuery(fragment);

    expect(compiled.sql).toContain('"port_id"');
    expect(compiled.sql).toContain('"archived_at"');
    expect(compiled.sql).toContain('"outcome"');
    expect(compiled.sql).toMatch(/"archived_at"\s+is\s+null/i);
    expect(compiled.sql).toMatch(/"outcome"\s+is\s+null/i);
    expect(compiled.params).toContain(PORT_ID);
  });

  it('does NOT include the legacy `outcome = "won"` permissive branch', () => {
    const fragment = activeInterestsWhere(PORT_ID);
    const compiled = dialect.sqlToQuery(fragment);

    expect(compiled.sql.toLowerCase()).not.toContain("'won'");
    expect(compiled.params).not.toContain('won');
  });
});
