import assert from 'node:assert/strict';
import test from 'node:test';

import { buildV22RequirementQuery } from './requirement-v22-query';

test('buildV22RequirementQuery creates a product-scoped query with server-side filters', () => {
  const query = buildV22RequirementQuery({
    selectedScope: { type: 'product', productId: 'product-1' },
    projects: [],
    statusFilter: 'adopted',
    priorityFilter: 'P1',
    typeFilter: 'feature',
    versionFilter: 'version-1',
    search: 'login',
    dateSort: 'asc',
    limit: 50,
    cursor: 'req-1',
  });

  assert.deepEqual(query, {
    productId: 'product-1',
    status: 'adopted',
    priority: 'P1',
    type: 'feature',
    versionId: 'version-1',
    q: 'login',
    sort: 'created_at_asc',
    limit: 50,
    cursor: 'req-1',
  });
});

test('buildV22RequirementQuery resolves project scope to product and project partition keys', () => {
  const query = buildV22RequirementQuery({
    selectedScope: { type: 'project', projectId: 'project-1' },
    projects: [{ id: 'project-1', productId: 'product-1' }],
    statusFilter: 'all',
    priorityFilter: 'all',
    typeFilter: 'all',
    versionFilter: 'all',
    search: '  ',
    dateSort: 'desc',
    limit: 20,
  });

  assert.deepEqual(query, {
    productId: 'product-1',
    projectId: 'project-1',
    sort: 'created_at_desc',
    limit: 20,
  });
});

test('buildV22RequirementQuery skips scopes that would require full scans or derived status filters', () => {
  assert.equal(buildV22RequirementQuery({
    selectedScope: { type: 'all' },
    projects: [],
    statusFilter: 'all',
    priorityFilter: 'all',
    typeFilter: 'all',
    versionFilter: 'all',
    search: '',
    dateSort: 'desc',
    limit: 20,
  }), undefined);

  assert.equal(buildV22RequirementQuery({
    selectedScope: { type: 'product', productId: 'product-1' },
    projects: [],
    statusFilter: 'dev_completed',
    priorityFilter: 'all',
    typeFilter: 'all',
    versionFilter: 'all',
    search: '',
    dateSort: 'desc',
    limit: 20,
  }), undefined);

  assert.equal(buildV22RequirementQuery({
    selectedScope: { type: 'project', projectId: 'missing-project' },
    projects: [{ id: 'project-1', productId: 'product-1' }],
    statusFilter: 'all',
    priorityFilter: 'all',
    typeFilter: 'all',
    versionFilter: 'all',
    search: '',
    dateSort: 'desc',
    limit: 20,
  }), undefined);
});
