import { describe, it, expect, beforeEach } from 'vitest';
import { createStore } from 'jotai';
import Graph from 'graphology';
import type { GraphNodeAttributes, GraphEdgeAttributes } from '../core/types';
import { graphAtom, graphOptions } from '../core/graph-store';
import {
	virtualizationMetricsAtom,
} from '../core/virtualization-store';
import {
	setVirtualizationEnabledAtom,
	toggleVirtualizationAtom,
	virtualizationEnabledAtom,
} from '../core/settings-store';

describe('useVirtualization (atom-level)', () => {
	let store: ReturnType<typeof createStore>;

	beforeEach(() => {
		store = createStore();
		store.set(graphAtom, new Graph<GraphNodeAttributes, GraphEdgeAttributes>(graphOptions));
	});

	it('metrics returns totalNodes=0 on empty graph', () => {
		const metrics = store.get(virtualizationMetricsAtom);
		expect(metrics.totalNodes).toBe(0);
		expect(metrics.totalEdges).toBe(0);
	});

	it('enabled defaults to true', () => {
		const metrics = store.get(virtualizationMetricsAtom);
		expect(metrics.enabled).toBe(true);
	});

	it('setVirtualizationEnabledAtom toggles enabled', () => {
		store.set(setVirtualizationEnabledAtom, false);
		expect(store.get(virtualizationEnabledAtom)).toBe(false);
		store.set(setVirtualizationEnabledAtom, true);
		expect(store.get(virtualizationEnabledAtom)).toBe(true);
	});

	it('toggleVirtualizationAtom toggles state', () => {
		const before = store.get(virtualizationEnabledAtom);
		store.set(toggleVirtualizationAtom);
		expect(store.get(virtualizationEnabledAtom)).toBe(!before);
	});

	it('culledNodes is 0 on empty graph', () => {
		const metrics = store.get(virtualizationMetricsAtom);
		expect(metrics.culledNodes).toBe(0);
	});
});
