import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { MemoryDatastoreProvider } from '@bytelyst/datastore';
import { _resetDatastoreProvider, setProvider } from '../../lib/datastore.js';
import * as repo from './repository.js';

describe('support cases repository', () => {
  beforeEach(() => {
    setProvider(new MemoryDatastoreProvider());
  });

  afterEach(() => {
    _resetDatastoreProvider();
  });

  it('stores support cases, notes, and escalations', async () => {
    await repo.createCase({
      id: 'sup_1',
      productId: 'lysnrai',
      title: 'Agent escalated an incident',
      status: 'open',
      priority: 'high',
      source: 'agent',
      tags: ['incident'],
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createNote({
      id: 'sup_1:note:1',
      caseId: 'sup_1',
      productId: 'lysnrai',
      authorType: 'user',
      authorId: 'admin_1',
      visibility: 'internal',
      body: 'Initial triage completed.',
      createdAt: '2026-03-15T00:01:00.000Z',
    });

    await repo.createEscalation({
      id: 'sup_1:esc:1',
      caseId: 'sup_1',
      productId: 'lysnrai',
      escalatedTo: 'tier2',
      reason: 'Customer impact confirmed',
      triggeredBy: 'admin_1',
      createdAt: '2026-03-15T00:02:00.000Z',
    });

    const cases = await repo.listCases('lysnrai', { limit: 20 });
    const notes = await repo.listNotes('sup_1');
    const escalations = await repo.listEscalations('sup_1');

    expect(cases).toHaveLength(1);
    expect(notes[0].body).toContain('triage');
    expect(escalations[0].escalatedTo).toBe('tier2');
  });
});
