import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
import type { CostSessionRecord } from './cost-session-history';
import {
  appendCostSessionRecord,
  clearCostSessionHistory,
  costHistoryStorageKey,
  formatCostSessionDate,
  loadCostSessionHistory,
  removeCostSessionRecord,
  saveCostSessionHistory,
} from './cost-session-history';

const scope = `test-${Date.now()}`;

const sampleRecord = (overrides: Partial<CostSessionRecord> = {}): CostSessionRecord => ({
  id: 'rec-1',
  title: 'Equity Options Pricer',
  endedAt: '2026-06-14T12:00:00.000Z',
  costUsd: 1.86,
  tokensConsumed: 1_400_000,
  models: [{ model: 'claude-sonnet-4-6', provider: 'anthropic' }],
  ...overrides,
});

const suite = createLogicSuite('costSessionHistory');

suite('uses a scoped storage key', () => {
  assert.is(costHistoryStorageKey('my-app'), 'genesis-ai-assistant:cost-history:my-app');
});

suite('persists and loads records newest-first via append', () => {
  clearCostSessionHistory(scope);
  appendCostSessionRecord(scope, sampleRecord({ id: 'a', title: 'First' }));
  appendCostSessionRecord(scope, sampleRecord({ id: 'b', title: 'Second' }));
  const loaded = loadCostSessionHistory(scope);
  assert.is(loaded.length, 2);
  assert.is(loaded[0]?.id, 'b');
  assert.is(loaded[1]?.id, 'a');
  clearCostSessionHistory(scope);
});

suite('removes one record by id', () => {
  clearCostSessionHistory(scope);
  saveCostSessionHistory(scope, [sampleRecord({ id: 'keep' }), sampleRecord({ id: 'drop' })]);
  const remaining = removeCostSessionRecord(scope, 'drop');
  assert.is(remaining.length, 1);
  assert.is(remaining[0]?.id, 'keep');
  clearCostSessionHistory(scope);
});

suite('formats session dates for display', () => {
  assert.is(formatCostSessionDate('2026-06-14T12:00:00.000Z'), 'Jun 14, 2026');
});

suite.run();
