import assert from 'node:assert/strict';
import test from 'node:test';
import type { SecurityTier } from '@pig/core';
import {
  capacityMeetsRequirement,
  type AvailabilityRow,
} from '../src/services/capacity';

function capacity(securityTier: SecurityTier): AvailabilityRow {
  return {
    commitmentId: '10000000-0000-4000-8000-000000000001',
    accountId: '20000000-0000-4000-8000-000000000001',
    name: `${securityTier} block`,
    gpuType: 'H100_80GB',
    gpuCount: 8,
    interconnectType: 'Infiniband',
    securityTier,
    startsAt: new Date('2026-01-01T00:00:00Z'),
    endsAt: new Date('2027-01-01T00:00:00Z'),
    totalGpuHours: 10_000,
    soldGpuHours: 0,
    heldGpuHours: 0,
    availableGpuHours: 10_000,
    costPerGpuHourCents: 100,
    utilisation: 0,
    breakEvenPriceCents: 100,
  };
}

test('government demand excludes both community and ordinary secure cloud', () => {
  const requirement = { gpuCount: 1, minSecurityTier: 'government' as const };
  assert.equal(capacityMeetsRequirement(capacity('community_cloud'), requirement), false);
  assert.equal(capacityMeetsRequirement(capacity('secure_cloud'), requirement), false);
  assert.equal(capacityMeetsRequirement(capacity('government'), requirement), true);
});

test('higher classifications may satisfy lower requirements', () => {
  assert.equal(
    capacityMeetsRequirement(capacity('government'), {
      gpuCount: 1,
      minSecurityTier: 'secure_cloud',
    }),
    true,
  );
  assert.equal(
    capacityMeetsRequirement(capacity('secure_cloud'), {
      gpuCount: 1,
      minSecurityTier: 'community_cloud',
    }),
    true,
  );
});

test('security sufficiency never overrides the export-control predicate', () => {
  const block = capacity('government');
  assert.equal(
    capacityMeetsRequirement(block, {
      gpuCount: 1,
      minSecurityTier: 'government',
      complianceDecision: { decision: 'block', supersededAt: null },
    }),
    false,
  );
  assert.equal(
    capacityMeetsRequirement(block, {
      gpuCount: 1,
      minSecurityTier: 'government',
      complianceDecision: null,
    }),
    false,
  );
  assert.equal(
    capacityMeetsRequirement(block, {
      gpuCount: 1,
      minSecurityTier: 'government',
      complianceDecision: { decision: 'allow', supersededAt: new Date() },
    }),
    false,
  );
  assert.equal(
    capacityMeetsRequirement(block, {
      gpuCount: 1,
      minSecurityTier: 'government',
      complianceDecision: { decision: 'allow', supersededAt: null },
    }),
    true,
  );
});
