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('orgs repository', () => {
  beforeEach(() => {
    setProvider(new MemoryDatastoreProvider());
  });

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

  it('creates orgs, workspaces, and memberships', async () => {
    await repo.createOrganization({
      id: 'org_1',
      productId: 'lysnrai',
      name: 'Acme',
      slug: 'acme',
      status: 'active',
      ownerUserId: 'user_1',
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createWorkspace({
      id: 'ws_1',
      orgId: 'org_1',
      productId: 'lysnrai',
      name: 'Operations',
      slug: 'operations',
      status: 'active',
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    await repo.createMembership({
      id: 'mbr_1',
      orgId: 'org_1',
      productId: 'lysnrai',
      scope: 'workspace',
      workspaceId: 'ws_1',
      userId: 'user_2',
      role: 'member',
      status: 'active',
      createdAt: '2026-03-15T00:00:00.000Z',
      updatedAt: '2026-03-15T00:00:00.000Z',
    });

    const orgs = await repo.listOrganizations('lysnrai', { limit: 20 });
    const workspaces = await repo.listWorkspaces('org_1');
    const memberships = await repo.listMemberships('org_1', { limit: 100 });

    expect(orgs).toHaveLength(1);
    expect(workspaces[0].name).toBe('Operations');
    expect(memberships[0].workspaceId).toBe('ws_1');
  });
});
